Complex classes like PropertySpecificationLookup 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 PropertySpecificationLookup, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class PropertySpecificationLookup { |
||
21 | |||
22 | const POOLCACHE_ID = 'property.specification.lookup'; |
||
23 | |||
24 | /** |
||
25 | * @var CachedPropertyValuesPrefetcher |
||
26 | */ |
||
27 | private $cachedPropertyValuesPrefetcher; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $languageCode = 'en'; |
||
33 | |||
34 | /** |
||
35 | * @var Cache |
||
36 | */ |
||
37 | private $intermediaryMemoryCache; |
||
38 | |||
39 | /** |
||
40 | * @since 2.4 |
||
41 | * |
||
42 | * @param CachedPropertyValuesPrefetcher $cachedPropertyValuesPrefetcher |
||
43 | * @param Cache $intermediaryMemoryCache |
||
44 | */ |
||
45 | 243 | public function __construct( CachedPropertyValuesPrefetcher $cachedPropertyValuesPrefetcher, Cache $intermediaryMemoryCache ) { |
|
49 | |||
50 | /** |
||
51 | * @since 2.4 |
||
52 | */ |
||
53 | 158 | public function resetCacheBy( DIWikiPage $subject ) { |
|
56 | |||
57 | /** |
||
58 | * @since 2.4 |
||
59 | * |
||
60 | * @param string |
||
61 | */ |
||
62 | public function getLanguageCode() { |
||
65 | |||
66 | /** |
||
67 | * @since 2.4 |
||
68 | * |
||
69 | * @param string $languageCode |
||
70 | */ |
||
71 | 233 | public function setLanguageCode( $languageCode ) { |
|
74 | |||
75 | /** |
||
76 | * @since 2.5 |
||
77 | * |
||
78 | * @param DIProperty $property |
||
79 | * |
||
80 | * @return false|DataItem |
||
81 | */ |
||
82 | public function getFieldListBy( DIProperty $property ) { |
||
105 | |||
106 | /** |
||
107 | * @since 2.5 |
||
108 | * |
||
109 | * @param string $id |
||
110 | 1 | * @param string $languageCode |
|
111 | * |
||
112 | 1 | * @return string |
|
113 | */ |
||
114 | 1 | public function getPreferredPropertyLabelBy( $id, $languageCode = '' ) { |
|
133 | |||
134 | /** |
||
135 | * @since 2.4 |
||
136 | * |
||
137 | * @param string $displayTitle |
||
138 | * |
||
139 | * @return DIProperty|false |
||
140 | */ |
||
141 | public function getPropertyFromDisplayTitle( $displayTitle ) { |
||
168 | |||
169 | /** |
||
170 | * @since 2.4 |
||
171 | * |
||
172 | * @param DIProperty $property |
||
173 | * |
||
174 | * @return boolean |
||
175 | */ |
||
176 | 2 | public function hasUniquenessConstraintBy( DIProperty $property ) { |
|
199 | 198 | ||
200 | /** |
||
201 | 198 | * @since 2.5 |
|
202 | 198 | * |
|
203 | * @param DIProperty $property |
||
204 | * |
||
205 | 198 | * @return DataItem|null |
|
206 | 163 | */ |
|
207 | public function getExternalFormatterUriBy( DIProperty $property ) { |
||
222 | |||
223 | /** |
||
224 | * @since 2.4 |
||
225 | * |
||
226 | * @param DIProperty $property |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | 206 | public function getAllowedPatternBy( DIProperty $property ) { |
|
253 | |||
254 | /** |
||
255 | * @since 2.4 |
||
256 | * |
||
257 | * @param DIProperty $property |
||
258 | * |
||
259 | * @return integer|false |
||
260 | */ |
||
261 | 40 | public function getAllowedValuesBy( DIProperty $property ) { |
|
284 | |||
285 | 18 | /** |
|
286 | * @since 2.4 |
||
287 | 18 | * |
|
288 | * @param DIProperty $property |
||
289 | 18 | * |
|
290 | 18 | * @return integer|false |
|
291 | 18 | */ |
|
292 | public function getDisplayPrecisionBy( DIProperty $property ) { |
||
308 | |||
309 | /** |
||
310 | * @since 2.4 |
||
311 | * |
||
312 | * @param DIProperty $property |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | public function getDisplayUnitsBy( DIProperty $property ) { |
||
333 | |||
334 | 65 | /** |
|
335 | * We try to cache anything to avoid unnecessary store connections or DB |
||
336 | * lookups. For cases where a property was changed, the EventDipatcher will |
||
337 | * receive a 'property.specification.change' event (emitted as soon as the content of |
||
338 | * a property page was altered) with PropertySpecificationLookup::resetCacheBy |
||
339 | * being invoked to remove the cache entry for that specific property. |
||
340 | * |
||
341 | * @since 2.4 |
||
342 | 65 | * |
|
343 | 14 | * @param DIProperty $property |
|
344 | * @param mixed|null $linker |
||
345 | * @param string $languageCode |
||
346 | 65 | * |
|
347 | * @return string |
||
348 | 65 | */ |
|
349 | public function getPropertyDescriptionBy( DIProperty $property, $linker = null, $languageCode = '' ) { |
||
385 | 65 | ||
386 | 65 | private function getPredefinedPropertyDescription( $property, $linker, $languageCode ) { |
|
413 | |||
414 | 201 | private function findLocalPropertyDescription( $property, $linker, $languageCode ) { |
|
430 | |||
431 | private function findPreferredPropertyLabel( $property, $languageCode ) { |
||
447 | |||
448 | private function findTextValueByLanguage( $dataItems, $property, $languageCode ) { |
||
472 | |||
473 | } |
||
474 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: