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 CachingResultsBuilder 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 CachingResultsBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class CachingResultsBuilder implements ResultsBuilder { |
||
40 | |||
41 | /** |
||
42 | * @var ResultsBuilder |
||
43 | */ |
||
44 | private $resultsBuilder; |
||
45 | |||
46 | /** |
||
47 | * @var ResultsCache |
||
48 | */ |
||
49 | private $cache; |
||
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 ResultsBuilder $resultsBuilder The ResultsBuilder that cache misses are delegated to. |
||
102 | * @param ResultsCache $cache The cache where results can be stored. |
||
103 | * @param WikiPageEntityMetaDataAccessor $wikiPageEntityMetaDataAccessor Used to get the latest revision ID. |
||
104 | * @param EntityIdParser $entityIdParser Used to parse entity IDs in cached objects. |
||
105 | * @param int $ttlInSeconds Time-to-live of the cached values, in seconds. |
||
106 | * @param string[] $possiblyStaleConstraintTypes item IDs of constraint types |
||
107 | * where cached results may always be stale, regardless of invalidation logic |
||
108 | * @param int $maxRevisionIds The maximum number of revision IDs to check; |
||
109 | * if a check result depends on more entity IDs than this number, it is not cached. |
||
110 | * @param LoggingHelper $loggingHelper |
||
111 | */ |
||
112 | public function __construct( |
||
138 | |||
139 | /** |
||
140 | * @param EntityId[] $entityIds |
||
141 | * @param string[] $claimIds |
||
142 | * @param string[]|null $constraintIds |
||
143 | * @param string[] $statuses |
||
144 | * @return CachedCheckConstraintsResponse |
||
145 | */ |
||
146 | public function getResults( |
||
180 | |||
181 | /** |
||
182 | * We can only use cached constraint results |
||
183 | * if exactly the problematic results of a full constraint check were requested: |
||
184 | * constraint checks for the full entity (not just individual statements), |
||
185 | * without restricting the set of constraints to check, |
||
186 | * and with exactly the 'violation', 'warning' and 'bad-parameters' statuses. |
||
187 | * |
||
188 | * (In theory, we could also use results for requests |
||
189 | * that asked for a subset of these result statuses, |
||
190 | * but removing the extra results from the cached value is tricky, |
||
191 | * especially if you consider that they might have added qualifier contexts to the output |
||
192 | * that should not only be empty, but completely absent.) |
||
193 | * |
||
194 | * @param EntityId[] $entityIds |
||
195 | * @param string[] $claimIds |
||
196 | * @param string[]|null $constraintIds |
||
197 | * @param string[] $statuses |
||
198 | * @return bool |
||
199 | */ |
||
200 | View Code Duplication | private function canUseStoredResults( |
|
217 | |||
218 | /** |
||
219 | * @param EntityId[] $entityIds |
||
220 | * @param string[] $claimIds |
||
221 | * @param string[]|null $constraintIds |
||
222 | * @param string[] $statuses |
||
223 | * @return CachedCheckConstraintsResponse |
||
224 | */ |
||
225 | public function getAndStoreResults( |
||
255 | |||
256 | /** |
||
257 | * We can only store constraint results |
||
258 | * if the set of constraints to check was not restricted |
||
259 | * and exactly the problematic results were requested. |
||
260 | * However, it doesn’t matter whether constraint checks on individual statements were requested: |
||
261 | * we only store results for the mentioned entity IDs, |
||
262 | * and those will be complete regardless of what’s in the statement IDs. |
||
263 | * |
||
264 | * (In theory, we could also store results of checks that requested extra result statuses, |
||
265 | * but removing the extra results before caching the value is tricky, |
||
266 | * especially if you consider that they might have added qualifier contexts to the output |
||
267 | * that should not only be empty, but completely absent.) |
||
268 | * |
||
269 | * @param EntityId[] $entityIds |
||
270 | * @param string[] $claimIds |
||
271 | * @param string[]|null $constraintIds |
||
272 | * @param string[] $statuses |
||
273 | * @return bool |
||
274 | */ |
||
275 | View Code Duplication | private function canStoreResults( |
|
289 | |||
290 | /** |
||
291 | * @param EntityId $entityId |
||
292 | * @return CachedCheckConstraintsResponse|null |
||
293 | */ |
||
294 | public function getStoredResults( |
||
338 | |||
339 | /** |
||
340 | * @param CachingMetadata $cachingMetadata |
||
341 | * @param EntityId[] $dependedEntityIds |
||
342 | * @param DependencyMetadata|null $futureTimeDependencyMetadata |
||
343 | * @return Metadata |
||
344 | */ |
||
345 | private function mergeStoredMetadata( |
||
364 | |||
365 | /** |
||
366 | * @param EntityId[] $entityIds |
||
367 | * @return int[]|null array from entity ID serializations to revision ID, |
||
368 | * or null to indicate that not all revision IDs could be loaded |
||
369 | */ |
||
370 | private function getLatestRevisionIds( array $entityIds ) { |
||
390 | |||
391 | /** |
||
392 | * @param array $array |
||
393 | * @return bool |
||
394 | */ |
||
395 | private function hasFalseElements( array $array ) { |
||
398 | |||
399 | public function updateCachingMetadata( &$element, $key, CachingMetadata $cachingMetadata ) { |
||
422 | |||
423 | /** |
||
424 | * Set a custom function to get the current time, instead of microtime(). |
||
425 | * |
||
426 | * @param callable $microtime |
||
427 | */ |
||
428 | public function setMicrotimeFunction( callable $microtime ) { |
||
431 | |||
432 | } |
||
433 |
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..