Complex classes like Codebase 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 Codebase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class Codebase |
||
44 | { |
||
45 | /** |
||
46 | * @var Config |
||
47 | */ |
||
48 | public $config; |
||
49 | |||
50 | /** |
||
51 | * A map of fully-qualified use declarations to the files |
||
52 | * that reference them (keyed by filename) |
||
53 | * |
||
54 | * @var array<string, array<int, \Psalm\CodeLocation>> |
||
55 | */ |
||
56 | public $use_referencing_locations = []; |
||
57 | |||
58 | /** |
||
59 | * A map of file names to the classes that they contain explicit references to |
||
60 | * used in collaboration with use_referencing_locations |
||
61 | * |
||
62 | * @var array<string, array<string, bool>> |
||
63 | */ |
||
64 | public $use_referencing_files = []; |
||
65 | |||
66 | /** |
||
67 | * @var FileStorageProvider |
||
68 | */ |
||
69 | public $file_storage_provider; |
||
70 | |||
71 | /** |
||
72 | * @var ClassLikeStorageProvider |
||
73 | */ |
||
74 | public $classlike_storage_provider; |
||
75 | |||
76 | /** |
||
77 | * @var bool |
||
78 | */ |
||
79 | public $collect_references = false; |
||
80 | |||
81 | /** |
||
82 | * @var bool |
||
83 | */ |
||
84 | public $collect_locations = false; |
||
85 | |||
86 | /** |
||
87 | * @var null|'always'|'auto' |
||
88 | */ |
||
89 | public $find_unused_code = null; |
||
90 | |||
91 | /** |
||
92 | * @var FileProvider |
||
93 | */ |
||
94 | public $file_provider; |
||
95 | |||
96 | /** |
||
97 | * @var FileReferenceProvider |
||
98 | */ |
||
99 | public $file_reference_provider; |
||
100 | |||
101 | /** |
||
102 | * @var StatementsProvider |
||
103 | */ |
||
104 | public $statements_provider; |
||
105 | |||
106 | /** |
||
107 | * @var Progress |
||
108 | */ |
||
109 | private $progress; |
||
110 | |||
111 | /** |
||
112 | * @var array<string, Type\Union> |
||
113 | */ |
||
114 | private static $stubbed_constants = []; |
||
115 | |||
116 | /** |
||
117 | * Whether to register autoloaded information |
||
118 | * |
||
119 | * @var bool |
||
120 | */ |
||
121 | public $register_autoload_files = false; |
||
122 | |||
123 | /** |
||
124 | * Whether to log functions just at the file level or globally (for stubs) |
||
125 | * |
||
126 | * @var bool |
||
127 | */ |
||
128 | public $register_stub_files = false; |
||
129 | |||
130 | /** |
||
131 | * @var bool |
||
132 | */ |
||
133 | public $find_unused_variables = false; |
||
134 | |||
135 | /** |
||
136 | * @var Internal\Codebase\Scanner |
||
137 | */ |
||
138 | public $scanner; |
||
139 | |||
140 | /** |
||
141 | * @var Internal\Codebase\Analyzer |
||
142 | */ |
||
143 | public $analyzer; |
||
144 | |||
145 | /** |
||
146 | * @var Internal\Codebase\Functions |
||
147 | */ |
||
148 | public $functions; |
||
149 | |||
150 | /** |
||
151 | * @var Internal\Codebase\ClassLikes |
||
152 | */ |
||
153 | public $classlikes; |
||
154 | |||
155 | /** |
||
156 | * @var Internal\Codebase\Methods |
||
157 | */ |
||
158 | public $methods; |
||
159 | |||
160 | /** |
||
161 | * @var Internal\Codebase\Properties |
||
162 | */ |
||
163 | public $properties; |
||
164 | |||
165 | /** |
||
166 | * @var Internal\Codebase\Populator |
||
167 | */ |
||
168 | public $populator; |
||
169 | |||
170 | /** |
||
171 | * @var ?Internal\Codebase\Taint |
||
172 | */ |
||
173 | public $taint = null; |
||
174 | |||
175 | /** |
||
176 | * @var bool |
||
177 | */ |
||
178 | public $server_mode = false; |
||
179 | |||
180 | /** |
||
181 | * @var bool |
||
182 | */ |
||
183 | public $store_node_types = false; |
||
184 | |||
185 | /** |
||
186 | * Whether or not to infer types from usage. Computationally expensive, so turned off by default |
||
187 | * |
||
188 | * @var bool |
||
189 | */ |
||
190 | public $infer_types_from_usage = false; |
||
191 | |||
192 | /** |
||
193 | * @var bool |
||
194 | */ |
||
195 | public $alter_code = false; |
||
196 | |||
197 | /** |
||
198 | * @var bool |
||
199 | */ |
||
200 | public $diff_methods = false; |
||
201 | |||
202 | /** |
||
203 | * @var array<lowercase-string, string> |
||
204 | */ |
||
205 | public $methods_to_move = []; |
||
206 | |||
207 | /** |
||
208 | * @var array<lowercase-string, string> |
||
209 | */ |
||
210 | public $methods_to_rename = []; |
||
211 | |||
212 | /** |
||
213 | * @var array<string, string> |
||
214 | */ |
||
215 | public $properties_to_move = []; |
||
216 | |||
217 | /** |
||
218 | * @var array<string, string> |
||
219 | */ |
||
220 | public $properties_to_rename = []; |
||
221 | |||
222 | /** |
||
223 | * @var array<string, string> |
||
224 | */ |
||
225 | public $class_constants_to_move = []; |
||
226 | |||
227 | /** |
||
228 | * @var array<string, string> |
||
229 | */ |
||
230 | public $class_constants_to_rename = []; |
||
231 | |||
232 | /** |
||
233 | * @var array<lowercase-string, string> |
||
234 | */ |
||
235 | public $classes_to_move = []; |
||
236 | |||
237 | /** |
||
238 | * @var array<string, string> |
||
239 | */ |
||
240 | public $call_transforms = []; |
||
241 | |||
242 | /** |
||
243 | * @var array<string, string> |
||
244 | */ |
||
245 | public $property_transforms = []; |
||
246 | |||
247 | /** |
||
248 | * @var array<string, string> |
||
249 | */ |
||
250 | public $class_constant_transforms = []; |
||
251 | |||
252 | /** |
||
253 | * @var array<lowercase-string, string> |
||
254 | */ |
||
255 | public $class_transforms = []; |
||
256 | |||
257 | /** |
||
258 | * @var bool |
||
259 | */ |
||
260 | public $allow_backwards_incompatible_changes = true; |
||
261 | |||
262 | /** |
||
263 | * @var int |
||
264 | */ |
||
265 | public $php_major_version = PHP_MAJOR_VERSION; |
||
266 | |||
267 | /** |
||
268 | * @var int |
||
269 | */ |
||
270 | public $php_minor_version = PHP_MINOR_VERSION; |
||
271 | |||
272 | /** |
||
273 | * @var bool |
||
274 | */ |
||
275 | public $track_unused_suppressions = false; |
||
276 | |||
277 | public function __construct( |
||
342 | |||
343 | /** |
||
344 | * @return void |
||
345 | */ |
||
346 | private function loadAnalyzer() |
||
355 | |||
356 | /** |
||
357 | * @param array<string> $candidate_files |
||
358 | * |
||
359 | * @return void |
||
360 | */ |
||
361 | public function reloadFiles(ProjectAnalyzer $project_analyzer, array $candidate_files) |
||
418 | |||
419 | /** @return void */ |
||
420 | public function enterServerMode() |
||
425 | |||
426 | /** |
||
427 | * @return void |
||
428 | */ |
||
429 | public function collectLocations() |
||
436 | |||
437 | /** |
||
438 | * @param 'always'|'auto' $find_unused_code |
||
|
|||
439 | * |
||
440 | * @return void |
||
441 | */ |
||
442 | public function reportUnusedCode(string $find_unused_code = 'auto') |
||
449 | |||
450 | /** |
||
451 | * @return void |
||
452 | */ |
||
453 | public function reportUnusedVariables() |
||
458 | |||
459 | /** |
||
460 | * @param array<string, string> $files_to_analyze |
||
461 | * |
||
462 | * @return void |
||
463 | */ |
||
464 | public function addFilesToAnalyze(array $files_to_analyze) |
||
469 | |||
470 | /** |
||
471 | * Scans all files their related files |
||
472 | * |
||
473 | * @return void |
||
474 | */ |
||
475 | public function scanFiles(int $threads = 1) |
||
483 | |||
484 | /** |
||
485 | * @param string $file_path |
||
486 | * |
||
487 | * @return string |
||
488 | */ |
||
489 | public function getFileContents($file_path) |
||
493 | |||
494 | /** |
||
495 | * @param string $file_path |
||
496 | * |
||
497 | * @return list<PhpParser\Node\Stmt> |
||
498 | */ |
||
499 | public function getStatementsForFile($file_path) |
||
506 | |||
507 | /** |
||
508 | * @param string $fq_classlike_name |
||
509 | * |
||
510 | * @return ClassLikeStorage |
||
511 | */ |
||
512 | public function createClassLikeStorage($fq_classlike_name) |
||
516 | |||
517 | /** |
||
518 | * @param string $file_path |
||
519 | * |
||
520 | * @return void |
||
521 | */ |
||
522 | public function cacheClassLikeStorage(ClassLikeStorage $classlike_storage, $file_path) |
||
530 | |||
531 | /** |
||
532 | * @param string $fq_classlike_name |
||
533 | * @param string $file_path |
||
534 | * |
||
535 | * @return void |
||
536 | */ |
||
537 | public function exhumeClassLikeStorage($fq_classlike_name, $file_path) |
||
554 | |||
555 | /** |
||
556 | * @param ?\ReflectionType $type |
||
557 | */ |
||
558 | public static function getPsalmTypeFromReflection($type) : Type\Union |
||
562 | |||
563 | /** |
||
564 | * @param string $file_path |
||
565 | * |
||
566 | * @return FileStorage |
||
567 | */ |
||
568 | public function createFileStorageForPath($file_path) |
||
572 | |||
573 | /** |
||
574 | * @param string $symbol |
||
575 | * |
||
576 | * @return \Psalm\CodeLocation[] |
||
577 | */ |
||
578 | public function findReferencesToSymbol($symbol) |
||
594 | |||
595 | /** |
||
596 | * @param string $method_id |
||
597 | * |
||
598 | * @return \Psalm\CodeLocation[] |
||
599 | */ |
||
600 | public function findReferencesToMethod($method_id) |
||
604 | |||
605 | /** |
||
606 | * @return \Psalm\CodeLocation[] |
||
607 | */ |
||
608 | public function findReferencesToProperty(string $property_id) |
||
616 | |||
617 | /** |
||
618 | * @param string $fq_class_name |
||
619 | * |
||
620 | * @return \Psalm\CodeLocation[] |
||
621 | */ |
||
622 | public function findReferencesToClassLike($fq_class_name) |
||
633 | |||
634 | /** |
||
635 | * @param string $file_path |
||
636 | * @param string $closure_id |
||
637 | * |
||
638 | * @return FunctionLikeStorage |
||
639 | */ |
||
640 | public function getClosureStorage($file_path, $closure_id) |
||
653 | |||
654 | /** |
||
655 | * @param string $const_id |
||
656 | * @param Type\Union $type |
||
657 | * |
||
658 | * @return void |
||
659 | */ |
||
660 | public function addGlobalConstantType($const_id, Type\Union $type) |
||
664 | |||
665 | /** |
||
666 | * @param string $const_id |
||
667 | * |
||
668 | * @return Type\Union|null |
||
669 | */ |
||
670 | public function getStubbedConstantType($const_id) |
||
674 | |||
675 | /** |
||
676 | * @return array<string, Type\Union> |
||
677 | */ |
||
678 | public function getAllStubbedConstants() |
||
682 | |||
683 | /** |
||
684 | * @param string $file_path |
||
685 | * |
||
686 | * @return bool |
||
687 | */ |
||
688 | public function fileExists($file_path) |
||
692 | |||
693 | /** |
||
694 | * Check whether a class/interface exists |
||
695 | * |
||
696 | * @param string $fq_class_name |
||
697 | * @param CodeLocation $code_location |
||
698 | * |
||
699 | * @return bool |
||
700 | */ |
||
701 | public function classOrInterfaceExists( |
||
714 | |||
715 | /** |
||
716 | * @param string $fq_class_name |
||
717 | * @param string $possible_parent |
||
718 | * |
||
719 | * @return bool |
||
720 | */ |
||
721 | public function classExtendsOrImplements($fq_class_name, $possible_parent) |
||
726 | |||
727 | /** |
||
728 | * Determine whether or not a given class exists |
||
729 | * |
||
730 | * @param string $fq_class_name |
||
731 | * |
||
732 | * @return bool |
||
733 | */ |
||
734 | public function classExists( |
||
747 | |||
748 | /** |
||
749 | * Determine whether or not a class extends a parent |
||
750 | * |
||
751 | * @param string $fq_class_name |
||
752 | * @param string $possible_parent |
||
753 | * |
||
754 | * @throws \Psalm\Exception\UnpopulatedClasslikeException when called on unpopulated class |
||
755 | * @throws \InvalidArgumentException when class does not exist |
||
756 | * |
||
757 | * @return bool |
||
758 | */ |
||
759 | public function classExtends($fq_class_name, $possible_parent) |
||
763 | |||
764 | /** |
||
765 | * Check whether a class implements an interface |
||
766 | * |
||
767 | * @param string $fq_class_name |
||
768 | * @param string $interface |
||
769 | * |
||
770 | * @return bool |
||
771 | */ |
||
772 | public function classImplements($fq_class_name, $interface) |
||
776 | |||
777 | /** |
||
778 | * @param string $fq_interface_name |
||
779 | * |
||
780 | * @return bool |
||
781 | */ |
||
782 | public function interfaceExists( |
||
795 | |||
796 | /** |
||
797 | * @param string $interface_name |
||
798 | * @param string $possible_parent |
||
799 | * |
||
800 | * @return bool |
||
801 | */ |
||
802 | public function interfaceExtends($interface_name, $possible_parent) |
||
806 | |||
807 | /** |
||
808 | * @param string $fq_interface_name |
||
809 | * |
||
810 | * @return array<string> all interfaces extended by $interface_name |
||
811 | */ |
||
812 | public function getParentInterfaces($fq_interface_name) |
||
818 | |||
819 | /** |
||
820 | * Determine whether or not a class has the correct casing |
||
821 | * |
||
822 | * @param string $fq_class_name |
||
823 | * |
||
824 | * @return bool |
||
825 | */ |
||
826 | public function classHasCorrectCasing($fq_class_name) |
||
830 | |||
831 | /** |
||
832 | * @param string $fq_interface_name |
||
833 | * |
||
834 | * @return bool |
||
835 | */ |
||
836 | public function interfaceHasCorrectCasing($fq_interface_name) |
||
840 | |||
841 | /** |
||
842 | * @param string $fq_trait_name |
||
843 | * |
||
844 | * @return bool |
||
845 | */ |
||
846 | public function traitHasCorrectCase($fq_trait_name) |
||
850 | |||
851 | /** |
||
852 | * Given a function id, return the function like storage for |
||
853 | * a method, closure, or function. |
||
854 | * |
||
855 | * @param non-empty-string $function_id |
||
856 | */ |
||
857 | public function getFunctionLikeStorage( |
||
879 | |||
880 | /** |
||
881 | * Whether or not a given method exists |
||
882 | * |
||
883 | * @param string|\Psalm\Internal\MethodIdentifier $method_id |
||
884 | * @param string|\Psalm\Internal\MethodIdentifier|null $calling_method_id |
||
885 | * |
||
886 | @return bool |
||
887 | */ |
||
888 | public function methodExists( |
||
902 | |||
903 | /** |
||
904 | * @param string|\Psalm\Internal\MethodIdentifier $method_id |
||
905 | * |
||
906 | * @return array<int, \Psalm\Storage\FunctionLikeParameter> |
||
907 | */ |
||
908 | public function getMethodParams($method_id) |
||
912 | |||
913 | /** |
||
914 | * @param string|\Psalm\Internal\MethodIdentifier $method_id |
||
915 | * |
||
916 | * @return bool |
||
917 | */ |
||
918 | public function isVariadic($method_id) |
||
922 | |||
923 | /** |
||
924 | * @param string|\Psalm\Internal\MethodIdentifier $method_id |
||
925 | * @param string $self_class |
||
926 | * @param array<int, PhpParser\Node\Arg> $call_args |
||
927 | * |
||
928 | * @return Type\Union|null |
||
929 | */ |
||
930 | public function getMethodReturnType($method_id, &$self_class, array $call_args = []) |
||
939 | |||
940 | /** |
||
941 | * @param string|\Psalm\Internal\MethodIdentifier $method_id |
||
942 | * |
||
943 | * @return bool |
||
944 | */ |
||
945 | public function getMethodReturnsByRef($method_id) |
||
949 | |||
950 | /** |
||
951 | * @param string|\Psalm\Internal\MethodIdentifier $method_id |
||
952 | * @param CodeLocation|null $defined_location |
||
953 | * |
||
954 | * @return CodeLocation|null |
||
955 | */ |
||
956 | public function getMethodReturnTypeLocation( |
||
965 | |||
966 | /** |
||
967 | * @param string|\Psalm\Internal\MethodIdentifier $method_id |
||
968 | * |
||
969 | * @return string|null |
||
970 | */ |
||
971 | public function getDeclaringMethodId($method_id) |
||
975 | |||
976 | /** |
||
977 | * Get the class this method appears in (vs is declared in, which could give a trait) |
||
978 | * |
||
979 | * @param string|\Psalm\Internal\MethodIdentifier $method_id |
||
980 | * |
||
981 | * @return string|null |
||
982 | */ |
||
983 | public function getAppearingMethodId($method_id) |
||
987 | |||
988 | /** |
||
989 | * @param string|\Psalm\Internal\MethodIdentifier $method_id |
||
990 | * |
||
991 | * @return array<string> |
||
992 | */ |
||
993 | public function getOverriddenMethodIds($method_id) |
||
997 | |||
998 | /** |
||
999 | * @param string|\Psalm\Internal\MethodIdentifier $method_id |
||
1000 | * |
||
1001 | * @return string |
||
1002 | */ |
||
1003 | public function getCasedMethodId($method_id) |
||
1007 | |||
1008 | /** |
||
1009 | * @param string $file_path |
||
1010 | * |
||
1011 | * @return void |
||
1012 | */ |
||
1013 | public function invalidateInformationForFile(string $file_path) |
||
1030 | |||
1031 | /** |
||
1032 | * @return ?string |
||
1033 | */ |
||
1034 | public function getSymbolInformation(string $file_path, string $symbol) |
||
1107 | |||
1108 | /** |
||
1109 | * @return ?CodeLocation |
||
1110 | */ |
||
1111 | public function getSymbolLocation(string $file_path, string $symbol) |
||
1196 | |||
1197 | /** |
||
1198 | * @return array{0: string, 1: Range}|null |
||
1199 | */ |
||
1200 | public function getReferenceAtPosition(string $file_path, Position $position) |
||
1249 | |||
1250 | /** |
||
1251 | * @return array{0: non-empty-string, 1: int, 2: Range}|null |
||
1252 | */ |
||
1253 | public function getFunctionArgumentAtPosition(string $file_path, Position $position) |
||
1303 | |||
1304 | /** |
||
1305 | * @param non-empty-string $function_symbol |
||
1306 | */ |
||
1307 | public function getSignatureInformation(string $function_symbol) : ?\LanguageServerProtocol\SignatureInformation |
||
1364 | |||
1365 | /** |
||
1366 | * @return array{0: string, 1: '->'|'::'|'symbol', 2: int}|null |
||
1367 | */ |
||
1368 | public function getCompletionDataAtPosition(string $file_path, Position $position) |
||
1428 | |||
1429 | /** |
||
1430 | * @return array<int, \LanguageServerProtocol\CompletionItem> |
||
1431 | */ |
||
1432 | public function getCompletionItemsForClassishThing(string $type_string, string $gap) : array |
||
1519 | |||
1520 | /** |
||
1521 | * @return array<int, \LanguageServerProtocol\CompletionItem> |
||
1522 | */ |
||
1523 | public function getCompletionItemsForPartialSymbol( |
||
1625 | |||
1626 | private static function getPositionFromOffset(int $offset, string $file_contents) : Position |
||
1637 | |||
1638 | /** |
||
1639 | * @return void |
||
1640 | */ |
||
1641 | public function addTemporaryFileChanges(string $file_path, string $new_content) |
||
1645 | |||
1646 | /** |
||
1647 | * @return void |
||
1648 | */ |
||
1649 | public function removeTemporaryFileChanges(string $file_path) |
||
1653 | |||
1654 | /** |
||
1655 | * Checks if type is a subtype of other |
||
1656 | * |
||
1657 | * Given two types, checks if `$input_type` is a subtype of `$container_type`. |
||
1658 | * If you consider `Type\Union` as a set of types, this will tell you |
||
1659 | * if `$input_type` is fully contained in `$container_type`, |
||
1660 | * |
||
1661 | * $input_type ⊆ $container_type |
||
1662 | * |
||
1663 | * Useful for emitting issues like InvalidArgument, where argument at the call site |
||
1664 | * should be a subset of the function parameter type. |
||
1665 | */ |
||
1666 | public function isTypeContainedByType( |
||
1672 | |||
1673 | /** |
||
1674 | * Checks if type has any part that is a subtype of other |
||
1675 | * |
||
1676 | * Given two types, checks if *any part* of `$input_type` is a subtype of `$container_type`. |
||
1677 | * If you consider `Type\Union` as a set of types, this will tell you if intersection |
||
1678 | * of `$input_type` with `$container_type` is not empty. |
||
1679 | * |
||
1680 | * $input_type ∩ $container_type ≠ ∅ , e.g. they are not disjoint. |
||
1681 | * |
||
1682 | * Useful for emitting issues like PossiblyInvalidArgument, where argument at the call |
||
1683 | * site should be a subtype of the function parameter type, but it's has some types that are |
||
1684 | * not a subtype of the required type. |
||
1685 | */ |
||
1686 | public function canTypeBeContainedByType( |
||
1692 | |||
1693 | /** |
||
1694 | * Extracts key and value types from a traversable object (or iterable) |
||
1695 | * |
||
1696 | * Given an iterable type (*but not TArray*) returns a tuple of it's key/value types. |
||
1697 | * First element of the tuple holds key type, second has the value type. |
||
1698 | * |
||
1699 | * Example: |
||
1700 | * ```php |
||
1701 | * $codebase->getKeyValueParamsForTraversableObject(Type::parseString('iterable<int,string>')) |
||
1702 | * // returns [Union(TInt), Union(TString)] |
||
1703 | * ``` |
||
1704 | * |
||
1705 | * @return array{Type\Union,Type\Union} |
||
1706 | */ |
||
1707 | public function getKeyValueParamsForTraversableObject(Type\Atomic $type): array |
||
1719 | |||
1720 | /** |
||
1721 | * @param array<string, mixed> $phantom_classes |
||
1722 | * @psalm-suppress PossiblyUnusedMethod part of the public API |
||
1723 | */ |
||
1724 | public function queueClassLikeForScanning( |
||
1732 | |||
1733 | /** |
||
1734 | * @param array<string> $taints |
||
1735 | * |
||
1736 | * @psalm-suppress PossiblyUnusedMethod |
||
1737 | */ |
||
1738 | public function addTaintSource( |
||
1762 | |||
1763 | /** |
||
1764 | * @param array<string> $taints |
||
1765 | * |
||
1766 | * @psalm-suppress PossiblyUnusedMethod |
||
1767 | */ |
||
1768 | public function addTaintSink( |
||
1787 | } |
||
1788 |
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.