Complex classes like PropertyManager 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 PropertyManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class PropertyManager implements RenderableInterface, MultilineCommentableInterface |
||
25 | { |
||
26 | |||
27 | use MultilineCommentTrait; |
||
28 | use TemplateTrait; |
||
29 | |||
30 | /** |
||
31 | * Property predefined types |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | const TYPE_BOOLEAN = 'boolean'; |
||
36 | const TYPE_ARRAY_COLLECTION = 'Doctrine\Common\Collections\ArrayCollection'; |
||
37 | |||
38 | /** |
||
39 | * Comment of property |
||
40 | * |
||
41 | * @Type("string") |
||
42 | */ |
||
43 | private $comment = ''; |
||
44 | |||
45 | /** |
||
46 | * Collection of Constraints |
||
47 | * |
||
48 | * @Type("Doctrine\Common\Collections\ArrayCollection<string>") |
||
49 | */ |
||
50 | private $constraints = null; |
||
51 | |||
52 | /** |
||
53 | * Property name |
||
54 | * |
||
55 | * @Type("string") |
||
56 | * @Assert\NotBlank(message="Property name can not be empty!") |
||
57 | */ |
||
58 | private $name = ''; |
||
59 | |||
60 | /** |
||
61 | * Property type |
||
62 | * |
||
63 | * @Type("string") |
||
64 | * @Assert\NotBlank(message="Property type can not be empty!") |
||
65 | */ |
||
66 | private $type = ''; |
||
67 | |||
68 | /** |
||
69 | * Property serialized name, default is property name in snake format eg. for property $lastPost => last_post |
||
70 | * |
||
71 | * @Type("string") |
||
72 | * @Assert\Type("string") |
||
73 | */ |
||
74 | private $serializedName = ''; |
||
75 | |||
76 | /** |
||
77 | * @Type("boolean") |
||
78 | * @Assert\Type("boolean") |
||
79 | */ |
||
80 | private $optional = false; |
||
81 | |||
82 | /** |
||
83 | * @Type("string") |
||
84 | * @Assert\Type("string") |
||
85 | * @SerializedName("property_manager_template_path") |
||
86 | */ |
||
87 | private $propertyManagerTemplatePath = ""; |
||
88 | |||
89 | /** |
||
90 | * @Type("string") |
||
91 | * @Assert\Type("string") |
||
92 | * @SerializedName("test_class_method_manager_template_path") |
||
93 | */ |
||
94 | private $testClassMethodManagerTemplatePath = ""; |
||
95 | |||
96 | /** |
||
97 | * @Type("string") |
||
98 | * @Assert\Type("string") |
||
99 | * @SerializedName("method_getter_boolean_interface_manager_template_path") |
||
100 | */ |
||
101 | private $methodGetterBooleanInterfaceManageTemplatePath = ""; |
||
102 | |||
103 | /** |
||
104 | * @Type("string") |
||
105 | * @Assert\Type("string") |
||
106 | * @SerializedName("method_getter_boolean_manager_template_path") |
||
107 | */ |
||
108 | private $methodGetterBooleanManagerTemplatePath = ""; |
||
109 | |||
110 | /** |
||
111 | * @Type("string") |
||
112 | * @Assert\Type("string") |
||
113 | * @SerializedName("method_getter_interface_manager_template_path") |
||
114 | */ |
||
115 | private $methodGetterInterfaceManagerTemplatePath = ""; |
||
116 | |||
117 | /** |
||
118 | * @Type("string") |
||
119 | * @Assert\Type("string") |
||
120 | * @SerializedName("method_getter_manager_template_path") |
||
121 | */ |
||
122 | private $methodGetterManagerTemplatePath = ""; |
||
123 | |||
124 | /** |
||
125 | * @Type("string") |
||
126 | * @Assert\Type("string") |
||
127 | * @SerializedName("method_setter_interface_manager_template_path") |
||
128 | */ |
||
129 | private $methodSetterInterfaceManagerTemplatePath = ""; |
||
130 | |||
131 | /** |
||
132 | * @Type("string") |
||
133 | * @Assert\Type("string") |
||
134 | * @SerializedName("method_setter_manager_template_path") |
||
135 | */ |
||
136 | private $methodSetterManagerTemplatePath = ""; |
||
137 | |||
138 | /** |
||
139 | * Constr. |
||
140 | */ |
||
141 | public function __construct() |
||
142 | { |
||
143 | $this->constraints = new ArrayCollection(); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @Assert\IsTrue(message = "Invalid property name!") |
||
148 | * @return boolean |
||
149 | */ |
||
150 | public function isValidName() |
||
151 | { |
||
152 | if (false === Tools::isValidPropertyNameString($this->getName())) { |
||
153 | return false; |
||
154 | } |
||
155 | |||
156 | return true; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @Assert\IsTrue(message = "Invalid property type!") |
||
161 | * @return boolean |
||
162 | */ |
||
163 | public function isValidType() |
||
164 | { |
||
165 | if (false === Tools::isValidPropertyTypeString($this->getType())) { |
||
166 | return false; |
||
167 | } |
||
168 | |||
169 | return true; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @Assert\IsTrue(message = "Property has invalid validation constraint! Insert only constraint class with parameters eg. NotBlank() or Email(message = 'Invalid email')") |
||
174 | */ |
||
175 | public function hasAllCallableConstraintIfHasAny() |
||
176 | { |
||
177 | if (false === $this->hasConstraints()) { |
||
178 | return true; |
||
179 | } |
||
180 | |||
181 | foreach ($this->getConstraintAnnotationCollection() as $constraintAnnotation) { |
||
182 | if (false === Tools::isCallableConstraintAnnotation($constraintAnnotation)) { |
||
183 | return false; |
||
184 | } |
||
185 | } |
||
186 | |||
187 | return true; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Check is property boolean type |
||
192 | * |
||
193 | * @return boolean |
||
194 | */ |
||
195 | public function isTypeBoolean() |
||
196 | { |
||
197 | return PropertyManager::TYPE_BOOLEAN == $this->getType(); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Check is property ArrayCollection type |
||
202 | * |
||
203 | * @return boolean |
||
204 | */ |
||
205 | public function isTypeArrayCollection() |
||
206 | { |
||
207 | return PropertyManager::TYPE_ARRAY_COLLECTION == $this->getTypeName(); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Return property comment |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | public function getComment() |
||
216 | { |
||
217 | return $this->comment; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Set property comment |
||
222 | * |
||
223 | * @param string $comment |
||
224 | * @return PropertyManager |
||
225 | */ |
||
226 | public function setComment($comment) |
||
227 | { |
||
228 | $this->comment = $comment; |
||
229 | return $this; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Return constraints parts array |
||
234 | * |
||
235 | * @return ArrayCollection |
||
236 | */ |
||
237 | public function getConstraints() |
||
238 | { |
||
239 | if (false === ($this->constraints instanceof ArrayCollection)) { |
||
240 | return new ArrayCollection(); |
||
241 | } |
||
242 | |||
243 | return $this->constraints; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Check that property has constraints |
||
248 | * |
||
249 | * @return boolean |
||
250 | */ |
||
251 | public function hasConstraints() |
||
252 | { |
||
253 | return (false === $this->getConstraints()->isEmpty()); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Return constraints names array |
||
258 | * |
||
259 | * @return ArrayCollection |
||
260 | */ |
||
261 | public function getConstraintAnnotationCollection() |
||
262 | { |
||
263 | $constraintsFull = new ArrayCollection(); |
||
264 | foreach ($this->getConstraints() as $constraintPart) { |
||
265 | $constraintsFull->add(sprintf("@\Symfony\Component\Validator\Constraints\%s", $constraintPart)); |
||
266 | } |
||
267 | |||
268 | return $constraintsFull; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Set constraints names array |
||
273 | * |
||
274 | * @param ArrayCollection $constraints |
||
275 | * @return PropertyManager |
||
276 | */ |
||
277 | public function setConstraints(ArrayCollection $constraints) |
||
278 | { |
||
279 | $this->constraints = $constraints; |
||
280 | return $this; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Return property name string |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | public function getName() |
||
289 | { |
||
290 | return $this->name; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Return prepared property name eg. name_and_surname => nameAndSurname |
||
295 | * |
||
296 | * @param string $propertyName |
||
297 | * @return string |
||
298 | */ |
||
299 | public function getPreparedName() |
||
300 | { |
||
301 | return Inflector::camelize($this->getName()); |
||
|
|||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Set property name |
||
306 | * |
||
307 | * @param string $name |
||
308 | * @return PropertyManager |
||
309 | */ |
||
310 | public function setName($name) |
||
311 | { |
||
312 | $this->name = $name; |
||
313 | return $this; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Return property type string |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | public function getType() |
||
322 | { |
||
323 | return $this->type; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Return only type name without params eg. Doctrine\Common\Collections\ArrayCollection<AppBundle\Entity\Post> result Doctrine\Common\Collections\ArrayCollection |
||
328 | * |
||
329 | * @return string |
||
330 | * @throws Exception |
||
331 | */ |
||
332 | public function getTypeName() |
||
333 | { |
||
334 | $typeParser = new TypeParser(); |
||
335 | $result = $typeParser->parse($this->getType()); |
||
336 | |||
337 | if (false === array_key_exists("name", $result)) { |
||
338 | throw new Exception("Invalid type parsing result"); |
||
339 | } |
||
340 | |||
341 | return $result["name"]; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Set property type |
||
346 | * |
||
347 | * @param string $type |
||
348 | * @return PropertyManager |
||
349 | */ |
||
350 | public function setType($type) |
||
351 | { |
||
352 | $this->type = $type; |
||
353 | return $this; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Return custom serialized name |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public function getSerializedName() |
||
362 | { |
||
363 | return $this->serializedName; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Set custom serialized name form property |
||
368 | * |
||
369 | * @param string $serializedName |
||
370 | * @return PropertyManager |
||
371 | */ |
||
372 | public function setSerializedName($serializedName) |
||
373 | { |
||
374 | $this->serializedName = $serializedName; |
||
375 | return $this; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Check that property has custom serialized name |
||
380 | * |
||
381 | * @return boolean |
||
382 | */ |
||
383 | public function hasSerializedName() |
||
384 | { |
||
385 | return false === empty($this->serializedName); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @return boolean |
||
390 | */ |
||
391 | public function isOptional() |
||
392 | { |
||
393 | return $this->optional; |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * @param boolean $optional |
||
398 | */ |
||
399 | public function setOptional($optional) |
||
400 | { |
||
401 | $this->optional = $optional; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Return set of tags used in template |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | public function getTemplateTags() |
||
410 | { |
||
411 | return [ |
||
412 | self::TAG_COMMENT, |
||
413 | self::TAG_CONSTRAINTS, |
||
414 | self::TAG_JMS_PART, |
||
415 | self::TAG_TYPE, |
||
416 | self::TAG_NAME, |
||
417 | self::TAG_MULTILINE_COMMENT, |
||
418 | ]; |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @return string |
||
423 | */ |
||
424 | public function getPropertyManagerTemplatePath() |
||
428 | |||
429 | /** |
||
430 | * @return string |
||
431 | */ |
||
432 | public function getMethodGetterBooleanInterfaceManageTemplatePath() |
||
436 | |||
437 | /** |
||
438 | * @return string |
||
439 | */ |
||
440 | public function getMethodGetterBooleanManagerTemplatePath() |
||
444 | |||
445 | /** |
||
446 | * @return string |
||
447 | */ |
||
448 | public function getMethodGetterInterfaceManagerTemplatePath() |
||
452 | |||
453 | /** |
||
454 | * @return string |
||
455 | */ |
||
456 | public function getMethodGetterManagerTemplatePath() |
||
460 | |||
461 | /** |
||
462 | * @param string $propertyManagerTemplatePath |
||
463 | * @return PropertyManager |
||
464 | */ |
||
465 | public function setPropertyManagerTemplatePath($propertyManagerTemplatePath) |
||
470 | |||
471 | /** |
||
472 | * @param string $methodGetterBooleanInterfaceManageTemplatePath |
||
473 | * @return PropertyManager |
||
474 | */ |
||
475 | public function setMethodGetterBooleanInterfaceManageTemplatePath($methodGetterBooleanInterfaceManageTemplatePath) |
||
480 | |||
481 | /** |
||
482 | * @param string $methodGetterBooleanManagerTemplatePath |
||
483 | * @return PropertyManager |
||
484 | */ |
||
485 | public function setMethodGetterBooleanManagerTemplatePath($methodGetterBooleanManagerTemplatePath) |
||
490 | |||
491 | /** |
||
492 | * @param string $methodGetterInterfaceManagerTemplatePath |
||
493 | * @return PropertyManager |
||
494 | */ |
||
495 | public function setMethodGetterInterfaceManagerTemplatePath($methodGetterInterfaceManagerTemplatePath) |
||
500 | |||
501 | /** |
||
502 | * @param string $methodGetterManagerTemplatePath |
||
503 | * @return PropertyManager |
||
504 | */ |
||
505 | public function setMethodGetterManagerTemplatePath($methodGetterManagerTemplatePath) |
||
510 | |||
511 | /** |
||
512 | * @return string |
||
513 | */ |
||
514 | public function getTestClassMethodManagerTemplatePath() |
||
518 | |||
519 | /** |
||
520 | * @param string $testClassMethodManagerTemplatePath |
||
521 | * @return PropertyManager |
||
522 | */ |
||
523 | public function setTestClassMethodManagerTemplatePath($testClassMethodManagerTemplatePath) |
||
528 | |||
529 | /** |
||
530 | * @return string |
||
531 | */ |
||
532 | public function getMethodSetterInterfaceManagerTemplatePath() |
||
536 | |||
537 | /** |
||
538 | * @return string |
||
539 | */ |
||
540 | public function getMethodSetterManagerTemplatePath() |
||
544 | |||
545 | /** |
||
546 | * @param string $methodSetterInterfaceManagerTemplatePath |
||
547 | * @return PropertyManager |
||
548 | */ |
||
549 | public function setMethodSetterInterfaceManagerTemplatePath($methodSetterInterfaceManagerTemplatePath) |
||
554 | |||
555 | /** |
||
556 | * @param string $methodSetterManagerTemplatePath |
||
557 | * @return PropertyManager |
||
558 | */ |
||
559 | public function setMethodSetterManagerTemplatePath($methodSetterManagerTemplatePath) |
||
564 | |||
565 | /** |
||
566 | * @return boolean |
||
567 | */ |
||
568 | public function isObjectType() |
||
572 | |||
573 | /** |
||
574 | * @return string |
||
575 | * @throws Exception |
||
576 | */ |
||
577 | public function getTypeNameAbsoluteIfIsObjectTypeOrThrowException() |
||
585 | } |
||
586 |
This method has been deprecated.