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 $included = []; |
||
75 | |||
76 | /** |
||
77 | * @param $className |
||
78 | * @param null $resourceUrlPattern |
||
79 | * @param array $idProperties |
||
80 | */ |
||
81 | public function __construct($className, $resourceUrlPattern = null, array $idProperties = []) |
||
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getClassAlias() |
||
95 | |||
96 | /** |
||
97 | * @param string $aliasedClass |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function setClassAlias($aliasedClass) |
||
109 | |||
110 | /** |
||
111 | * @return array |
||
112 | */ |
||
113 | public function getIdProperties() |
||
117 | |||
118 | /** |
||
119 | * @param $idProperty |
||
120 | */ |
||
121 | public function addIdProperty($idProperty) |
||
125 | |||
126 | /** |
||
127 | * @param string $propertyName |
||
128 | */ |
||
129 | public function hideProperty($propertyName) |
||
133 | |||
134 | /** |
||
135 | * @param $propertyName |
||
136 | * @param $propertyAlias |
||
137 | */ |
||
138 | public function addPropertyAlias($propertyName, $propertyAlias) |
||
144 | |||
145 | /** |
||
146 | * @param $propertyName |
||
147 | * @param $propertyAlias |
||
148 | */ |
||
149 | private function updatePropertyMappings($propertyName, $propertyAlias) |
||
163 | |||
164 | /** |
||
165 | * @param array $properties |
||
166 | */ |
||
167 | public function setPropertyNameAliases(array $properties) |
||
175 | |||
176 | /** |
||
177 | * @param $properties |
||
178 | */ |
||
179 | public function setProperties(array $properties) |
||
183 | |||
184 | /** |
||
185 | * @return array |
||
186 | */ |
||
187 | public function getProperties() |
||
191 | |||
192 | /** |
||
193 | * @return mixed |
||
194 | */ |
||
195 | public function getClassName() |
||
199 | |||
200 | /** |
||
201 | */ |
||
202 | public function getResourceUrl() |
||
206 | |||
207 | /** |
||
208 | * @return array |
||
209 | */ |
||
210 | public function getAliasedProperties() |
||
214 | |||
215 | /** |
||
216 | * @return array |
||
217 | */ |
||
218 | public function getHiddenProperties() |
||
222 | |||
223 | /** |
||
224 | * @param array $hidden |
||
225 | */ |
||
226 | public function setHiddenProperties(array $hidden) |
||
230 | |||
231 | /** |
||
232 | * @return array |
||
233 | */ |
||
234 | public function getRelationships() |
||
238 | |||
239 | /** |
||
240 | * @param array $relationships |
||
241 | */ |
||
242 | public function addAdditionalRelationships(array $relationships) |
||
246 | |||
247 | /** |
||
248 | * @return array |
||
249 | */ |
||
250 | public function getMetaData() |
||
254 | |||
255 | /** |
||
256 | * @param array $metaData |
||
257 | */ |
||
258 | public function setMetaData(array $metaData) |
||
262 | |||
263 | /** |
||
264 | * @param string $key |
||
265 | * @param $value |
||
266 | */ |
||
267 | public function addMetaData($key, $value) |
||
271 | |||
272 | /** |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getSelfUrl() |
||
279 | |||
280 | /** |
||
281 | * @param string $self |
||
282 | * |
||
283 | * @throws \InvalidArgumentException |
||
284 | */ |
||
285 | public function setSelfUrl($self) |
||
289 | |||
290 | /** |
||
291 | * @param $propertyName |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | public function getRelatedUrl($propertyName) |
||
301 | |||
302 | /** |
||
303 | * @param array $filterKeys |
||
304 | */ |
||
305 | public function setFilterKeys(array $filterKeys) |
||
309 | |||
310 | /** |
||
311 | * @return array |
||
312 | */ |
||
313 | public function getFilterKeys() |
||
317 | |||
318 | /** |
||
319 | * @param string $propertyName |
||
320 | * @param string $urls |
||
321 | * |
||
322 | * @return $this |
||
323 | */ |
||
324 | public function setRelationshipUrls($propertyName, $urls) |
||
330 | |||
331 | /** |
||
332 | * @param $propertyName |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | public function getRelationshipSelfUrl($propertyName) |
||
342 | |||
343 | /** |
||
344 | * @param array $urls |
||
345 | */ |
||
346 | public function setUrls(array $urls) |
||
350 | |||
351 | /** |
||
352 | * @return array |
||
353 | */ |
||
354 | public function getUrls() |
||
358 | |||
359 | /** |
||
360 | * @param array $curies |
||
361 | * |
||
362 | * @throws MappingException |
||
363 | */ |
||
364 | public function setCuries(array $curies) |
||
372 | |||
373 | /** |
||
374 | * @return array |
||
375 | */ |
||
376 | public function getCuries() |
||
380 | |||
381 | /** |
||
382 | * Used by JSON API included resource filtering. |
||
383 | * |
||
384 | * @param $resource |
||
385 | */ |
||
386 | public function addIncludedResource($resource) |
||
390 | |||
391 | /** |
||
392 | * Returns the allowed included resources. |
||
393 | * |
||
394 | * @return array |
||
395 | */ |
||
396 | public function getIncludedResources() |
||
400 | |||
401 | /** |
||
402 | * Returns true if included resource filtering has been set, false otherwise. |
||
403 | * |
||
404 | * @return bool |
||
405 | */ |
||
406 | public function isFilteringIncludedResources() |
||
410 | } |
||
411 |