Complex classes like BugsPlugin 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 BugsPlugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class BugsPlugin extends AbstractDatabaseCollectorPlugin |
||
21 | { |
||
22 | |||
23 | const STATISTIC_BUG_ADDED_TO_COMMIT = 'bug_added_to_commit'; |
||
24 | |||
25 | /** |
||
26 | * Repository url. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $_repositoryUrl; |
||
31 | |||
32 | /** |
||
33 | * Repository connector. |
||
34 | * |
||
35 | * @var Connector |
||
36 | */ |
||
37 | private $_repositoryConnector; |
||
38 | |||
39 | /** |
||
40 | * Log message parser factory. |
||
41 | * |
||
42 | * @var LogMessageParserFactory |
||
43 | */ |
||
44 | private $_logMessageParserFactory; |
||
45 | |||
46 | /** |
||
47 | * Create bugs revision log plugin. |
||
48 | * |
||
49 | * @param ExtendedPdoInterface $database Database. |
||
50 | * @param RepositoryFiller $repository_filler Repository filler. |
||
51 | * @param string $repository_url Repository url. |
||
52 | * @param Connector $repository_connector Repository connector. |
||
53 | * @param LogMessageParserFactory $log_message_parser_factory Log message parser. |
||
54 | */ |
||
55 | 19 | public function __construct( |
|
68 | |||
69 | /** |
||
70 | * Returns plugin name. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 13 | public function getName() |
|
78 | |||
79 | /** |
||
80 | * Defines parsing statistic types. |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | 19 | public function defineStatisticTypes() |
|
90 | |||
91 | /** |
||
92 | * Processes data. |
||
93 | * |
||
94 | * @param integer $from_revision From revision. |
||
95 | * @param integer $to_revision To revision. |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | 9 | public function doProcess($from_revision, $to_revision) |
|
110 | |||
111 | /** |
||
112 | * Populate "BugRegExp" column for projects without it. |
||
113 | * |
||
114 | * @return void |
||
115 | */ |
||
116 | 9 | protected function populateMissingBugRegExp() |
|
137 | |||
138 | /** |
||
139 | * Determines project bug tracking regular expression. |
||
140 | * |
||
141 | * @param string $project_path Project project_path. |
||
142 | * @param integer $revision Revision. |
||
143 | * @param boolean $project_deleted Project is deleted. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 4 | protected function detectProjectBugTraqRegEx($project_path, $revision, $project_deleted) |
|
170 | |||
171 | /** |
||
172 | * Returns given project refs, where last changed are on top. |
||
173 | * |
||
174 | * @param string $project_path Path. |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | 4 | protected function getLastChangedRefPaths($project_path) |
|
219 | |||
220 | /** |
||
221 | * Detects if given project_path is known project root. |
||
222 | * |
||
223 | * @param string $path Path. |
||
224 | * |
||
225 | * @return boolean |
||
226 | */ |
||
227 | 3 | protected function isRef($path) |
|
236 | |||
237 | /** |
||
238 | * Detects bugs, associated with each commit from a given revision range. |
||
239 | * |
||
240 | * @param integer $from_revision From revision. |
||
241 | * @param integer $to_revision To revision. |
||
242 | * |
||
243 | * @return void |
||
244 | */ |
||
245 | 7 | protected function detectBugs($from_revision, $to_revision) |
|
266 | |||
267 | /** |
||
268 | * Returns "BugRegExp" field associated with every project. |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | 7 | protected function getProjectBugRegExps() |
|
288 | |||
289 | /** |
||
290 | * Detects bugs, associated with each commit from a given revision range. |
||
291 | * |
||
292 | * @param integer $from_revision From revision. |
||
293 | * @param integer $to_revision To revision. |
||
294 | * @param array $bug_regexp_mapping Mapping between project and it's "BugRegExp" field. |
||
295 | * |
||
296 | * @return void |
||
297 | */ |
||
298 | 4 | protected function doDetectBugs($from_revision, $to_revision, array $bug_regexp_mapping) |
|
321 | |||
322 | /** |
||
323 | * Returns commits grouped by project. |
||
324 | * |
||
325 | * @param integer $from_revision From revision. |
||
326 | * @param integer $to_revision To revision. |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | 4 | protected function getCommitsGroupedByProject($from_revision, $to_revision) |
|
364 | |||
365 | /** |
||
366 | * Find revisions by collected data. |
||
367 | * |
||
368 | * @param array $criteria Criteria. |
||
369 | * @param string $project_path Project path. |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | 5 | public function find(array $criteria, $project_path) |
|
391 | |||
392 | /** |
||
393 | * Returns information about revisions. |
||
394 | * |
||
395 | * @param array $revisions Revisions. |
||
396 | * |
||
397 | * @return array |
||
398 | */ |
||
399 | 1 | public function getRevisionsData(array $revisions) |
|
421 | |||
422 | } |
||
423 |
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.