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 |
||
30 | class CachingResultsSource implements ResultsSource { |
||
31 | |||
32 | /** |
||
33 | * @var ResultsSource |
||
34 | */ |
||
35 | private $resultsSource; |
||
36 | |||
37 | /** |
||
38 | * @var ResultsCache |
||
39 | */ |
||
40 | private $cache; |
||
41 | |||
42 | /** |
||
43 | * @var CheckResultSerializer |
||
44 | */ |
||
45 | private $checkResultSerializer; |
||
46 | |||
47 | /** |
||
48 | * @var CheckResultDeserializer |
||
49 | */ |
||
50 | private $checkResultDeserializer; |
||
51 | |||
52 | /** |
||
53 | * @var WikiPageEntityMetaDataAccessor |
||
54 | */ |
||
55 | private $wikiPageEntityMetaDataAccessor; |
||
56 | |||
57 | /** |
||
58 | * @var EntityIdParser |
||
59 | */ |
||
60 | private $entityIdParser; |
||
61 | |||
62 | /** |
||
63 | * @var int |
||
64 | */ |
||
65 | private $ttlInSeconds; |
||
66 | |||
67 | /** |
||
68 | * @var string[] |
||
69 | */ |
||
70 | private $possiblyStaleConstraintTypes; |
||
71 | |||
72 | /** |
||
73 | * @var int |
||
74 | */ |
||
75 | private $maxRevisionIds; |
||
76 | |||
77 | /** |
||
78 | * @var LoggingHelper |
||
79 | */ |
||
80 | private $loggingHelper; |
||
81 | |||
82 | /** |
||
83 | * @var TimeValueComparer |
||
84 | */ |
||
85 | private $timeValueComparer; |
||
86 | |||
87 | /** |
||
88 | * @var callable |
||
89 | */ |
||
90 | private $microtime = 'microtime'; |
||
91 | |||
92 | /** |
||
93 | * TODO: In PHP 5.6, make this a public class constant instead, |
||
94 | * and also use it in CheckConstraints::getAllowedParams() |
||
95 | * and in some of the tests. |
||
96 | * |
||
97 | * @var string[] |
||
98 | */ |
||
99 | private $cachedStatuses; |
||
100 | |||
101 | private $cachedStatusesFlipped; |
||
102 | |||
103 | /** |
||
104 | * @param ResultsSource $resultsSource The ResultsSource that cache misses are delegated to. |
||
105 | * @param ResultsCache $cache The cache where results can be stored. |
||
106 | * @param CheckResultSerializer $checkResultSerializer Used to serialize check results. |
||
107 | * @param CheckResultDeserializer $checkResultDeserializer Used to deserialize check results. |
||
108 | * @param WikiPageEntityMetaDataAccessor $wikiPageEntityMetaDataAccessor Used to get the latest revision ID. |
||
109 | * @param EntityIdParser $entityIdParser Used to parse entity IDs in cached objects. |
||
110 | * @param int $ttlInSeconds Time-to-live of the cached values, in seconds. |
||
111 | * @param string[] $possiblyStaleConstraintTypes item IDs of constraint types |
||
112 | * where cached results may always be stale, regardless of invalidation logic |
||
113 | * @param int $maxRevisionIds The maximum number of revision IDs to check; |
||
114 | * if a check result depends on more entity IDs than this number, it is not cached. |
||
115 | * @param LoggingHelper $loggingHelper |
||
116 | */ |
||
117 | public function __construct( |
||
148 | |||
149 | public function getResults( |
||
188 | |||
189 | /** |
||
190 | * We can only use cached constraint results |
||
191 | * if nothing more than the problematic results of a full constraint check were requested: |
||
192 | * constraint checks for the full entity (not just individual statements), |
||
193 | * without restricting the set of constraints to check, |
||
194 | * and with no statuses other than 'violation', 'warning' and 'bad-parameters'. |
||
195 | * |
||
196 | * @param EntityId[] $entityIds |
||
197 | * @param string[] $claimIds |
||
198 | * @param string[]|null $constraintIds |
||
199 | * @param string[] $statuses |
||
200 | * @return bool |
||
201 | */ |
||
202 | View Code Duplication | private function canUseStoredResults( |
|
219 | |||
220 | /** |
||
221 | * Check whether a check result should be used, |
||
222 | * either because it has the right status |
||
223 | * or because it is a NullResult whose metadata should be preserved. |
||
224 | * |
||
225 | * @param string[] $statusesFlipped |
||
226 | * @param CheckResult $result |
||
227 | * @return bool |
||
228 | */ |
||
229 | private function statusSelected( array $statusesFlipped, CheckResult $result ) { |
||
233 | |||
234 | /** |
||
235 | * @param EntityId[] $entityIds |
||
236 | * @param string[] $claimIds |
||
237 | * @param string[]|null $constraintIds |
||
238 | * @param string[] $statuses |
||
239 | * @return CachedCheckResults |
||
240 | */ |
||
241 | public function getAndStoreResults( |
||
257 | |||
258 | /** |
||
259 | * We can only store constraint results |
||
260 | * if the set of constraints to check was not restricted |
||
261 | * and all the problematic results were requested. |
||
262 | * However, it doesn’t matter whether constraint checks on individual statements were requested: |
||
263 | * we only store results for the mentioned entity IDs, |
||
264 | * and those will be complete regardless of what’s in the statement IDs. |
||
265 | * And it also doesn’t matter whether the set of statuses requested |
||
266 | * was exactly the statuses we cache or a superset of it: |
||
267 | * as long as all the results we want to cache are there, |
||
268 | * we can filter out the extraneous ones before we serialize them. |
||
269 | * |
||
270 | * @param EntityId[] $entityIds |
||
271 | * @param string[] $claimIds |
||
272 | * @param string[]|null $constraintIds |
||
273 | * @param string[] $statuses |
||
274 | * @return bool |
||
275 | */ |
||
276 | View Code Duplication | private function canStoreResults( |
|
290 | |||
291 | /** |
||
292 | * Store check results for the given entity ID in the cache, if possible. |
||
293 | * |
||
294 | * @param EntityId $entityId The entity ID. |
||
295 | * @param CachedCheckResults $results A collection of check results with metadata. |
||
296 | * May include check results for other entity IDs as well, |
||
297 | * or check results with statuses that we’re not interested in caching. |
||
298 | */ |
||
299 | private function storeResults( EntityId $entityId, CachedCheckResults $results ) { |
||
328 | |||
329 | /** |
||
330 | * @param EntityId $entityId |
||
331 | * @param int $forRevision Requested revision of $entityId |
||
332 | * If this parameter is not zero, the results are returned if this is the latest revision, |
||
333 | * otherwise null is returned, since we can't get constraints for past revisions. |
||
334 | * @return CachedCheckResults|null |
||
335 | */ |
||
336 | public function getStoredResults( |
||
367 | |||
368 | /** |
||
369 | * Extract the dependency metadata of $value |
||
370 | * and check that the dependency metadata does not indicate staleness. |
||
371 | * |
||
372 | * @param array|false $value |
||
373 | * @param int[] $paramRevs Revisions from parameters, id => revision |
||
374 | * These revisions are used instead of ones recorded in the metadata, |
||
375 | * so we can serve requests specifying concrete revisions, and if they are not latest, |
||
376 | * we will reject then. |
||
377 | * @return DependencyMetadata|null the dependency metadata, |
||
378 | * or null if $value should no longer be used |
||
379 | */ |
||
380 | private function checkDependencyMetadata( $value, $paramRevs ) { |
||
421 | |||
422 | /** |
||
423 | * Deserialize a check result. |
||
424 | * If the result might be stale after caching |
||
425 | * (because its dependencies cannot be fully tracked in its dependency metadata), |
||
426 | * also add $cachingMetadata to it. |
||
427 | * |
||
428 | * @param array $resultSerialization |
||
429 | * @param CachingMetadata $cachingMetadata |
||
430 | * @return CheckResult |
||
431 | */ |
||
432 | private function deserializeCheckResult( |
||
447 | |||
448 | /** |
||
449 | * @param CheckResult $result |
||
450 | * @return bool |
||
451 | */ |
||
452 | private function isPossiblyStaleResult( CheckResult $result ) { |
||
462 | |||
463 | /** |
||
464 | * @param EntityId[] $entityIds |
||
465 | * @return int[]|null array from entity ID serializations to revision ID, |
||
466 | * or null to indicate that not all revision IDs could be loaded |
||
467 | */ |
||
468 | private function getLatestRevisionIds( array $entityIds ) { |
||
488 | |||
489 | /** |
||
490 | * @param array $array |
||
491 | * @return bool |
||
492 | */ |
||
493 | private function hasFalseElements( array $array ) { |
||
496 | |||
497 | /** |
||
498 | * Set a custom function to get the current time, instead of microtime(). |
||
499 | * |
||
500 | * @param callable $microtime |
||
501 | */ |
||
502 | public function setMicrotimeFunction( callable $microtime ) { |
||
505 | |||
506 | } |
||
507 |
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..