| Total Complexity | 57 |
| Total Lines | 556 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Tokenizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Tokenizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | final class Tokenizer |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * The namespace aliases. |
||
| 47 | */ |
||
| 48 | private $aliases = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int[] The list of valid class identifiers |
||
| 52 | */ |
||
| 53 | private static $classIdentifiers = [ |
||
| 54 | DocLexer::T_IDENTIFIER, |
||
| 55 | DocLexer::T_TRUE, |
||
| 56 | DocLexer::T_FALSE, |
||
| 57 | DocLexer::T_NULL, |
||
| 58 | ]; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The list of ignored annotation identifiers. |
||
| 62 | */ |
||
| 63 | private $ignored = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Parses the docblock and returns its annotation tokens. |
||
| 67 | * |
||
| 68 | * @param string $docblock |
||
| 69 | * @param array<string, string> $annotationAliases |
||
| 70 | * |
||
| 71 | * Annotation aliases are for when the annotations are imported and aliased via a use statement, for example: |
||
| 72 | * |
||
| 73 | * ```php |
||
| 74 | * use Doctrine\ORM\Mapping as ORM; |
||
| 75 | * ``` |
||
| 76 | * |
||
| 77 | * Would require the following alias configuration: |
||
| 78 | * |
||
| 79 | * ```php |
||
| 80 | * $annotationAliases = [ |
||
| 81 | * 'ORM' => 'Doctrine\ORM\Mapping', |
||
| 82 | * ]; |
||
| 83 | * ``` |
||
| 84 | * |
||
| 85 | * @return array the list of tokens |
||
| 86 | */ |
||
| 87 | public function parse(string $docblock, array $annotationAliases = []): TreeNode |
||
| 88 | { |
||
| 89 | Assertion::allString($annotationAliases); |
||
| 90 | Assertion::allString(array_keys($annotationAliases)); |
||
| 91 | |||
| 92 | if (0 !== strpos(ltrim($docblock), '/**')) { |
||
| 93 | return []; |
||
| 94 | } |
||
| 95 | |||
| 96 | $dumper = new Dump(); |
||
| 97 | $compiler = Llk::load(new Read(__DIR__ . '/../../res/annotation-grammar.pp')); |
||
| 98 | return $compiler->parse($docblock); |
||
| 99 | |||
| 100 | $nodes = $this->parseDocblock($docblock); |
||
| 101 | |||
| 102 | if (false === ($position = strpos($docblock, '@'))) { |
||
| 103 | return []; |
||
| 104 | } |
||
| 105 | |||
| 106 | if (0 < $position) { |
||
| 107 | --$position; |
||
| 108 | } |
||
| 109 | |||
| 110 | $docblock = substr($docblock, $position); |
||
| 111 | $docblock = trim($docblock, '*/ '); |
||
| 112 | |||
| 113 | $lexer = $this->createLexer($docblock); |
||
| 114 | |||
| 115 | return $this->retrieveAnnotations($lexer, $annotationAliases); |
||
| 116 | } |
||
| 117 | |||
| 118 | private function parseDocblock(string $docblock): TreeNode |
||
| 119 | { |
||
| 120 | $dumper = new Dump(); |
||
| 121 | $compiler = Llk::load(new Read(__DIR__ . '/../../res/annotation-grammar.pp')); |
||
| 122 | $ast = $compiler->parse($docblock); |
||
| 123 | |||
| 124 | return $ast; |
||
| 125 | } |
||
| 126 | |||
| 127 | private function createLexer(string $docblock): DocLexer |
||
| 128 | { |
||
| 129 | $lexer = new DocLexer(); |
||
| 130 | |||
| 131 | $lexer->setInput($docblock); |
||
| 132 | |||
| 133 | // Start at the first @ symbol |
||
| 134 | while (null === $lexer->token || DocLexer::T_AT !== $lexer->token['type']) { |
||
| 135 | $lexer->moveNext(); |
||
| 136 | } |
||
| 137 | |||
| 138 | return $lexer; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Sets the annotation identifiers to ignore. |
||
| 143 | * |
||
| 144 | * @param array $ignore the list of ignored identifiers |
||
| 145 | */ |
||
| 146 | public function ignore(array $ignore): void |
||
| 147 | { |
||
| 148 | $this->ignored = $ignore; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the tokens for the next annotation. |
||
| 153 | * |
||
| 154 | * @return array the tokens |
||
| 155 | */ |
||
| 156 | private function retrieveAnnotation(DocLexer $lexer): ?array |
||
| 157 | { |
||
| 158 | // Get the complete name |
||
| 159 | $identifier = $this->retrieveIdentifier($lexer); |
||
| 160 | |||
| 161 | // Skip if necessary |
||
| 162 | if (in_array($identifier, $this->ignored, true)) { |
||
| 163 | return null; |
||
| 164 | } |
||
| 165 | |||
| 166 | // use alias if applicable |
||
| 167 | if (false !== ($pos = strpos($identifier, '\\'))) { |
||
| 168 | $alias = substr($identifier, 0, $pos); |
||
| 169 | |||
| 170 | if (isset($this->aliases[$alias])) { |
||
| 171 | $identifier = $this->aliases[$alias] |
||
| 172 | .'\\' |
||
| 173 | .substr($identifier, $pos + 1); |
||
| 174 | } |
||
| 175 | } elseif (isset($this->aliases[$identifier])) { |
||
| 176 | $identifier = $this->aliases[$identifier]; |
||
| 177 | } |
||
| 178 | |||
| 179 | // return the @, name, and any values found |
||
| 180 | return array_merge( |
||
| 181 | [ |
||
| 182 | [DocLexer::T_AT], |
||
| 183 | [DocLexer::T_IDENTIFIER, $identifier], |
||
| 184 | ], |
||
| 185 | $this->retrieveAnnotationValues($lexer) |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | |||
| 189 | private function retrieveAnnotations(DocLexer $lexer, array $annotationAliases): array |
||
| 190 | { |
||
| 191 | $tokens = []; |
||
| 192 | |||
| 193 | while (true) { |
||
| 194 | /** |
||
| 195 | * @var string $value |
||
| 196 | * @var int $type |
||
| 197 | * @var int $position |
||
| 198 | */ |
||
| 199 | [$value, $type, $position] = array_values($lexer->token); |
||
| 200 | |||
| 201 | // something about being preceded by a non-catchable pattern |
||
| 202 | // TODO: what is that comment about? |
||
| 203 | $position = $lexer->token['position'] + strlen($lexer->token['value']); |
||
| 204 | |||
| 205 | // if ((null !== $this->lexer->token) |
||
| 206 | // && ($this->lexer->lookahead['position'] === $position)) { |
||
| 207 | // $this->lexer->moveNext(); |
||
| 208 | // |
||
| 209 | // continue; |
||
| 210 | // } |
||
| 211 | |||
| 212 | // // make sure we get a valid annotation name |
||
| 213 | // if ((null === ($glimpse = $this->lexer->glimpse())) |
||
| 214 | // || ((DocLexer::T_NAMESPACE_SEPARATOR !== $glimpse['type']) |
||
| 215 | // && !in_array($glimpse['type'], self::$classIdentifiers, true))) { |
||
| 216 | // $this->lexer->moveNext(); |
||
| 217 | // |
||
| 218 | // continue; |
||
| 219 | // } |
||
| 220 | |||
| 221 | // find them all and merge them to the list |
||
| 222 | if (null !== ($annotationToken = $this->retrieveAnnotation($lexer))) { |
||
| 223 | $tokens = array_merge($tokens, $annotationToken); |
||
| 224 | } |
||
| 225 | |||
| 226 | if (null === $lexer->lookahead) { |
||
| 227 | break; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | return $tokens; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the tokens for the next array of values. |
||
| 236 | * |
||
| 237 | * @return array the tokens |
||
| 238 | */ |
||
| 239 | private function getArray(DocLexer $lexer): array |
||
| 240 | { |
||
| 241 | $this->match($lexer, DocLexer::T_OPEN_CURLY_BRACES); |
||
| 242 | |||
| 243 | $tokens = [ |
||
| 244 | [DocLexer::T_OPEN_CURLY_BRACES], |
||
| 245 | ]; |
||
| 246 | |||
| 247 | // check if empty array, bail early if it is |
||
| 248 | if ($lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) { |
||
| 249 | $this->match($lexer, DocLexer::T_CLOSE_CURLY_BRACES); |
||
| 250 | |||
| 251 | $tokens[] = [DocLexer::T_CLOSE_CURLY_BRACES]; |
||
| 252 | |||
| 253 | return $tokens; |
||
| 254 | } |
||
| 255 | |||
| 256 | // collect the first value |
||
| 257 | $tokens = array_merge($tokens, $this->getArrayEntry($lexer)); |
||
| 258 | |||
| 259 | // collect the remaining values |
||
| 260 | while ($lexer->isNextToken(DocLexer::T_COMMA)) { |
||
| 261 | $this->match($lexer, DocLexer::T_COMMA); |
||
| 262 | |||
| 263 | $tokens[] = [DocLexer::T_COMMA]; |
||
| 264 | |||
| 265 | if ($lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) { |
||
| 266 | break; |
||
| 267 | } |
||
| 268 | |||
| 269 | $tokens = array_merge($tokens, $this->getArrayEntry($lexer)); |
||
| 270 | } |
||
| 271 | |||
| 272 | // end the collection |
||
| 273 | $this->match($lexer, DocLexer::T_CLOSE_CURLY_BRACES); |
||
| 274 | |||
| 275 | $tokens[] = [DocLexer::T_CLOSE_CURLY_BRACES]; |
||
| 276 | |||
| 277 | return $tokens; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns the tokens for the next array entry. |
||
| 282 | * |
||
| 283 | * @return array the tokens |
||
| 284 | */ |
||
| 285 | private function getArrayEntry(DocLexer $lexer): array |
||
| 286 | { |
||
| 287 | $glimpse = $lexer->glimpse(); |
||
| 288 | $tokens = []; |
||
| 289 | |||
| 290 | // append the correct assignment token: ":" or "=" |
||
| 291 | if (DocLexer::T_COLON === $glimpse['type']) { |
||
| 292 | $token = [DocLexer::T_COLON]; |
||
| 293 | } elseif (DocLexer::T_EQUALS === $glimpse['type']) { |
||
| 294 | $token = [DocLexer::T_EQUALS]; |
||
| 295 | } |
||
| 296 | |||
| 297 | // is it an assignment? |
||
| 298 | if (isset($token)) { |
||
| 299 | // if the key is a constant, hand off |
||
| 300 | if ($lexer->isNextToken(DocLexer::T_IDENTIFIER)) { |
||
| 301 | $tokens = $this->getConstant($lexer); |
||
| 302 | |||
| 303 | // match only integer and string keys |
||
| 304 | } else { |
||
| 305 | $this->matchAny( |
||
| 306 | $lexer, |
||
| 307 | [ |
||
| 308 | DocLexer::T_INTEGER, |
||
| 309 | DocLexer::T_STRING, |
||
| 310 | ] |
||
| 311 | ); |
||
| 312 | |||
| 313 | $tokens = [ |
||
| 314 | [ |
||
| 315 | $lexer->token['type'], |
||
| 316 | $lexer->token['value'], |
||
| 317 | ], |
||
| 318 | ]; |
||
| 319 | } |
||
| 320 | |||
| 321 | $tokens[] = $token; |
||
| 322 | |||
| 323 | $this->matchAny( |
||
| 324 | $lexer, |
||
| 325 | [ |
||
| 326 | DocLexer::T_COLON, |
||
| 327 | DocLexer::T_EQUALS, |
||
| 328 | ] |
||
| 329 | ); |
||
| 330 | } |
||
| 331 | |||
| 332 | // merge in the value |
||
| 333 | return array_merge($tokens, $this->getPlainValue($lexer)); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Returns the tokens for the next assigned (key/value) value. |
||
| 338 | * |
||
| 339 | * @return array the tokens |
||
| 340 | */ |
||
| 341 | private function getAssignedValue(DocLexer $lexer): array |
||
| 342 | { |
||
| 343 | $this->match($lexer, DocLexer::T_IDENTIFIER); |
||
| 344 | |||
| 345 | $tokens = [ |
||
| 346 | [DocLexer::T_IDENTIFIER, $lexer->token['value']], |
||
| 347 | [DocLexer::T_EQUALS], |
||
| 348 | ]; |
||
| 349 | |||
| 350 | $this->match($lexer, DocLexer::T_EQUALS); |
||
| 351 | |||
| 352 | return array_merge($tokens, $this->getPlainValue($lexer)); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns the current constant value for the current annotation. |
||
| 357 | * |
||
| 358 | * @return array the tokens |
||
| 359 | */ |
||
| 360 | private function getConstant(DocLexer $lexer): array |
||
| 361 | { |
||
| 362 | $identifier = $this->retrieveIdentifier($lexer); |
||
| 363 | $tokens = []; |
||
| 364 | |||
| 365 | // check for a special constant type |
||
| 366 | switch (strtolower($identifier)) { |
||
| 367 | case 'true': |
||
| 368 | $tokens[] = [DocLexer::T_TRUE, $identifier]; |
||
| 369 | break; |
||
| 370 | case 'false': |
||
| 371 | $tokens[] = [DocLexer::T_FALSE, $identifier]; |
||
| 372 | break; |
||
| 373 | case 'null': |
||
| 374 | $tokens[] = [DocLexer::T_NULL, $identifier]; |
||
| 375 | break; |
||
| 376 | default: |
||
| 377 | $tokens[] = [DocLexer::T_IDENTIFIER, $identifier]; |
||
| 378 | } |
||
| 379 | |||
| 380 | return $tokens; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Returns the next identifier. |
||
| 385 | * |
||
| 386 | * @throws Exception |
||
| 387 | * @throws SyntaxException if a syntax error is found |
||
| 388 | * |
||
| 389 | * @return string the identifier |
||
| 390 | */ |
||
| 391 | private function retrieveIdentifier(DocLexer $lexer): string |
||
| 392 | { |
||
| 393 | // grab the first bit of the identifier |
||
| 394 | if ($lexer->isNextTokenAny(self::$classIdentifiers)) { |
||
| 395 | $lexer->moveNext(); |
||
| 396 | |||
| 397 | $name = $lexer->token['value']; |
||
| 398 | } else { |
||
| 399 | throw SyntaxException::expectedToken( |
||
| 400 | 'namespace separator or identifier', |
||
| 401 | null, |
||
| 402 | $lexer |
||
| 403 | ); |
||
| 404 | } |
||
| 405 | |||
| 406 | // grab the remaining bits |
||
| 407 | $position = $lexer->token['position'] |
||
| 408 | + strlen($lexer->token['value']); |
||
| 409 | |||
| 410 | while (($lexer->lookahead['position'] === $position) |
||
| 411 | && $lexer->isNextToken(DocLexer::T_NAMESPACE_SEPARATOR)) { |
||
| 412 | $this->match($lexer, DocLexer::T_NAMESPACE_SEPARATOR); |
||
| 413 | $this->matchAny(self::$classIdentifiers); |
||
| 414 | |||
| 415 | $name .= '\\'.$lexer->token['value']; |
||
| 416 | } |
||
| 417 | |||
| 418 | return $name; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Returns the tokens for the next "plain" value. |
||
| 423 | * |
||
| 424 | * @throws Exception |
||
| 425 | * @throws SyntaxException if a syntax error is found |
||
| 426 | * |
||
| 427 | * @return array the tokens |
||
| 428 | */ |
||
| 429 | private function getPlainValue(DocLexer $lexer): array |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Returns the tokens for the next value. |
||
| 477 | * |
||
| 478 | * @return array the tokens |
||
| 479 | */ |
||
| 480 | private function getValue(DocLexer $lexer): array |
||
| 481 | { |
||
| 482 | $glimpse = $lexer->glimpse(); |
||
| 483 | |||
| 484 | // check if it's an assigned value: @example(assigned="value") |
||
| 485 | if (DocLexer::T_EQUALS === $glimpse['type']) { |
||
| 486 | return $this->getAssignedValue($lexer); |
||
| 487 | } |
||
| 488 | |||
| 489 | return $this->getPlainValue($lexer); |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Returns the tokens for all of the values for the current annotation. |
||
| 494 | * |
||
| 495 | * @throws Exception |
||
| 496 | * @throws SyntaxException if a syntax error is found |
||
| 497 | * |
||
| 498 | * @return array the tokens |
||
| 499 | */ |
||
| 500 | private function retrieveAnnotationValues(DocLexer $lexer): array |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Matches the next token and advances. |
||
| 554 | * |
||
| 555 | * @param int $token the next token to match |
||
| 556 | * |
||
| 557 | * @throws Exception |
||
| 558 | * @throws SyntaxException if a syntax error is found |
||
| 559 | * |
||
| 560 | * @return null|array TRUE if the next token matches, FALSE if not |
||
| 561 | */ |
||
| 562 | private function match(DocLexer $lexer, $token): bool |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Matches any one of the tokens and advances. |
||
| 577 | * |
||
| 578 | * @param array $tokens the list of tokens |
||
| 579 | * |
||
| 580 | * @throws Exception |
||
| 581 | * @throws SyntaxException if a syntax error is found |
||
| 582 | * |
||
| 583 | * @return bool TRUE if the next token matches, FALSE if not |
||
| 584 | */ |
||
| 585 | private function matchAny(DocLexer $lexer, array $tokens): bool |
||
| 601 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: