Complex classes like Mapping 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 Mapping, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Mapping |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | private $className = ''; |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $resourceUrlPattern = ''; |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $classAlias = ''; |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $aliasedProperties = []; |
||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $hiddenProperties = []; |
||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $idProperties = []; |
||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $relationships = []; |
||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $metaData = []; |
||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $selfUrl = ''; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $otherUrls = []; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private $relationshipSelfUrl = []; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $filterKeys = []; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | private $curies = []; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | private $properties = []; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | private $includedKeys = []; |
||
75 | |||
76 | /** |
||
77 | * @var bool |
||
78 | */ |
||
79 | private $filteringIncluded = false; |
||
80 | |||
81 | /** |
||
82 | * @param $className |
||
83 | * @param null $resourceUrlPattern |
||
84 | * @param array $idProperties |
||
85 | */ |
||
86 | public function __construct($className, $resourceUrlPattern = null, array $idProperties = []) |
||
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getClassAlias() |
||
100 | |||
101 | /** |
||
102 | * @param string $aliasedClass |
||
103 | * |
||
104 | * @return $this |
||
105 | */ |
||
106 | public function setClassAlias($aliasedClass) |
||
114 | |||
115 | /** |
||
116 | * @return array |
||
117 | */ |
||
118 | public function getIdProperties() |
||
122 | |||
123 | /** |
||
124 | * @param $idProperty |
||
125 | */ |
||
126 | public function addIdProperty($idProperty) |
||
130 | |||
131 | /** |
||
132 | * @param string $propertyName |
||
133 | */ |
||
134 | public function hideProperty($propertyName) |
||
138 | |||
139 | /** |
||
140 | * @param $propertyName |
||
141 | * @param $propertyAlias |
||
142 | */ |
||
143 | public function addPropertyAlias($propertyName, $propertyAlias) |
||
149 | |||
150 | /** |
||
151 | * @param $propertyName |
||
152 | * @param $propertyAlias |
||
153 | */ |
||
154 | private function updatePropertyMappings($propertyName, $propertyAlias) |
||
168 | |||
169 | /** |
||
170 | * @param array $properties |
||
171 | */ |
||
172 | public function setPropertyNameAliases(array $properties) |
||
180 | |||
181 | /** |
||
182 | * @param $properties |
||
183 | */ |
||
184 | public function setProperties(array $properties) |
||
188 | |||
189 | /** |
||
190 | * @return array |
||
191 | */ |
||
192 | public function getProperties() |
||
196 | |||
197 | /** |
||
198 | * @return mixed |
||
199 | */ |
||
200 | public function getClassName() |
||
204 | |||
205 | /** |
||
206 | */ |
||
207 | public function getResourceUrl() |
||
211 | |||
212 | /** |
||
213 | * @return array |
||
214 | */ |
||
215 | public function getAliasedProperties() |
||
219 | |||
220 | /** |
||
221 | * @return array |
||
222 | */ |
||
223 | public function getHiddenProperties() |
||
227 | |||
228 | /** |
||
229 | * @param array $hidden |
||
230 | */ |
||
231 | public function setHiddenProperties(array $hidden) |
||
235 | |||
236 | /** |
||
237 | * @return array |
||
238 | */ |
||
239 | public function getRelationships() |
||
243 | |||
244 | /** |
||
245 | * @param array $relationships |
||
246 | */ |
||
247 | public function addAdditionalRelationships(array $relationships) |
||
251 | |||
252 | /** |
||
253 | * @return array |
||
254 | */ |
||
255 | public function getMetaData() |
||
259 | |||
260 | /** |
||
261 | * @param array $metaData |
||
262 | */ |
||
263 | public function setMetaData(array $metaData) |
||
267 | |||
268 | /** |
||
269 | * @param string $key |
||
270 | * @param $value |
||
271 | */ |
||
272 | public function addMetaData($key, $value) |
||
276 | |||
277 | /** |
||
278 | * @return string |
||
279 | */ |
||
280 | public function getSelfUrl() |
||
284 | |||
285 | /** |
||
286 | * @param string $self |
||
287 | * |
||
288 | * @throws \InvalidArgumentException |
||
289 | */ |
||
290 | public function setSelfUrl($self) |
||
294 | |||
295 | /** |
||
296 | * @param $propertyName |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | public function getRelatedUrl($propertyName) |
||
306 | |||
307 | /** |
||
308 | * @param array $filterKeys |
||
309 | */ |
||
310 | public function setFilterKeys(array $filterKeys) |
||
314 | |||
315 | /** |
||
316 | * @return array |
||
317 | */ |
||
318 | public function getFilterKeys() |
||
322 | |||
323 | /** |
||
324 | * @param string $propertyName |
||
325 | * @param string $urls |
||
326 | * |
||
327 | * @return $this |
||
328 | */ |
||
329 | public function setRelationshipUrls($propertyName, $urls) |
||
335 | |||
336 | /** |
||
337 | * @param $propertyName |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | public function getRelationshipSelfUrl($propertyName) |
||
347 | |||
348 | /** |
||
349 | * @param array $urls |
||
350 | */ |
||
351 | public function setUrls(array $urls) |
||
355 | |||
356 | /** |
||
357 | * @return array |
||
358 | */ |
||
359 | public function getUrls() |
||
363 | |||
364 | /** |
||
365 | * @param array $curies |
||
366 | * |
||
367 | * @throws MappingException |
||
368 | */ |
||
369 | public function setCuries(array $curies) |
||
377 | |||
378 | /** |
||
379 | * @return array |
||
380 | */ |
||
381 | public function getCuries() |
||
385 | |||
386 | |||
387 | /** |
||
388 | * Used by JSON API included resource filtering. |
||
389 | * |
||
390 | * @param $resource |
||
391 | */ |
||
392 | public function addIncludedResource($resource) { |
||
396 | |||
397 | /** |
||
398 | * Returns the allowed included resources. |
||
399 | * |
||
400 | * @return array |
||
401 | */ |
||
402 | public function getIncludedResources() |
||
406 | |||
407 | |||
408 | /** |
||
409 | * @param bool $filtering |
||
410 | */ |
||
411 | public function filteringIncludedResources($filtering = true) |
||
415 | |||
416 | /** |
||
417 | * Returns true if included resource filtering has been set, false otherwise. |
||
418 | * |
||
419 | * @return bool |
||
420 | */ |
||
421 | public function isFilteringIncludedResources() |
||
425 | } |
||
426 |