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 | 21 | public function __construct( |
|
68 | |||
69 | /** |
||
70 | * Returns plugin name. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 15 | public function getName() |
|
78 | |||
79 | /** |
||
80 | * Defines parsing statistic types. |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | 21 | 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 | 11 | public function doProcess($from_revision, $to_revision) |
|
110 | |||
111 | /** |
||
112 | * Populate "BugRegExp" column for projects without it. |
||
113 | * |
||
114 | * @param boolean $cache_overwrite Overwrite used "bugtraq:logregex" SVN property's cached value. |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | 11 | protected function populateMissingBugRegExp($cache_overwrite = false) |
|
140 | |||
141 | /** |
||
142 | * Determines project bug tracking regular expression. |
||
143 | * |
||
144 | * @param string $project_path Project project_path. |
||
145 | * @param integer $revision Revision. |
||
146 | * @param boolean $project_deleted Project is deleted. |
||
147 | * @param boolean $cache_overwrite Overwrite used "bugtraq:logregex" SVN property's cached value. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 6 | protected function detectProjectBugTraqRegEx($project_path, $revision, $project_deleted, $cache_overwrite = false) |
|
174 | |||
175 | /** |
||
176 | * Returns given project refs, where last changed are on top. |
||
177 | * |
||
178 | * @param string $project_path Path. |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | 6 | protected function getLastChangedRefPaths($project_path) |
|
223 | |||
224 | /** |
||
225 | * Detects if given project_path is known project root. |
||
226 | * |
||
227 | * @param string $path Path. |
||
228 | * |
||
229 | * @return boolean |
||
230 | */ |
||
231 | 5 | protected function isRef($path) |
|
240 | |||
241 | /** |
||
242 | * Detects bugs, associated with each commit from a given revision range. |
||
243 | * |
||
244 | * @param integer $from_revision From revision. |
||
245 | * @param integer $to_revision To revision. |
||
246 | * |
||
247 | * @return void |
||
248 | */ |
||
249 | 7 | protected function detectBugs($from_revision, $to_revision) |
|
270 | |||
271 | /** |
||
272 | * Returns "BugRegExp" field associated with every project. |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | 7 | protected function getProjectBugRegExps() |
|
292 | |||
293 | /** |
||
294 | * Detects bugs, associated with each commit from a given revision range. |
||
295 | * |
||
296 | * @param integer $from_revision From revision. |
||
297 | * @param integer $to_revision To revision. |
||
298 | * @param array $bug_regexp_mapping Mapping between project and it's "BugRegExp" field. |
||
299 | * |
||
300 | * @return void |
||
301 | */ |
||
302 | 4 | protected function doDetectBugs($from_revision, $to_revision, array $bug_regexp_mapping) |
|
325 | |||
326 | /** |
||
327 | * Returns commits grouped by project. |
||
328 | * |
||
329 | * @param integer $from_revision From revision. |
||
330 | * @param integer $to_revision To revision. |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | 4 | protected function getCommitsGroupedByProject($from_revision, $to_revision) |
|
368 | |||
369 | /** |
||
370 | * Find revisions by collected data. |
||
371 | * |
||
372 | * @param array $criteria Criteria. |
||
373 | * @param string $project_path Project path. |
||
374 | * |
||
375 | * @return array |
||
376 | */ |
||
377 | 5 | public function find(array $criteria, $project_path) |
|
395 | |||
396 | /** |
||
397 | * Returns information about revisions. |
||
398 | * |
||
399 | * @param array $revisions Revisions. |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | 1 | public function getRevisionsData(array $revisions) |
|
425 | |||
426 | /** |
||
427 | * Refreshes BugRegExp of a project. |
||
428 | * |
||
429 | * @param string $project_path Project path. |
||
430 | * |
||
431 | * @return void |
||
432 | */ |
||
433 | 2 | public function refreshBugRegExp($project_path) |
|
446 | |||
447 | } |
||
448 |
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.