Skip to content

Commit 5782d6b

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents efa9d81 + c28d352 commit 5782d6b

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

src/Reflection/BetterReflection/SourceLocator/PhpFileCleaner.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use function in_array;
88
use function preg_match;
99
use function preg_quote;
10+
use function strcspn;
1011
use function strlen;
12+
use function strspn;
1113
use function substr;
1214

1315
/**
@@ -20,7 +22,7 @@ final class PhpFileCleaner
2022
/** @var array<array{name: string, length: int, pattern: string}> */
2123
private array $typeConfig = [];
2224

23-
private string $restPattern;
25+
private string $rejectChars;
2426

2527
private string $contents = '';
2628

@@ -38,7 +40,7 @@ public function __construct()
3840
];
3941
}
4042

41-
$this->restPattern = '{[^{}?"\'</d' . implode('', array_keys($this->typeConfig)) . ']+}A';
43+
$this->rejectChars = '{}?"\'</d' . implode('', array_keys($this->typeConfig));
4244
}
4345

4446
public function clean(string $contents, int $maxMatches): string
@@ -150,9 +152,10 @@ public function clean(string $contents, int $maxMatches): string
150152
}
151153

152154
$this->index += 1;
153-
if ($this->match($this->restPattern, $match)) {
154-
$clean .= $char . $match[0];
155-
$this->index += strlen($match[0]);
155+
$skip = strcspn($this->contents, $this->rejectChars, $this->index);
156+
if ($skip > 0) {
157+
$clean .= $char . substr($this->contents, $this->index, $skip);
158+
$this->index += $skip;
156159
} else {
157160
$clean .= $char;
158161
}
@@ -203,8 +206,13 @@ private function consumeString(string $delimiter): string
203206

204207
private function skipString(string $delimiter): void
205208
{
209+
$rejectChars = '\\' . $delimiter;
206210
$this->index += 1;
207211
while ($this->index < $this->len) {
212+
$this->index += strcspn($this->contents, $rejectChars, $this->index);
213+
if ($this->index >= $this->len) {
214+
break;
215+
}
208216
if ($this->contents[$this->index] === '\\' && ($this->peek('\\') || $this->peek($delimiter))) {
209217
$this->index += 2;
210218
continue;
@@ -221,7 +229,9 @@ private function skipComment(): void
221229
{
222230
$this->index += 2;
223231
while ($this->index < $this->len) {
224-
if ($this->contents[$this->index] === '*' && $this->peek('/')) {
232+
$this->index += strcspn($this->contents, '*', $this->index);
233+
234+
if ($this->peek('/')) {
225235
$this->index += 2;
226236
break;
227237
}
@@ -232,12 +242,7 @@ private function skipComment(): void
232242

233243
private function skipToNewline(): void
234244
{
235-
while ($this->index < $this->len) {
236-
if (in_array($this->contents[$this->index], ["\r", "\n"], true)) {
237-
return;
238-
}
239-
$this->index += 1;
240-
}
245+
$this->index += strcspn($this->contents, "\r\n", $this->index);
241246
}
242247

243248
private function skipHeredoc(string $delimiter): void
@@ -265,16 +270,10 @@ private function skipHeredoc(string $delimiter): void
265270
}
266271

267272
// skip the rest of the line
268-
while ($this->index < $this->len) {
269-
$this->skipToNewline();
273+
$this->skipToNewline();
270274

271-
// skip newlines
272-
while ($this->index < $this->len && ($this->contents[$this->index] === "\r" || $this->contents[$this->index] === "\n")) {
273-
$this->index += 1;
274-
}
275-
276-
break;
277-
}
275+
// skip newlines
276+
$this->index += strspn($this->contents, "\r\n", $this->index);
278277
}
279278
}
280279

0 commit comments

Comments
 (0)