Complex classes like PropertyMetadata 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 PropertyMetadata, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class PropertyMetadata implements PropertyMetadataInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $name; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $alias; |
||
28 | |||
29 | /** |
||
30 | * @var TypeMetadataInterface|null |
||
31 | */ |
||
32 | private $type; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $readable = true; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | private $writable = true; |
||
43 | |||
44 | /** |
||
45 | * @var string|null |
||
46 | */ |
||
47 | private $accessor; |
||
48 | |||
49 | /** |
||
50 | * @var string|null |
||
51 | */ |
||
52 | private $mutator; |
||
53 | |||
54 | /** |
||
55 | * @var string|null |
||
56 | */ |
||
57 | private $since; |
||
58 | |||
59 | /** |
||
60 | * @var string|null |
||
61 | */ |
||
62 | private $until; |
||
63 | |||
64 | /** |
||
65 | * @var int|null |
||
66 | */ |
||
67 | private $maxDepth; |
||
68 | |||
69 | /** |
||
70 | * @var string[] |
||
71 | */ |
||
72 | private $groups = []; |
||
73 | |||
74 | /** |
||
75 | * @var bool |
||
76 | */ |
||
77 | 1230 | private $xmlAttribute = false; |
|
78 | |||
79 | 1230 | /** |
|
80 | 1230 | * @var bool |
|
81 | */ |
||
82 | private $xmlValue = false; |
||
83 | |||
84 | /** |
||
85 | 1065 | * @param string $name |
|
86 | */ |
||
87 | 1065 | public function __construct($name) |
|
88 | { |
||
89 | $this->setName($name); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | 1230 | * {@inheritdoc} |
|
94 | */ |
||
95 | 1230 | public function getName() |
|
99 | |||
100 | /** |
||
101 | 1029 | * {@inheritdoc} |
|
102 | */ |
||
103 | 1029 | public function setName($name) |
|
104 | { |
||
105 | $this->name = $name; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | 357 | * {@inheritdoc} |
|
110 | */ |
||
111 | 357 | public function hasAlias() |
|
112 | { |
||
113 | return $this->alias !== null; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | 114 | * {@inheritdoc} |
|
118 | */ |
||
119 | 114 | public function getAlias() |
|
123 | |||
124 | /** |
||
125 | 261 | * {@inheritdoc} |
|
126 | */ |
||
127 | 261 | public function setAlias($alias) |
|
128 | { |
||
129 | $this->alias = $alias; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | 819 | * {@inheritdoc} |
|
134 | */ |
||
135 | 819 | public function hasType() |
|
136 | { |
||
137 | return $this->type !== null; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | 336 | * {@inheritdoc} |
|
142 | */ |
||
143 | 336 | public function getType() |
|
147 | |||
148 | /** |
||
149 | 741 | * {@inheritdoc} |
|
150 | */ |
||
151 | 741 | public function setType(TypeMetadataInterface $type = null) |
|
152 | { |
||
153 | $this->type = $type; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | 1173 | * {@inheritdoc} |
|
158 | */ |
||
159 | 1173 | public function isReadable() |
|
163 | |||
164 | /** |
||
165 | 549 | * {@inheritdoc} |
|
166 | */ |
||
167 | 549 | public function setReadable($readable) |
|
168 | { |
||
169 | $this->readable = $readable; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | 1170 | * {@inheritdoc} |
|
174 | */ |
||
175 | 1170 | public function isWritable() |
|
179 | |||
180 | /** |
||
181 | 741 | * {@inheritdoc} |
|
182 | */ |
||
183 | 741 | public function setWritable($writable) |
|
184 | { |
||
185 | $this->writable = $writable; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | 273 | * {@inheritdoc} |
|
190 | */ |
||
191 | 273 | public function hasAccessor() |
|
192 | { |
||
193 | return $this->accessor !== null; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | 42 | * {@inheritdoc} |
|
198 | */ |
||
199 | 42 | public function getAccessor() |
|
203 | |||
204 | /** |
||
205 | 489 | * {@inheritdoc} |
|
206 | */ |
||
207 | 489 | public function setAccessor($accessor) |
|
208 | { |
||
209 | $this->accessor = $accessor; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | 273 | * {@inheritdoc} |
|
214 | */ |
||
215 | 273 | public function hasMutator() |
|
216 | { |
||
217 | return $this->mutator !== null; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | 42 | * {@inheritdoc} |
|
222 | */ |
||
223 | 42 | public function getMutator() |
|
227 | |||
228 | /** |
||
229 | 309 | * {@inheritdoc} |
|
230 | */ |
||
231 | 309 | public function setMutator($mutator) |
|
232 | { |
||
233 | $this->mutator = $mutator; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | 309 | * {@inheritdoc} |
|
238 | */ |
||
239 | 309 | public function hasSinceVersion() |
|
240 | { |
||
241 | return $this->since !== null; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | 90 | * {@inheritdoc} |
|
246 | */ |
||
247 | 90 | public function getSinceVersion() |
|
251 | |||
252 | /** |
||
253 | 309 | * {@inheritdoc} |
|
254 | */ |
||
255 | 309 | public function setSinceVersion($since) |
|
256 | { |
||
257 | $this->since = $since; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | 309 | * {@inheritdoc} |
|
262 | */ |
||
263 | 309 | public function hasUntilVersion() |
|
264 | { |
||
265 | return $this->until !== null; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | 90 | * {@inheritdoc} |
|
270 | */ |
||
271 | 90 | public function getUntilVersion() |
|
275 | |||
276 | /** |
||
277 | 285 | * {@inheritdoc} |
|
278 | */ |
||
279 | 285 | public function setUntilVersion($until) |
|
280 | { |
||
281 | $this->until = $until; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | 285 | * {@inheritdoc} |
|
286 | */ |
||
287 | 285 | public function hasMaxDepth() |
|
288 | { |
||
289 | return $this->maxDepth !== null; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | 66 | * {@inheritdoc} |
|
294 | */ |
||
295 | 66 | public function getMaxDepth() |
|
299 | |||
300 | /** |
||
301 | 267 | * {@inheritdoc} |
|
302 | */ |
||
303 | 267 | public function setMaxDepth($maxDepth) |
|
304 | { |
||
305 | $this->maxDepth = $maxDepth; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | 270 | * {@inheritdoc} |
|
310 | */ |
||
311 | 270 | public function hasGroups() |
|
312 | { |
||
313 | return !empty($this->groups); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | 9 | * {@inheritdoc} |
|
318 | */ |
||
319 | 9 | public function getGroups() |
|
320 | 9 | { |
|
321 | 9 | return array_keys($this->groups); |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * {@inheritdoc} |
||
326 | 9 | */ |
|
327 | public function setGroups(array $groups) |
||
332 | |||
333 | /** |
||
334 | * {@inheritdoc} |
||
335 | */ |
||
336 | 111 | public function addGroups(array $groups) |
|
337 | { |
||
338 | 111 | foreach ($groups as $group) { |
|
339 | $this->addGroup($group); |
||
340 | } |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | 111 | * {@inheritdoc} |
|
345 | */ |
||
346 | 111 | public function hasGroup($group) |
|
347 | 111 | { |
|
348 | 74 | return isset($this->groups[$group]); |
|
349 | 111 | } |
|
350 | |||
351 | /** |
||
352 | * {@inheritdoc} |
||
353 | */ |
||
354 | 3 | public function addGroup($group) |
|
355 | { |
||
356 | 3 | if (!$this->hasGroup($group)) { |
|
357 | 3 | $this->groups[$group] = true; |
|
358 | } |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | 3 | * {@inheritdoc} |
|
363 | */ |
||
364 | 3 | public function removeGroup($group) |
|
368 | 3 | ||
369 | 2 | /** |
|
370 | * {@inheritdoc} |
||
371 | 3 | */ |
|
372 | 3 | public function isXmlAttribute() |
|
373 | 2 | { |
|
374 | return $this->xmlAttribute; |
||
375 | 3 | } |
|
376 | 3 | ||
377 | 2 | /** |
|
378 | * {@inheritdoc} |
||
379 | 3 | */ |
|
380 | 3 | public function setXmlAttribute($xmlAttribute) |
|
384 | 3 | ||
385 | 2 | /** |
|
386 | * {@inheritdoc} |
||
387 | 3 | */ |
|
388 | 3 | public function isXmlValue() |
|
389 | 2 | { |
|
390 | return $this->xmlValue; |
||
391 | 3 | } |
|
392 | 3 | ||
393 | 2 | /** |
|
394 | * {@inheritdoc} |
||
395 | 3 | */ |
|
396 | 3 | public function setXmlValue($xmlValue) |
|
400 | |||
401 | /** |
||
402 | * {@inheritdoc} |
||
403 | 3 | */ |
|
404 | public function merge(PropertyMetadataInterface $propertyMetadata) |
||
405 | 3 | { |
|
406 | 3 | $this->setReadable($propertyMetadata->isReadable()); |
|
407 | 3 | $this->setWritable($propertyMetadata->isWritable()); |
|
408 | 3 | $this->setXmlAttribute($propertyMetadata->isXmlAttribute()); |
|
409 | 3 | $this->setXmlValue($propertyMetadata->isXmlValue()); |
|
410 | 3 | ||
411 | 3 | if ($propertyMetadata->hasAlias()) { |
|
412 | 3 | $this->setAlias($propertyMetadata->getAlias()); |
|
413 | 3 | } |
|
414 | 3 | ||
415 | 3 | if ($propertyMetadata->hasType()) { |
|
416 | 3 | $this->setType($propertyMetadata->getType()); |
|
417 | 2 | } |
|
418 | |||
419 | if ($propertyMetadata->hasAccessor()) { |
||
420 | $this->setAccessor($propertyMetadata->getAccessor()); |
||
421 | } |
||
422 | |||
423 | 3 | if ($propertyMetadata->hasMutator()) { |
|
424 | $this->setMutator($propertyMetadata->getMutator()); |
||
425 | } |
||
443 | |||
444 | /** |
||
445 | * {@inheritdoc} |
||
446 | */ |
||
447 | public function serialize() |
||
465 | |||
466 | /** |
||
467 | * {@inheritdoc} |
||
468 | */ |
||
469 | public function unserialize($serialized) |
||
487 | } |
||
488 |