Complex classes like FileAnalyzer 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 FileAnalyzer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class FileAnalyzer extends SourceAnalyzer implements StatementsSource |
||
| 23 | { |
||
| 24 | use CanAlias; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $file_name; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $file_path; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string|null |
||
| 38 | */ |
||
| 39 | protected $root_file_path; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string|null |
||
| 43 | */ |
||
| 44 | protected $root_file_name; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array<string, bool> |
||
| 48 | */ |
||
| 49 | private $required_file_paths = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array<string, bool> |
||
| 53 | */ |
||
| 54 | private $parent_file_paths = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array<string> |
||
| 58 | */ |
||
| 59 | private $suppressed_issues = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array<string, array<string, string>> |
||
| 63 | */ |
||
| 64 | private $namespace_aliased_classes = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array<string, array<string, string>> |
||
| 68 | */ |
||
| 69 | private $namespace_aliased_classes_flipped = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array<string, array<string, string>> |
||
| 73 | */ |
||
| 74 | private $namespace_aliased_classes_flipped_replaceable = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array<string, InterfaceAnalyzer> |
||
| 78 | */ |
||
| 79 | public $interface_analyzers_to_analyze = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array<lowercase-string, ClassAnalyzer> |
||
| 83 | */ |
||
| 84 | public $class_analyzers_to_analyze = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var null|Context |
||
| 88 | */ |
||
| 89 | public $context; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var ProjectAnalyzer |
||
| 93 | */ |
||
| 94 | public $project_analyzer; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var Codebase |
||
| 98 | */ |
||
| 99 | public $codebase; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | private $first_statement_offset = -1; |
||
| 105 | |||
| 106 | /** @var ?\Psalm\Internal\Provider\NodeDataProvider */ |
||
| 107 | private $node_data; |
||
| 108 | |||
| 109 | /** @var ?Type\Union */ |
||
| 110 | private $return_type; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $file_path |
||
| 114 | * @param string $file_name |
||
| 115 | */ |
||
| 116 | public function __construct(ProjectAnalyzer $project_analyzer, $file_path, $file_name) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param bool $preserve_analyzers |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public function analyze( |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param array<int, PhpParser\Node\Stmt> $stmts |
||
| 245 | * |
||
| 246 | * @return array<int, PhpParser\Node\Stmt> |
||
|
|
|||
| 247 | */ |
||
| 248 | public function populateCheckers(array $stmts) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return void |
||
| 288 | */ |
||
| 289 | private function populateClassLikeAnalyzers(PhpParser\Node\Stmt\ClassLike $stmt) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param string $fq_class_name |
||
| 317 | * @param ClassAnalyzer $class_analyzer |
||
| 318 | * |
||
| 319 | * @return void |
||
| 320 | */ |
||
| 321 | public function addNamespacedClassAnalyzer($fq_class_name, ClassAnalyzer $class_analyzer) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $fq_class_name |
||
| 328 | * @param InterfaceAnalyzer $interface_analyzer |
||
| 329 | * |
||
| 330 | * @return void |
||
| 331 | */ |
||
| 332 | public function addNamespacedInterfaceAnalyzer($fq_class_name, InterfaceAnalyzer $interface_analyzer) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return void |
||
| 339 | */ |
||
| 340 | public function getMethodMutations( |
||
| 400 | |||
| 401 | public function getFunctionLikeAnalyzer(\Psalm\Internal\MethodIdentifier $method_id) : ?FunctionLikeAnalyzer |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return null|string |
||
| 419 | */ |
||
| 420 | public function getNamespace() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param string|null $namespace_name |
||
| 427 | * |
||
| 428 | * @return array<string, string> |
||
| 429 | */ |
||
| 430 | public function getAliasedClassesFlipped($namespace_name = null) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string|null $namespace_name |
||
| 441 | * |
||
| 442 | * @return array<string, string> |
||
| 443 | */ |
||
| 444 | public function getAliasedClassesFlippedReplaceable($namespace_name = null) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return void |
||
| 455 | */ |
||
| 456 | public static function clearCache() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getFileName() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | public function getFilePath() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | public function getRootFileName() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | public function getRootFilePath() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param string $file_path |
||
| 503 | * @param string $file_name |
||
| 504 | * |
||
| 505 | * @return void |
||
| 506 | */ |
||
| 507 | public function setRootFilePath($file_path, $file_name) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param string $file_path |
||
| 515 | * |
||
| 516 | * @return void |
||
| 517 | */ |
||
| 518 | public function addRequiredFilePath($file_path) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @param string $file_path |
||
| 525 | * |
||
| 526 | * @return void |
||
| 527 | */ |
||
| 528 | public function addParentFilePath($file_path) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @param string $file_path |
||
| 535 | * |
||
| 536 | * @return bool |
||
| 537 | */ |
||
| 538 | public function hasParentFilePath($file_path) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param string $file_path |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | public function hasAlreadyRequiredFilePath($file_path) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @return array<int, string> |
||
| 555 | */ |
||
| 556 | public function getRequiredFilePaths() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @return array<int, string> |
||
| 563 | */ |
||
| 564 | public function getParentFilePaths() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @return int |
||
| 571 | */ |
||
| 572 | public function getRequireNesting() |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @return array<string> |
||
| 579 | */ |
||
| 580 | public function getSuppressedIssues() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @param array<int, string> $new_issues |
||
| 587 | * |
||
| 588 | * @return void |
||
| 589 | */ |
||
| 590 | public function addSuppressedIssues(array $new_issues) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param array<int, string> $new_issues |
||
| 601 | * |
||
| 602 | * @return void |
||
| 603 | */ |
||
| 604 | public function removeSuppressedIssues(array $new_issues) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @return null|string |
||
| 615 | */ |
||
| 616 | public function getFQCLN() |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @return null|string |
||
| 623 | */ |
||
| 624 | public function getParentFQCLN() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @return null|string |
||
| 631 | */ |
||
| 632 | public function getClassName() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @return array<string, array<string, array{Type\Union}>>|null |
||
| 639 | */ |
||
| 640 | public function getTemplateTypeMap() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @return bool |
||
| 647 | */ |
||
| 648 | public function isStatic() |
||
| 652 | |||
| 653 | public function getFileAnalyzer() : FileAnalyzer |
||
| 657 | |||
| 658 | public function getProjectAnalyzer() : ProjectAnalyzer |
||
| 662 | |||
| 663 | public function getCodebase() : Codebase |
||
| 667 | |||
| 668 | public function getFirstStatementOffset() : int |
||
| 672 | |||
| 673 | public function getNodeTypeProvider() : \Psalm\NodeTypeProvider |
||
| 681 | |||
| 682 | public function getReturnType() : ?Type\Union |
||
| 686 | |||
| 687 | public function clearSourceBeforeDestruction() : void |
||
| 692 | } |
||
| 693 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.