Skip to content

Commit 5e6b8dd

Browse files
committed
Micro optimizations on context factory
1 parent a288636 commit 5e6b8dd

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ setup: install-phive
1313

1414
.PHONY: phpcs
1515
phpcs:
16-
docker run -it --rm -v${PWD}:/opt/project -w /opt/project phpdoc/phpcs-ga:v1.0.0 -s
16+
docker run -it --rm -v${PWD}:/opt/project -w /opt/project phpdoc/phpcs-ga:latest -s
1717

1818
.PHONY: phpstan
1919
phpstan:

src/Types/ContextFactory.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Reflector;
2424
use RuntimeException;
2525
use UnexpectedValueException;
26-
use function array_merge;
2726
use function file_exists;
2827
use function file_get_contents;
2928
use function get_class;
@@ -145,7 +144,8 @@ public function createForNamespace(string $namespace, string $fileContents) : Co
145144
$tokens = new ArrayIterator(token_get_all($fileContents));
146145

147146
while ($tokens->valid()) {
148-
switch ($tokens->current()[0]) {
147+
$currentToken = $tokens->current();
148+
switch ($currentToken[0]) {
149149
case T_NAMESPACE:
150150
$currentNamespace = $this->parseNamespace($tokens);
151151
break;
@@ -156,17 +156,18 @@ public function createForNamespace(string $namespace, string $fileContents) : Co
156156
$braceLevel = 0;
157157
$firstBraceFound = false;
158158
while ($tokens->valid() && ($braceLevel > 0 || !$firstBraceFound)) {
159-
if ($tokens->current() === '{'
160-
|| $tokens->current()[0] === T_CURLY_OPEN
161-
|| $tokens->current()[0] === T_DOLLAR_OPEN_CURLY_BRACES) {
159+
$currentToken = $tokens->current();
160+
if ($currentToken === '{'
161+
|| $currentToken[0] === T_CURLY_OPEN
162+
|| $currentToken[0] === T_DOLLAR_OPEN_CURLY_BRACES) {
162163
if (!$firstBraceFound) {
163164
$firstBraceFound = true;
164165
}
165166

166167
++$braceLevel;
167168
}
168169

169-
if ($tokens->current() === '}') {
170+
if ($currentToken === '}') {
170171
--$braceLevel;
171172
}
172173

@@ -176,7 +177,7 @@ public function createForNamespace(string $namespace, string $fileContents) : Co
176177
break;
177178
case T_USE:
178179
if ($currentNamespace === $namespace) {
179-
$useStatements = array_merge($useStatements, $this->parseUseStatement($tokens));
180+
$useStatements += $this->parseUseStatement($tokens);
180181
}
181182

182183
break;
@@ -218,12 +219,13 @@ private function parseUseStatement(ArrayIterator $tokens) : array
218219
while (true) {
219220
$this->skipToNextStringOrNamespaceSeparator($tokens);
220221

221-
$uses = array_merge($uses, $this->extractUseStatements($tokens));
222-
if ($tokens->current()[0] === self::T_LITERAL_END_OF_USE) {
222+
$uses += $this->extractUseStatements($tokens);
223+
$currentToken = $tokens->current();
224+
if ($currentToken[0] === self::T_LITERAL_END_OF_USE) {
223225
return $uses;
224226
}
225227

226-
if ($tokens->current() === false) {
228+
if ($currentToken === false) {
227229
break;
228230
}
229231
}
@@ -236,7 +238,12 @@ private function parseUseStatement(ArrayIterator $tokens) : array
236238
*/
237239
private function skipToNextStringOrNamespaceSeparator(ArrayIterator $tokens) : void
238240
{
239-
while ($tokens->valid() && ($tokens->current()[0] !== T_STRING) && ($tokens->current()[0] !== T_NS_SEPARATOR)) {
241+
while ($tokens->valid()) {
242+
$currentToken = $tokens->current();
243+
if ($currentToken[0] === T_STRING || $currentToken[0] === T_NS_SEPARATOR) {
244+
break;
245+
}
246+
240247
$tokens->next();
241248
}
242249
}

0 commit comments

Comments
 (0)