Complex classes like AbstractFileDecorator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AbstractFileDecorator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class AbstractFileDecorator extends File |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The CodeSniffer file |
||
| 20 | * |
||
| 21 | * @var File |
||
| 22 | */ |
||
| 23 | private $baseFile; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * File constructor. |
||
| 27 | * |
||
| 28 | * @param File $baseFile CodeSniffer file |
||
| 29 | */ |
||
| 30 | public function __construct(File $baseFile) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Returns the wrapped classes method result. |
||
| 37 | * |
||
| 38 | * @param string $method |
||
| 39 | * @param array $args |
||
| 40 | * |
||
| 41 | * @return mixed |
||
| 42 | */ |
||
| 43 | public function __call(string $method, array $args = []) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Returns the property of the base file. |
||
| 50 | * |
||
| 51 | * @param string $property |
||
| 52 | * |
||
| 53 | * @return mixed |
||
| 54 | */ |
||
| 55 | public function __get(string $property) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Records an error against a specific token in the file. |
||
| 62 | * |
||
| 63 | * @param string $error The error message. |
||
| 64 | * @param int $stackPtr The stack position where the error occurred. |
||
| 65 | * @param string $code A violation code unique to the sniff message. |
||
| 66 | * @param array $data Replacements for the error message. |
||
| 67 | * @param int $severity The severity level for this error. A value of 0 |
||
| 68 | * will be converted into the default severity level. |
||
| 69 | * @param boolean $fixable Can the error be fixed by the sniff? |
||
| 70 | * |
||
| 71 | * @return boolean |
||
| 72 | */ |
||
| 73 | public function addError( |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Records an error against a specific line in the file. |
||
| 87 | * |
||
| 88 | * @param string $error The error message. |
||
| 89 | * @param int $line The line on which the error occurred. |
||
| 90 | * @param string $code A violation code unique to the sniff message. |
||
| 91 | * @param array $data Replacements for the error message. |
||
| 92 | * @param int $severity The severity level for this error. A value of 0 |
||
| 93 | * will be converted into the default severity level. |
||
| 94 | * |
||
| 95 | * @return boolean |
||
| 96 | */ |
||
| 97 | public function addErrorOnLine( |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Records a fixable error against a specific token in the file. |
||
| 110 | * |
||
| 111 | * Returns true if the error was recorded and should be fixed. |
||
| 112 | * |
||
| 113 | * @param string $error The error message. |
||
| 114 | * @param int $stackPtr The stack position where the error occurred. |
||
| 115 | * @param string $code A violation code unique to the sniff message. |
||
| 116 | * @param array $data Replacements for the error message. |
||
| 117 | * @param int $severity The severity level for this error. A value of 0 |
||
| 118 | * will be converted into the default severity level. |
||
| 119 | * |
||
| 120 | * @return boolean |
||
| 121 | */ |
||
| 122 | public function addFixableError( |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Records a fixable warning against a specific token in the file. |
||
| 135 | * |
||
| 136 | * Returns true if the warning was recorded and should be fixed. |
||
| 137 | * |
||
| 138 | * @param string $warning The error message. |
||
| 139 | * @param int $stackPtr The stack position where the error occurred. |
||
| 140 | * @param string $code A violation code unique to the sniff message. |
||
| 141 | * @param array $data Replacements for the warning message. |
||
| 142 | * @param int $severity The severity level for this warning. A value of 0 |
||
| 143 | * will be converted into the default severity level. |
||
| 144 | * |
||
| 145 | * @return boolean |
||
| 146 | */ |
||
| 147 | public function addFixableWarning( |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Adds an error to the error stack. |
||
| 160 | * |
||
| 161 | * @param boolean $error Is this an error message? |
||
| 162 | * @param string $message The text of the message. |
||
| 163 | * @param int $line The line on which the message occurred. |
||
| 164 | * @param int $column The column at which the message occurred. |
||
| 165 | * @param string $code A violation code unique to the sniff message. |
||
| 166 | * @param array $data Replacements for the message. |
||
| 167 | * @param int $severity The severity level for this message. A value of 0 |
||
| 168 | * will be converted into the default severity level. |
||
| 169 | * @param boolean $fixable Can the problem be fixed by the sniff? |
||
| 170 | * |
||
| 171 | * @return boolean |
||
| 172 | */ |
||
| 173 | protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Records a warning against a specific token in the file. |
||
| 180 | * |
||
| 181 | * @param string $warning The error message. |
||
| 182 | * @param int $stackPtr The stack position where the error occurred. |
||
| 183 | * @param string $code A violation code unique to the sniff message. |
||
| 184 | * @param array $data Replacements for the warning message. |
||
| 185 | * @param int $severity The severity level for this warning. A value of 0 |
||
| 186 | * will be converted into the default severity level. |
||
| 187 | * @param boolean $fixable Can the warning be fixed by the sniff? |
||
| 188 | * |
||
| 189 | * @return boolean |
||
| 190 | */ |
||
| 191 | public function addWarning( |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Records a warning against a specific token in the file. |
||
| 205 | * |
||
| 206 | * @param string $warning The error message. |
||
| 207 | * @param int $line The line on which the warning occurred. |
||
| 208 | * @param string $code A violation code unique to the sniff message. |
||
| 209 | * @param array $data Replacements for the warning message. |
||
| 210 | * @param int $severity The severity level for this warning. A value of 0 will |
||
| 211 | * will be converted into the default severity level. |
||
| 212 | * |
||
| 213 | * @return boolean |
||
| 214 | */ |
||
| 215 | public function addWarningOnLine( |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Remove vars stored in this file that are no longer required. |
||
| 228 | * |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | public function cleanUp() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Disables caching of this file. |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | public function disableCaching() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Returns the position of the last non-whitespace token in a statement. |
||
| 248 | * |
||
| 249 | * @param int $start The position to start searching from in the token stack. |
||
| 250 | * @param int|array $ignore Token types that should not be considered stop points. |
||
| 251 | * |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | public function findEndOfStatement($start, $ignore = null) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns the name of the class that the specified class extends. |
||
| 261 | * (works for classes, anonymous classes and interfaces) |
||
| 262 | * |
||
| 263 | * Returns FALSE on error or if there is no extended class name. |
||
| 264 | * |
||
| 265 | * @param int $stackPtr The stack position of the class. |
||
| 266 | * |
||
| 267 | * @return string|false |
||
| 268 | */ |
||
| 269 | public function findExtendedClassName($stackPtr) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Returns the position of the first token on a line, matching given type. |
||
| 276 | * |
||
| 277 | * Returns false if no token can be found. |
||
| 278 | * |
||
| 279 | * @param int|array $types The type(s) of tokens to search for. |
||
| 280 | * @param int $start The position to start searching from in the |
||
| 281 | * token stack. The first token matching on |
||
| 282 | * this line before this token will be returned. |
||
| 283 | * @param bool $exclude If true, find the token that is NOT of |
||
| 284 | * the types specified in $types. |
||
| 285 | * @param string $value The value that the token must be equal to. |
||
| 286 | * If value is omitted, tokens with any value will |
||
| 287 | * be returned. |
||
| 288 | * |
||
| 289 | * @return int | bool |
||
| 290 | */ |
||
| 291 | public function findFirstOnLine($types, $start, $exclude = false, $value = null) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns the names of the interfaces that the specified class implements. |
||
| 298 | * |
||
| 299 | * Returns FALSE on error or if there are no implemented interface names. |
||
| 300 | * |
||
| 301 | * @param int $stackPtr The stack position of the class. |
||
| 302 | * |
||
| 303 | * @return array|false |
||
| 304 | */ |
||
| 305 | public function findImplementedInterfaceNames($stackPtr) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns the position of the next specified token(s). |
||
| 312 | * |
||
| 313 | * If a value is specified, the next token of the specified type(s) |
||
| 314 | * containing the specified value will be returned. |
||
| 315 | * |
||
| 316 | * Returns false if no token can be found. |
||
| 317 | * |
||
| 318 | * @param int|array $types The type(s) of tokens to search for. |
||
| 319 | * @param int $start The position to start searching from in the |
||
| 320 | * token stack. |
||
| 321 | * @param int $end The end position to fail if no token is found. |
||
| 322 | * if not specified or null, end will default to |
||
| 323 | * the end of the token stack. |
||
| 324 | * @param bool $exclude If true, find the next token that is NOT of |
||
| 325 | * a type specified in $types. |
||
| 326 | * @param string $value The value that the token(s) must be equal to. |
||
| 327 | * If value is omitted, tokens with any value will |
||
| 328 | * be returned. |
||
| 329 | * @param bool $local If true, tokens outside the current statement |
||
| 330 | * will not be checked. i.e., checking will stop |
||
| 331 | * at the next semi-colon found. |
||
| 332 | * |
||
| 333 | * @return int|bool |
||
| 334 | * @see findPrevious() |
||
| 335 | */ |
||
| 336 | public function findNext( |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Returns the position of the previous specified token(s). |
||
| 350 | * |
||
| 351 | * If a value is specified, the previous token of the specified type(s) |
||
| 352 | * containing the specified value will be returned. |
||
| 353 | * |
||
| 354 | * Returns false if no token can be found. |
||
| 355 | * |
||
| 356 | * @param int|array $types The type(s) of tokens to search for. |
||
| 357 | * @param int $start The position to start searching from in the |
||
| 358 | * token stack. |
||
| 359 | * @param int $end The end position to fail if no token is found. |
||
| 360 | * if not specified or null, end will default to |
||
| 361 | * the start of the token stack. |
||
| 362 | * @param bool $exclude If true, find the previous token that is NOT of |
||
| 363 | * the types specified in $types. |
||
| 364 | * @param string $value The value that the token(s) must be equal to. |
||
| 365 | * If value is omitted, tokens with any value will |
||
| 366 | * be returned. |
||
| 367 | * @param bool $local If true, tokens outside the current statement |
||
| 368 | * will not be checked. IE. checking will stop |
||
| 369 | * at the previous semi-colon found. |
||
| 370 | * |
||
| 371 | * @return int|bool |
||
| 372 | * @see findNext() |
||
| 373 | */ |
||
| 374 | public function findPrevious( |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Returns the position of the first non-whitespace token in a statement. |
||
| 388 | * |
||
| 389 | * @param int $start The position to start searching from in the token stack. |
||
| 390 | * @param int|array $ignore Token types that should not be considered stop points. |
||
| 391 | * |
||
| 392 | * @return int |
||
| 393 | */ |
||
| 394 | public function findStartOfStatement($start, $ignore = null) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Returns the original file for this decorator |
||
| 401 | * |
||
| 402 | * @return File The original file wrapper. |
||
| 403 | */ |
||
| 404 | public function getBaseFile(): File |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Returns the visibility and implementation properties of a class. |
||
| 411 | * |
||
| 412 | * The format of the array is: |
||
| 413 | * <code> |
||
| 414 | * array( |
||
| 415 | * 'is_abstract' => false, // true if the abstract keyword was found. |
||
| 416 | * 'is_final' => false, // true if the final keyword was found. |
||
| 417 | * ); |
||
| 418 | * </code> |
||
| 419 | * |
||
| 420 | * @param int $stackPtr The position in the stack of the T_CLASS token to |
||
| 421 | * acquire the properties for. |
||
| 422 | * |
||
| 423 | * @return array |
||
| 424 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
||
| 425 | * T_CLASS token. |
||
| 426 | */ |
||
| 427 | public function getClassProperties($stackPtr) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Return the position of the condition for the passed token. |
||
| 434 | * |
||
| 435 | * Returns FALSE if the token does not have the condition. |
||
| 436 | * |
||
| 437 | * @param int $stackPtr The position of the token we are checking. |
||
| 438 | * @param int $type The type of token to search for. |
||
| 439 | * |
||
| 440 | * @return int |
||
| 441 | */ |
||
| 442 | public function getCondition($stackPtr, $type) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Returns the declaration names for classes, interfaces, traits, and functions. |
||
| 449 | * |
||
| 450 | * @param int $stackPtr The position of the declaration token which |
||
| 451 | * declared the class, interface, trait, or function. |
||
| 452 | * |
||
| 453 | * @return string|null The name of the class, interface, trait, or function; |
||
| 454 | * or NULL if the function or class is anonymous. |
||
| 455 | * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type |
||
| 456 | * T_FUNCTION, T_CLASS, T_ANON_CLASS, |
||
| 457 | * T_CLOSURE, T_TRAIT, or T_INTERFACE. |
||
| 458 | */ |
||
| 459 | public function getDeclarationName($stackPtr) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Returns the number of errors raised. |
||
| 466 | * |
||
| 467 | * @return int |
||
| 468 | */ |
||
| 469 | public function getErrorCount() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Returns the errors raised from processing this file. |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public function getErrors() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Returns the absolute filename of this file. |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function getFilename() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Returns the number of fixable errors/warnings raised. |
||
| 496 | * |
||
| 497 | * @return int |
||
| 498 | */ |
||
| 499 | public function getFixableCount() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Returns the number of fixed errors/warnings. |
||
| 506 | * |
||
| 507 | * @return int |
||
| 508 | */ |
||
| 509 | public function getFixedCount() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Returns the list of ignored lines. |
||
| 516 | * |
||
| 517 | * @return array |
||
| 518 | */ |
||
| 519 | public function getIgnoredLines() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Returns the visibility and implementation properties of the class member |
||
| 526 | * variable found at the specified position in the stack. |
||
| 527 | * |
||
| 528 | * The format of the array is: |
||
| 529 | * |
||
| 530 | * <code> |
||
| 531 | * array( |
||
| 532 | * 'scope' => 'public', // public protected or protected. |
||
| 533 | * 'scope_specified' => false, // true if the scope was explicitely specified. |
||
| 534 | * 'is_static' => false, // true if the static keyword was found. |
||
| 535 | * ); |
||
| 536 | * </code> |
||
| 537 | * |
||
| 538 | * @param int $stackPtr The position in the stack of the T_VARIABLE token to |
||
| 539 | * acquire the properties for. |
||
| 540 | * |
||
| 541 | * @return array |
||
| 542 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
||
| 543 | * T_VARIABLE token, or if the position is not |
||
| 544 | * a class member variable. |
||
| 545 | */ |
||
| 546 | public function getMemberProperties($stackPtr) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Returns the method parameters for the specified function token. |
||
| 553 | * |
||
| 554 | * Each parameter is in the following format: |
||
| 555 | * |
||
| 556 | * <code> |
||
| 557 | * 0 => array( |
||
| 558 | * 'name' => '$var', // The variable name. |
||
| 559 | * 'token' => integer, // The stack pointer to the variable name. |
||
| 560 | * 'content' => string, // The full content of the variable definition. |
||
| 561 | * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
||
| 562 | * 'variable_length' => boolean, // Is the param of variable length through use of `...` ? |
||
| 563 | * 'type_hint' => string, // The type hint for the variable. |
||
| 564 | * 'type_hint_token' => integer, // The stack pointer to the type hint |
||
| 565 | * // or false if there is no type hint. |
||
| 566 | * 'nullable_type' => boolean, // Is the variable using a nullable type? |
||
| 567 | * ) |
||
| 568 | * </code> |
||
| 569 | * |
||
| 570 | * Parameters with default values have an additional array index of |
||
| 571 | * 'default' with the value of the default as a string. |
||
| 572 | * |
||
| 573 | * @param int $stackPtr The position in the stack of the function token |
||
| 574 | * to acquire the parameters for. |
||
| 575 | * |
||
| 576 | * @return array |
||
| 577 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified $stackPtr is not of |
||
| 578 | * type T_FUNCTION or T_CLOSURE. |
||
| 579 | */ |
||
| 580 | public function getMethodParameters($stackPtr) |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * Returns the visibility and implementation properties of a method. |
||
| 588 | * |
||
| 589 | * The format of the array is: |
||
| 590 | * <code> |
||
| 591 | * array( |
||
| 592 | * 'scope' => 'public', // public protected or protected |
||
| 593 | * 'scope_specified' => true, // true is scope keyword was found. |
||
| 594 | * 'return_type' => '', // the return type of the method. |
||
| 595 | * 'return_type_token' => integer, // The stack pointer to the start of the return type |
||
| 596 | * // or false if there is no return type. |
||
| 597 | * 'nullable_return_type' => false, // true if the return type is nullable. |
||
| 598 | * 'is_abstract' => false, // true if the abstract keyword was found. |
||
| 599 | * 'is_final' => false, // true if the final keyword was found. |
||
| 600 | * 'is_static' => false, // true if the static keyword was found. |
||
| 601 | * ); |
||
| 602 | * </code> |
||
| 603 | * |
||
| 604 | * @param int $stackPtr The position in the stack of the function token to |
||
| 605 | * acquire the properties for. |
||
| 606 | * |
||
| 607 | * @return array |
||
| 608 | * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a |
||
| 609 | * T_FUNCTION token. |
||
| 610 | */ |
||
| 611 | public function getMethodProperties($stackPtr) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Returns the metrics found while processing this file. |
||
| 618 | * |
||
| 619 | * @return array |
||
| 620 | */ |
||
| 621 | public function getMetrics() |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Returns the number of successes recorded. |
||
| 628 | * |
||
| 629 | * @return int |
||
| 630 | */ |
||
| 631 | public function getSuccessCount() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Returns the token stack for this file. |
||
| 638 | * |
||
| 639 | * @return array |
||
| 640 | */ |
||
| 641 | public function getTokens() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Returns the content of the tokens from the specified start position in |
||
| 648 | * the token stack for the specified length. |
||
| 649 | * |
||
| 650 | * @param int $start The position to start from in the token stack. |
||
| 651 | * @param int $length The length of tokens to traverse from the start pos. |
||
| 652 | * @param int $origContent Whether the original content or the tab replaced |
||
| 653 | * content should be used. |
||
| 654 | * |
||
| 655 | * @return string The token contents. |
||
| 656 | */ |
||
| 657 | public function getTokensAsString($start, $length, $origContent = false) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Returns the number of warnings raised. |
||
| 664 | * |
||
| 665 | * @return int |
||
| 666 | */ |
||
| 667 | public function getWarningCount() |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Returns the warnings raised from processing this file. |
||
| 674 | * |
||
| 675 | * @return array |
||
| 676 | */ |
||
| 677 | public function getWarnings() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Determine if the passed token has a condition of one of the passed types. |
||
| 684 | * |
||
| 685 | * @param int $stackPtr The position of the token we are checking. |
||
| 686 | * @param int|array $types The type(s) of tokens to search for. |
||
| 687 | * |
||
| 688 | * @return boolean |
||
| 689 | */ |
||
| 690 | public function hasCondition($stackPtr, $types) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Determine if the passed token is a reference operator. |
||
| 697 | * |
||
| 698 | * Returns true if the specified token position represents a reference. |
||
| 699 | * Returns false if the token represents a bitwise operator. |
||
| 700 | * |
||
| 701 | * @param int $stackPtr The position of the T_BITWISE_AND token. |
||
| 702 | * |
||
| 703 | * @return boolean |
||
| 704 | */ |
||
| 705 | public function isReference($stackPtr) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Tokenizes the file and prepares it for the test run. |
||
| 712 | * |
||
| 713 | * @return void |
||
| 714 | */ |
||
| 715 | public function parse() |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Starts the stack traversal and tells listeners when tokens are found. |
||
| 722 | * |
||
| 723 | * @return void |
||
| 724 | */ |
||
| 725 | public function process() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Record a metric about the file being examined. |
||
| 732 | * |
||
| 733 | * @param int $stackPtr The stack position where the metric was recorded. |
||
| 734 | * @param string $metric The name of the metric being recorded. |
||
| 735 | * @param string $value The value of the metric being recorded. |
||
| 736 | * |
||
| 737 | * @return boolean |
||
| 738 | */ |
||
| 739 | public function recordMetric($stackPtr, $metric, $value) |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Reloads the content of the file. |
||
| 746 | * |
||
| 747 | * By default, we have no idea where our content comes from, so we can't do anything. |
||
| 748 | * |
||
| 749 | * @return void |
||
| 750 | */ |
||
| 751 | public function reloadContent() |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Set the content of the file. |
||
| 758 | * |
||
| 759 | * Setting the content also calculates the EOL char being used. |
||
| 760 | * |
||
| 761 | * @param string $content The file content. |
||
| 762 | * |
||
| 763 | * @return void |
||
| 764 | */ |
||
| 765 | public function setContent($content) |
||
| 769 | } |
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: