Complex classes like PathsPlugin 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 PathsPlugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class PathsPlugin extends AbstractRepositoryCollectorPlugin |
||
22 | { |
||
23 | |||
24 | const TYPE_NEW = 'new'; |
||
25 | |||
26 | const TYPE_EXISTING = 'existing'; |
||
27 | |||
28 | const STATISTIC_PATH_ADDED = 'path_added'; |
||
29 | |||
30 | const STATISTIC_PATH_FOUND = 'path_found'; |
||
31 | |||
32 | const STATISTIC_PROJECT_ADDED = 'project_added'; |
||
33 | |||
34 | const STATISTIC_PROJECT_FOUND = 'project_found'; |
||
35 | |||
36 | const STATISTIC_PROJECT_COLLISION_FOUND = 'project_collision_found'; |
||
37 | |||
38 | const STATISTIC_REF_ADDED = 'ref_added'; |
||
39 | |||
40 | const STATISTIC_REF_FOUND = 'ref_found'; |
||
41 | |||
42 | const STATISTIC_COMMIT_ADDED_TO_PROJECT = 'commit_added_to_project'; |
||
43 | |||
44 | const STATISTIC_COMMIT_ADDED_TO_REF = 'commit_added_to_ref'; |
||
45 | |||
46 | const STATISTIC_EMPTY_COMMIT = 'empty_commit'; |
||
47 | |||
48 | /** |
||
49 | * Database cache. |
||
50 | * |
||
51 | * @var DatabaseCache |
||
52 | */ |
||
53 | private $_databaseCache; |
||
54 | |||
55 | /** |
||
56 | * Projects. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | private $_projects = array( |
||
61 | self::TYPE_NEW => array(), |
||
62 | self::TYPE_EXISTING => array(), |
||
63 | ); |
||
64 | |||
65 | /** |
||
66 | * Refs. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | private $_refs = array(); |
||
71 | |||
72 | /** |
||
73 | * Repository connector. |
||
74 | * |
||
75 | * @var Connector |
||
76 | */ |
||
77 | private $_repositoryConnector; |
||
78 | |||
79 | /** |
||
80 | * Path collision detector. |
||
81 | * |
||
82 | * @var PathCollisionDetector |
||
83 | */ |
||
84 | private $_pathCollisionDetector; |
||
85 | |||
86 | /** |
||
87 | * Create paths revision log plugin. |
||
88 | * |
||
89 | * @param ExtendedPdoInterface $database Database. |
||
90 | * @param RepositoryFiller $repository_filler Repository filler. |
||
91 | * @param DatabaseCache $database_cache Database cache. |
||
92 | * @param Connector $repository_connector Repository connector. |
||
93 | * @param PathCollisionDetector $path_collision_detector Path collision detector. |
||
94 | */ |
||
95 | 34 | public function __construct( |
|
110 | |||
111 | /** |
||
112 | * Hook, that is called before "RevisionLog::refresh" method call. |
||
113 | * |
||
114 | * @return void |
||
115 | */ |
||
116 | 34 | public function whenDatabaseReady() |
|
124 | |||
125 | /** |
||
126 | * Initializes database cache. |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | 34 | protected function initDatabaseCache() |
|
136 | |||
137 | /** |
||
138 | * Returns plugin name. |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | 25 | public function getName() |
|
146 | |||
147 | /** |
||
148 | * Returns revision query flags. |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | 1 | public function getRevisionQueryFlags() |
|
156 | |||
157 | /** |
||
158 | * Defines parsing statistic types. |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | 34 | public function defineStatisticTypes() |
|
177 | |||
178 | /** |
||
179 | * Does actual parsing. |
||
180 | * |
||
181 | * @param integer $revision Revision. |
||
182 | * @param \SimpleXMLElement $log_entry Log Entry. |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | 19 | protected function doParse($revision, \SimpleXMLElement $log_entry) |
|
239 | |||
240 | /** |
||
241 | * Sorts paths to move parent folders above their sub-folders. |
||
242 | * |
||
243 | * @param \SimpleXMLElement $paths Paths. |
||
244 | * |
||
245 | * @return \SimpleXMLElement[] |
||
246 | */ |
||
247 | 18 | protected function sortPaths(\SimpleXMLElement $paths) |
|
259 | |||
260 | /** |
||
261 | * Processes path. |
||
262 | * |
||
263 | * @param string $path Path. |
||
264 | * @param integer $revision Revision. |
||
265 | * @param string $action Action. |
||
266 | * @param boolean $is_usage This is usage. |
||
267 | * |
||
268 | * @return integer |
||
269 | */ |
||
270 | 18 | protected function processPath($path, $revision, $action, $is_usage = true) |
|
342 | |||
343 | /** |
||
344 | * Adapts path to kind. |
||
345 | * |
||
346 | * @param string $path Path. |
||
347 | * @param string $kind Kind. |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | 18 | protected function adaptPathToKind($path, $kind) |
|
359 | |||
360 | /** |
||
361 | * Processes project. |
||
362 | * |
||
363 | * @param string $project_path Project path. |
||
364 | * @param boolean $is_usage This is usage. |
||
365 | * |
||
366 | * @return integer |
||
367 | */ |
||
368 | 5 | protected function processProject($project_path, $is_usage = true) |
|
403 | |||
404 | /** |
||
405 | * Processes ref. |
||
406 | * |
||
407 | * @param integer $project_id Project ID. |
||
408 | * @param string $ref Ref. |
||
409 | * @param boolean $is_usage This is usage. |
||
410 | * |
||
411 | * @return integer |
||
412 | */ |
||
413 | 5 | protected function processRef($project_id, $ref, $is_usage = true) |
|
450 | |||
451 | /** |
||
452 | * Retroactively map paths/commits to project, where path doesn't contain ref. |
||
453 | * |
||
454 | * @param integer $project_id Project ID. |
||
455 | * @param string $project_path Project path. |
||
456 | * |
||
457 | * @return array |
||
458 | */ |
||
459 | 5 | protected function addMissingCommitsToProject($project_id, $project_path) |
|
483 | |||
484 | /** |
||
485 | * Associates revision with project. |
||
486 | * |
||
487 | * @param integer $revision Revision. |
||
488 | * @param integer $project_id Project. |
||
489 | * |
||
490 | * @return void |
||
491 | */ |
||
492 | 5 | protected function addCommitToProject($revision, $project_id) |
|
497 | |||
498 | /** |
||
499 | * Associates revision with ref. |
||
500 | * |
||
501 | * @param integer $revision Revision. |
||
502 | * @param integer $ref_id Ref. |
||
503 | * |
||
504 | * @return void |
||
505 | */ |
||
506 | 5 | protected function addCommitToRef($revision, $ref_id) |
|
511 | |||
512 | /** |
||
513 | * Find revisions by collected data. |
||
514 | * |
||
515 | * @param array $criteria Criteria. |
||
516 | * @param string|null $project_path Project path. |
||
517 | * |
||
518 | * @return array |
||
519 | */ |
||
520 | 7 | public function find(array $criteria, $project_path) |
|
577 | |||
578 | /** |
||
579 | * Returns information about revisions. |
||
580 | * |
||
581 | * @param array $revisions Revisions. |
||
582 | * |
||
583 | * @return array |
||
584 | */ |
||
585 | 2 | public function getRevisionsData(array $revisions) |
|
616 | |||
617 | /** |
||
618 | * Frees consumed memory. |
||
619 | * |
||
620 | * @return void |
||
621 | * |
||
622 | * @codeCoverageIgnore |
||
623 | */ |
||
624 | protected function freeMemoryManually() |
||
630 | |||
631 | } |
||
632 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.