Skip to content

Commit 6331a85

Browse files
committed
Improve code style for php8 types
To reduce the number of checks we define the php 8 defined constants and expect them to be available in the rest of the code.
1 parent f598aec commit 6331a85

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

composer-require-config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"symbol-whitelist" : [
33
"null", "true", "false",
44
"static", "self", "parent",
5-
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "object", "XSLTProcessor"
5+
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "object", "XSLTProcessor",
6+
"T_NAME_QUALIFIED", "T_NAME_FULLY_QUALIFIED"
67
],
78
"php-core-extensions" : [
89
"Core",

src/Types/ContextFactory.php

+24-10
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@
2323
use Reflector;
2424
use RuntimeException;
2525
use UnexpectedValueException;
26+
use function define;
27+
use function defined;
2628
use function file_exists;
2729
use function file_get_contents;
2830
use function get_class;
2931
use function in_array;
3032
use function is_string;
33+
use function strrpos;
34+
use function substr;
3135
use function token_get_all;
3236
use function trim;
3337
use const T_AS;
@@ -39,6 +43,14 @@
3943
use const T_STRING;
4044
use const T_USE;
4145

46+
if (!defined('T_NAME_QUALIFIED')) {
47+
define('T_NAME_QUALIFIED', 'T_NAME_QUALIFIED');
48+
}
49+
50+
if (!defined('T_NAME_FULLY_QUALIFIED')) {
51+
define('T_NAME_FULLY_QUALIFIED', 'T_NAME_FULLY_QUALIFIED');
52+
}
53+
4254
/**
4355
* Convenience class to create a Context for DocBlocks when not using the Reflection Component of phpDocumentor.
4456
*
@@ -221,7 +233,8 @@ private function parseNamespace(ArrayIterator $tokens) : string
221233
$this->skipToNextStringOrNamespaceSeparator($tokens);
222234

223235
$name = '';
224-
while ($tokens->valid() && (in_array($tokens->current()[0], [T_STRING, T_NS_SEPARATOR], true) || defined('T_NAME_QUALIFIED') && T_NAME_QUALIFIED === $tokens->current()[0])) {
236+
$acceptedTokens = [T_STRING, T_NS_SEPARATOR, T_NAME_QUALIFIED];
237+
while ($tokens->valid() && in_array($tokens->current()[0], $acceptedTokens, true)) {
225238
$name .= $tokens->current()[1];
226239
$tokens->next();
227240
}
@@ -268,11 +281,11 @@ private function skipToNextStringOrNamespaceSeparator(ArrayIterator $tokens) : v
268281
break;
269282
}
270283

271-
if (defined('T_NAME_QUALIFIED') && T_NAME_QUALIFIED === $currentToken[0]) {
284+
if ($currentToken[0] === T_NAME_QUALIFIED) {
272285
break;
273286
}
274287

275-
if (defined('T_NAME_FULLY_QUALIFIED') && T_NAME_FULLY_QUALIFIED === $currentToken[0]) {
288+
if (defined('T_NAME_FULLY_QUALIFIED') && $currentToken[0] === T_NAME_FULLY_QUALIFIED) {
276289
break;
277290
}
278291

@@ -312,6 +325,14 @@ private function extractUseStatements(ArrayIterator $tokens) : array
312325
$currentNs .= (string) $tokenValue;
313326
$currentAlias = $tokenValue;
314327
break;
328+
case T_NAME_QUALIFIED:
329+
case T_NAME_FULLY_QUALIFIED:
330+
$currentNs .= (string) $tokenValue;
331+
$currentAlias = substr(
332+
(string) $tokenValue,
333+
(int) (strrpos((string) $tokenValue, '\\')) + 1
334+
);
335+
break;
315336
case T_CURLY_OPEN:
316337
case '{':
317338
$state = 'grouped';
@@ -325,13 +346,6 @@ private function extractUseStatements(ArrayIterator $tokens) : array
325346
$state = 'end';
326347
break;
327348
default:
328-
if (defined('T_NAME_QUALIFIED') && T_NAME_QUALIFIED === $tokenId) {
329-
$currentNs .= (string)$tokenValue;
330-
$currentAlias = substr($tokenValue, strrpos($tokenValue, '\\') + 1);
331-
} elseif (defined('T_NAME_FULLY_QUALIFIED') && T_NAME_FULLY_QUALIFIED === $tokenId) {
332-
$currentNs .= (string) $tokenValue;
333-
$currentAlias = substr($tokenValue, strrpos($tokenValue, '\\') + 1);
334-
}
335349
break;
336350
}
337351

0 commit comments

Comments
 (0)