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 CachingResultsSource 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 CachingResultsSource, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class CachingResultsSource implements ResultsSource { |
||
30 | |||
31 | /** |
||
32 | * @var ResultsSource |
||
33 | */ |
||
34 | private $resultsSource; |
||
35 | |||
36 | /** |
||
37 | * @var ResultsCache |
||
38 | */ |
||
39 | private $cache; |
||
40 | |||
41 | /** |
||
42 | * @var CheckResultSerializer |
||
43 | */ |
||
44 | private $checkResultSerializer; |
||
45 | |||
46 | /** |
||
47 | * @var CheckResultDeserializer |
||
48 | */ |
||
49 | private $checkResultDeserializer; |
||
50 | |||
51 | /** |
||
52 | * @var WikiPageEntityMetaDataAccessor |
||
53 | */ |
||
54 | private $wikiPageEntityMetaDataAccessor; |
||
55 | |||
56 | /** |
||
57 | * @var EntityIdParser |
||
58 | */ |
||
59 | private $entityIdParser; |
||
60 | |||
61 | /** |
||
62 | * @var int |
||
63 | */ |
||
64 | private $ttlInSeconds; |
||
65 | |||
66 | /** |
||
67 | * @var string[] |
||
68 | */ |
||
69 | private $possiblyStaleConstraintTypes; |
||
70 | |||
71 | /** |
||
72 | * @var int |
||
73 | */ |
||
74 | private $maxRevisionIds; |
||
75 | |||
76 | /** |
||
77 | * @var LoggingHelper |
||
78 | */ |
||
79 | private $loggingHelper; |
||
80 | |||
81 | /** |
||
82 | * @var TimeValueComparer |
||
83 | */ |
||
84 | private $timeValueComparer; |
||
85 | |||
86 | /** |
||
87 | * @var callable |
||
88 | */ |
||
89 | private $microtime = 'microtime'; |
||
90 | |||
91 | /** |
||
92 | * TODO: In PHP 5.6, make this a public class constant instead, |
||
93 | * and also use it in CheckConstraints::getAllowedParams() |
||
94 | * and in some of the tests. |
||
95 | * |
||
96 | * @var string[] |
||
97 | */ |
||
98 | private $cachedStatuses; |
||
99 | |||
100 | /** |
||
101 | * @param ResultsSource $resultsSource The ResultsSource that cache misses are delegated to. |
||
102 | * @param ResultsCache $cache The cache where results can be stored. |
||
103 | * @param CheckResultSerializer $checkResultSerializer Used to serialize check results. |
||
104 | * @param CheckResultDeserializer $checkResultDeserializer Used to deserialize check results. |
||
105 | * @param WikiPageEntityMetaDataAccessor $wikiPageEntityMetaDataAccessor Used to get the latest revision ID. |
||
106 | * @param EntityIdParser $entityIdParser Used to parse entity IDs in cached objects. |
||
107 | * @param int $ttlInSeconds Time-to-live of the cached values, in seconds. |
||
108 | * @param string[] $possiblyStaleConstraintTypes item IDs of constraint types |
||
109 | * where cached results may always be stale, regardless of invalidation logic |
||
110 | * @param int $maxRevisionIds The maximum number of revision IDs to check; |
||
111 | * if a check result depends on more entity IDs than this number, it is not cached. |
||
112 | * @param LoggingHelper $loggingHelper |
||
113 | */ |
||
114 | public function __construct( |
||
144 | |||
145 | public function getResults( |
||
183 | |||
184 | /** |
||
185 | * We can only use cached constraint results |
||
186 | * if nothing more than the problematic results of a full constraint check were requested: |
||
187 | * constraint checks for the full entity (not just individual statements), |
||
188 | * without restricting the set of constraints to check, |
||
189 | * and with no statuses other than 'violation', 'warning' and 'bad-parameters'. |
||
190 | * |
||
191 | * @param EntityId[] $entityIds |
||
192 | * @param string[] $claimIds |
||
193 | * @param string[]|null $constraintIds |
||
194 | * @param string[] $statuses |
||
195 | * @return bool |
||
196 | */ |
||
197 | View Code Duplication | private function canUseStoredResults( |
|
214 | |||
215 | /** |
||
216 | * @param EntityId[] $entityIds |
||
217 | * @param string[] $claimIds |
||
218 | * @param string[]|null $constraintIds |
||
219 | * @param string[] $statuses |
||
220 | * @return CachedCheckResults |
||
221 | */ |
||
222 | public function getAndStoreResults( |
||
238 | |||
239 | /** |
||
240 | * We can only store constraint results |
||
241 | * if the set of constraints to check was not restricted |
||
242 | * and all the problematic results were requested. |
||
243 | * However, it doesn’t matter whether constraint checks on individual statements were requested: |
||
244 | * we only store results for the mentioned entity IDs, |
||
245 | * and those will be complete regardless of what’s in the statement IDs. |
||
246 | * And it also doesn’t matter whether the set of statuses requested |
||
247 | * was exactly the statuses we cache or a superset of it: |
||
248 | * as long as all the results we want to cache are there, |
||
249 | * we can filter out the extraneous ones before we serialize them. |
||
250 | * |
||
251 | * @param EntityId[] $entityIds |
||
252 | * @param string[] $claimIds |
||
253 | * @param string[]|null $constraintIds |
||
254 | * @param string[] $statuses |
||
255 | * @return bool |
||
256 | */ |
||
257 | View Code Duplication | private function canStoreResults( |
|
271 | |||
272 | /** |
||
273 | * Store check results for the given entity ID in the cache, if possible. |
||
274 | * |
||
275 | * @param EntityId $entityId The entity ID. |
||
276 | * @param CachedCheckResults $results A collection of check results with metadata. |
||
277 | * May include check results for other entity IDs as well, |
||
278 | * or check results with statuses that we’re not interested in caching. |
||
279 | */ |
||
280 | private function storeResults( EntityId $entityId, CachedCheckResults $results ) { |
||
310 | |||
311 | /** |
||
312 | * @param EntityId $entityId |
||
313 | * @return CachedCheckResults|null |
||
314 | */ |
||
315 | public function getStoredResults( |
||
344 | |||
345 | /** |
||
346 | * Extract the dependency metadata of $value |
||
347 | * and check that the dependency metadata does not indicate staleness. |
||
348 | * |
||
349 | * @param array|bool $value |
||
350 | * @return DependencyMetadata|null the dependency metadata, |
||
351 | * or null if $value should no longer be used |
||
352 | */ |
||
353 | private function checkDependencyMetadata( $value ) { |
||
388 | |||
389 | /** |
||
390 | * Deserialize a check result. |
||
391 | * If the result might be stale after caching |
||
392 | * (because its dependencies cannot be fully tracked in its dependency metadata), |
||
393 | * also add $cachingMetadata to it. |
||
394 | * |
||
395 | * @param array $resultSerialization |
||
396 | * @param CachingMetadata $cachingMetadata |
||
397 | * @return CheckResult |
||
398 | */ |
||
399 | private function deserializeCheckResult( |
||
414 | |||
415 | /** |
||
416 | * @param CheckResult $result |
||
417 | * @return bool |
||
418 | */ |
||
419 | private function isPossiblyStaleResult( CheckResult $result ) { |
||
425 | |||
426 | /** |
||
427 | * @param EntityId[] $entityIds |
||
428 | * @return int[]|null array from entity ID serializations to revision ID, |
||
429 | * or null to indicate that not all revision IDs could be loaded |
||
430 | */ |
||
431 | View Code Duplication | private function getLatestRevisionIds( array $entityIds ) { |
|
451 | |||
452 | /** |
||
453 | * @param array $array |
||
454 | * @return bool |
||
455 | */ |
||
456 | private function hasFalseElements( array $array ) { |
||
459 | |||
460 | /** |
||
461 | * Set a custom function to get the current time, instead of microtime(). |
||
462 | * |
||
463 | * @param callable $microtime |
||
464 | */ |
||
465 | public function setMicrotimeFunction( callable $microtime ) { |
||
468 | |||
469 | } |
||
470 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..