Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AnalysisResult 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 AnalysisResult, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class AnalysisResult implements \IteratorAggregate, \Countable |
||
30 | { |
||
31 | const INITIAL_ANALYSIS_TARGET_ID = 'unknown'; |
||
32 | const VERSION_LIMIT_MIN = 0; |
||
33 | const VERSION_LIMIT_MAX = 1; |
||
34 | |||
35 | /** |
||
36 | * The state of this instance |
||
37 | * |
||
38 | * @var bool |
||
39 | */ |
||
40 | private $isSealed = false; |
||
41 | |||
42 | /** |
||
43 | * @var string Filename or hash of input string |
||
44 | */ |
||
45 | private $analysisTargetId = self::INITIAL_ANALYSIS_TARGET_ID; |
||
46 | |||
47 | /** |
||
48 | * @var array|Reasoning[] |
||
49 | */ |
||
50 | private $requirements = []; |
||
51 | |||
52 | /** |
||
53 | * @var array|Reasoning[] |
||
54 | */ |
||
55 | private $limits = []; |
||
56 | |||
57 | /** |
||
58 | * @var string|null |
||
59 | */ |
||
60 | private $cachedVersionRequirement; |
||
61 | |||
62 | /** |
||
63 | * @var string|null |
||
64 | */ |
||
65 | private $cachedVersionLimit; |
||
66 | |||
67 | /** |
||
68 | * @var int |
||
69 | */ |
||
70 | private $cachedRequiredVersionId; |
||
71 | |||
72 | /** |
||
73 | * @var int |
||
74 | */ |
||
75 | private $cachedLimitedVersionId; |
||
76 | |||
77 | /** |
||
78 | * Number of attached `Reasonings` instances. |
||
79 | * |
||
80 | * Related to the `\Countable` interface. |
||
81 | * |
||
82 | * @var int |
||
83 | * @see Pvra\AnalysisResult::count Accessable through count |
||
84 | */ |
||
85 | private $count = 0; |
||
86 | |||
87 | /** |
||
88 | * @var MessageFormatter |
||
89 | */ |
||
90 | private $msgFormatter; |
||
91 | |||
92 | /** |
||
93 | * Calculate the id of the required version |
||
94 | * |
||
95 | * Creates an integer representation of a version in the format "a.b[.c]". |
||
96 | * The third version element is optional and can be omitted. A default value of "0" will be |
||
97 | * assumed. |
||
98 | * |
||
99 | * @return int |
||
100 | * @throws \Exception |
||
101 | */ |
||
102 | 22 | public function getRequiredVersionId() |
|
110 | |||
111 | 2 | public function getVersionLimitId() |
|
119 | |||
120 | /** |
||
121 | * @param string $version |
||
122 | * @return int |
||
123 | * @throws \Exception |
||
124 | */ |
||
125 | 24 | private function calculateVersionIdFromString($version) |
|
139 | |||
140 | /** |
||
141 | * Get the attached message formatter |
||
142 | * |
||
143 | * If no message formatter has been set a default one will be created assuming default values. |
||
144 | * |
||
145 | * @return \Pvra\Result\MessageFormatter |
||
146 | */ |
||
147 | 50 | public function getMsgFormatter() |
|
155 | |||
156 | /** |
||
157 | * @param \Pvra\Result\MessageFormatter $formatter |
||
158 | * @return $this |
||
159 | */ |
||
160 | 48 | public function setMsgFormatter(MessageFormatter $formatter) |
|
166 | |||
167 | /** |
||
168 | * Retrieve the required version |
||
169 | * |
||
170 | * This method calculates the highest required version of all known requirements. |
||
171 | * If no changes were made between the calls to this method the version requirement will |
||
172 | * not be recalculated. |
||
173 | * |
||
174 | * @return string The required version in the format `Major.Minor[.Patch]` |
||
175 | * @see http://php.net/manual/en/function.version-compare.php version_compare() |
||
176 | */ |
||
177 | 110 | public function getRequiredVersion() |
|
191 | |||
192 | /** |
||
193 | 12 | * Retrieve the upper version limit |
|
194 | * |
||
195 | * This method calculates the upper version limit of all known reasonings. |
||
196 | * If no changes were made between the calls to this method the version limit will |
||
197 | * not be recalculated. |
||
198 | * |
||
199 | * @return string The version limit in the format `Major.Minor[.Patch]` |
||
200 | * @see http://php.net/manual/en/function.version-compare.php version_compare() |
||
201 | */ |
||
202 | public function getVersionLimit() |
||
216 | 4 | ||
217 | 14 | /** |
|
218 | * Add an arbitrary requirement identified by version |
||
219 | 14 | * |
|
220 | * This method can be used to add an arbitrary requirement. All parameters but the first are optional |
||
221 | * |
||
222 | 2 | * @param string $version The version in the format `Major.Minor[.Patch]` |
|
223 | * @param int $line The line that caused the requirement. |
||
224 | * @param string $msg The message template that should be used. If `null` is passed the attached `MessageLocator` |
||
225 | * will be called to retrieve a template based on the `$reason` parameter. |
||
226 | * @param int $reason The reason for this requirement. Please be aware: Setting this parameter will **not** |
||
227 | * override the required version |
||
228 | * @param array $data Additional data that should be passed to the message formatter. |
||
229 | * @return $this |
||
230 | */ |
||
231 | View Code Duplication | public function addArbitraryRequirement( |
|
243 | |||
244 | /** |
||
245 | * Add a requirement identified by reason id |
||
246 | 62 | * |
|
247 | 31 | * This method can be used to add a requirement that is identified by its reason id. |
|
248 | * |
||
249 | 58 | * @param int $reason The reason for this requirement. The required version is determined from this id. |
|
250 | * @param int $line The line that caused the requirement. |
||
251 | * @param string $msg The message template that should be used. If `null` is passed the attached `MessageLocator` |
||
252 | * will be called to retrieve a template based on the `$reason` parameter. |
||
253 | * @param array $data Additional data that should be passed to the message formatter. |
||
254 | * @return $this |
||
255 | * @throws \LogicException Thrown if the reason is unknown or does not have a version requirement associated. |
||
256 | */ |
||
257 | View Code Duplication | public function addRequirement($reason, $line = -1, $msg = null, array $data = []) |
|
271 | 2 | ||
272 | /** |
||
273 | * @param int $reason |
||
274 | 96 | * @param int $line |
|
275 | 48 | * @param null|string $msg |
|
276 | * @param array $data |
||
277 | 96 | * @return $this |
|
278 | */ |
||
279 | View Code Duplication | public function addLimit($reason, $line = -1, $msg = null, array $data = []) |
|
293 | 2 | ||
294 | /** |
||
295 | * @param string $version |
||
296 | 12 | * @param int $line |
|
297 | 6 | * @param null|string $msg |
|
298 | * @param int $reason |
||
299 | 12 | * @param array $data |
|
300 | * @return $this |
||
301 | */ |
||
302 | View Code Duplication | public function addArbitraryLimit( |
|
314 | |||
315 | /** |
||
316 | * @param int $type |
||
317 | 28 | * @param string $version |
|
318 | 14 | * @param int $line |
|
319 | * @param null|string $msg |
||
320 | 28 | * @param int $reason |
|
321 | * @param array $data |
||
322 | */ |
||
323 | protected function addArbitraryVersionConstraint( |
||
344 | 154 | ||
345 | /** |
||
346 | 154 | * @return bool |
|
347 | 136 | */ |
|
348 | 97 | public function isSealed() |
|
352 | |||
353 | /** |
||
354 | * @return array|\Pvra\Result\Reasoning[] |
||
355 | */ |
||
356 | 184 | public function getRequirements() |
|
360 | |||
361 | /** |
||
362 | * @return array|\Pvra\Result\Reasoning[] |
||
363 | */ |
||
364 | 148 | public function getLimits() |
|
368 | |||
369 | /** |
||
370 | * Get all reasonings related to a version |
||
371 | * |
||
372 | 114 | * If no reasoning for a version is known an empty array will be returned. |
|
373 | * |
||
374 | 114 | * @param string $version Version in the format `Major.Minor.Patch` |
|
375 | * @return array|Reasoning[] List of `Reasoning` or empty array |
||
376 | */ |
||
377 | public function getRequirementInfo($version) |
||
385 | 10 | ||
386 | /** |
||
387 | 10 | * @param string $version |
|
388 | 10 | * @return array|\Pvra\Result\Reasoning[] |
|
389 | */ |
||
390 | public function getLimitInfo($version) |
||
398 | 6 | ||
399 | /** |
||
400 | 6 | * Get the current analysis target id |
|
401 | 4 | * |
|
402 | * @return string Analysis target id |
||
403 | */ |
||
404 | 2 | public function getAnalysisTargetId() |
|
408 | |||
409 | /** |
||
410 | * @param string $analysisTargetId |
||
411 | * @return $this |
||
412 | 90 | */ |
|
413 | public function setAnalysisTargetId($analysisTargetId) |
||
423 | 82 | ||
424 | 4 | /** |
|
425 | * |
||
426 | */ |
||
427 | 80 | public function seal() |
|
431 | |||
432 | /** |
||
433 | * @inheritdoc |
||
434 | * @return \ArrayIterator|array|Reasoning[] |
||
435 | 56 | */ |
|
436 | public function getIterator() |
||
448 | 90 | ||
449 | 68 | /** |
|
450 | 68 | * @return \ArrayIterator |
|
451 | 34 | */ |
|
452 | 90 | View Code Duplication | public function getLimitIterator() |
466 | 2 | ||
467 | 2 | /** |
|
468 | 1 | * @return \ArrayIterator |
|
469 | 1 | */ |
|
470 | 1 | View Code Duplication | public function getRequirementIterator() |
484 | 6 | ||
485 | 6 | /** |
|
486 | 3 | * Number of registered reasonings |
|
487 | 3 | * |
|
488 | 3 | * @return int |
|
489 | */ |
||
490 | 6 | public function count() |
|
494 | |||
495 | /** |
||
496 | * Clear the cached max version requirements |
||
497 | */ |
||
498 | 104 | private function clearInstanceCaches() |
|
505 | } |
||
506 |