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 |
||
| 23 | class PropertyManager implements RenderableInterface, MultilineCommentableInterface |
||
| 24 | { |
||
| 25 | |||
| 26 | use MultilineCommentTrait; |
||
| 27 | use TemplateTrait; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Property predefined types |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | const TYPE_BOOLEAN = 'boolean'; |
||
| 35 | const TYPE_ARRAY_COLLECTION = 'Doctrine\Common\Collections\ArrayCollection'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Comment of property |
||
| 39 | * |
||
| 40 | * @Type("string") |
||
| 41 | */ |
||
| 42 | private $comment = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Collection of Constraints |
||
| 46 | * |
||
| 47 | * @Type("Doctrine\Common\Collections\ArrayCollection<string>") |
||
| 48 | */ |
||
| 49 | private $constraints = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Property name |
||
| 53 | * |
||
| 54 | * @Type("string") |
||
| 55 | * @Assert\NotBlank(message="Property name can not be empty!") |
||
| 56 | */ |
||
| 57 | private $name = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Property type |
||
| 61 | * |
||
| 62 | * @Type("string") |
||
| 63 | * @Assert\NotBlank(message="Property type can not be empty!") |
||
| 64 | */ |
||
| 65 | private $type = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Property serialized name, default is property name in snake format eg. for property $lastPost => last_post |
||
| 69 | * |
||
| 70 | * @Type("string") |
||
| 71 | * @Assert\Type("string") |
||
| 72 | */ |
||
| 73 | private $serializedName = ''; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @Type("boolean") |
||
| 77 | * @Assert\Type("boolean") |
||
| 78 | */ |
||
| 79 | private $optional = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @Type("string") |
||
| 83 | * @Assert\Type("string") |
||
| 84 | * @SerializedName("property_manager_template_path") |
||
| 85 | */ |
||
| 86 | private $propertyManagerTemplatePath = ""; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @Type("string") |
||
| 90 | * @Assert\Type("string") |
||
| 91 | * @SerializedName("test_class_method_manager_template_path") |
||
| 92 | */ |
||
| 93 | private $testClassMethodManagerTemplatePath = ""; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @Type("string") |
||
| 97 | * @Assert\Type("string") |
||
| 98 | * @SerializedName("method_getter_boolean_interface_manager_template_path") |
||
| 99 | */ |
||
| 100 | private $methodGetterBooleanInterfaceManageTemplatePath = ""; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @Type("string") |
||
| 104 | * @Assert\Type("string") |
||
| 105 | * @SerializedName("method_getter_boolean_manager_template_path") |
||
| 106 | */ |
||
| 107 | private $methodGetterBooleanManagerTemplatePath = ""; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @Type("string") |
||
| 111 | * @Assert\Type("string") |
||
| 112 | * @SerializedName("method_getter_interface_manager_template_path") |
||
| 113 | */ |
||
| 114 | private $methodGetterInterfaceManagerTemplatePath = ""; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @Type("string") |
||
| 118 | * @Assert\Type("string") |
||
| 119 | * @SerializedName("method_getter_manager_template_path") |
||
| 120 | */ |
||
| 121 | private $methodGetterManagerTemplatePath = ""; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @Type("string") |
||
| 125 | * @Assert\Type("string") |
||
| 126 | * @SerializedName("method_setter_interface_manager_template_path") |
||
| 127 | */ |
||
| 128 | private $methodSetterInterfaceManagerTemplatePath = ""; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @Type("string") |
||
| 132 | * @Assert\Type("string") |
||
| 133 | * @SerializedName("method_setter_manager_template_path") |
||
| 134 | */ |
||
| 135 | private $methodSetterManagerTemplatePath = ""; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Constr. |
||
| 139 | */ |
||
| 140 | public function __construct() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @Assert\IsTrue(message = "Invalid property name!") |
||
| 147 | * @return boolean |
||
| 148 | */ |
||
| 149 | public function isValidName() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @Assert\IsTrue(message = "Invalid property type!") |
||
| 160 | * @return boolean |
||
| 161 | */ |
||
| 162 | public function isValidType() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @Assert\IsTrue(message = "Property has invalid validation constraint! Insert only constraint class with parameters eg. NotBlank() or Email(message = 'Invalid email')") |
||
| 173 | */ |
||
| 174 | public function hasAllCallableConstraintIfHasAny() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Check is property boolean type |
||
| 191 | * |
||
| 192 | * @return boolean |
||
| 193 | */ |
||
| 194 | public function isTypeBoolean() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Check is property ArrayCollection type |
||
| 201 | * |
||
| 202 | * @return boolean |
||
| 203 | */ |
||
| 204 | public function isTypeArrayCollection() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Return property comment |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getComment() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Set property comment |
||
| 221 | * |
||
| 222 | * @param string $comment |
||
| 223 | * @return PropertyManager |
||
| 224 | */ |
||
| 225 | public function setComment($comment) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Return constraints parts array |
||
| 233 | * |
||
| 234 | * @return ArrayCollection |
||
| 235 | */ |
||
| 236 | public function getConstraints() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Check that property has constraints |
||
| 247 | * |
||
| 248 | * @return boolean |
||
| 249 | */ |
||
| 250 | public function hasConstraints() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Return constraints names array |
||
| 257 | * |
||
| 258 | * @return ArrayCollection |
||
| 259 | */ |
||
| 260 | public function getConstraintAnnotationCollection() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Set constraints names array |
||
| 272 | * |
||
| 273 | * @param ArrayCollection $constraints |
||
| 274 | * @return PropertyManager |
||
| 275 | */ |
||
| 276 | public function setConstraints(ArrayCollection $constraints) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Return property name string |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getName() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Return prepared property name eg. name_and_surname => nameAndSurname |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function getPreparedName() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Set property name |
||
| 304 | * |
||
| 305 | * @param string $name |
||
| 306 | * @return PropertyManager |
||
| 307 | */ |
||
| 308 | public function setName($name) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Return property type string |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | public function getType() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Return only type name without params eg. Doctrine\Common\Collections\ArrayCollection<AppBundle\Entity\Post> result Doctrine\Common\Collections\ArrayCollection |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | * @throws Exception |
||
| 329 | */ |
||
| 330 | public function getTypeName() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Set property type |
||
| 344 | * |
||
| 345 | * @param string $type |
||
| 346 | * @return PropertyManager |
||
| 347 | */ |
||
| 348 | public function setType($type) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Return custom serialized name |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function getSerializedName() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Set custom serialized name form property |
||
| 366 | * |
||
| 367 | * @param string $serializedName |
||
| 368 | * @return PropertyManager |
||
| 369 | */ |
||
| 370 | public function setSerializedName($serializedName) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Check that property has custom serialized name |
||
| 378 | * |
||
| 379 | * @return boolean |
||
| 380 | */ |
||
| 381 | public function hasSerializedName() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return boolean |
||
| 388 | */ |
||
| 389 | public function isOptional() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param boolean $optional |
||
| 396 | */ |
||
| 397 | public function setOptional($optional) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Return set of tags used in template |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | public function getTemplateTags() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | public function getPropertyManagerTemplatePath() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | public function getMethodGetterBooleanInterfaceManageTemplatePath() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | public function getMethodGetterBooleanManagerTemplatePath() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | public function getMethodGetterInterfaceManagerTemplatePath() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | public function getMethodGetterManagerTemplatePath() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param string $propertyManagerTemplatePath |
||
| 461 | * @return PropertyManager |
||
| 462 | */ |
||
| 463 | public function setPropertyManagerTemplatePath($propertyManagerTemplatePath) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param string $methodGetterBooleanInterfaceManageTemplatePath |
||
| 471 | * @return PropertyManager |
||
| 472 | */ |
||
| 473 | public function setMethodGetterBooleanInterfaceManageTemplatePath($methodGetterBooleanInterfaceManageTemplatePath) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param string $methodGetterBooleanManagerTemplatePath |
||
| 481 | * @return PropertyManager |
||
| 482 | */ |
||
| 483 | public function setMethodGetterBooleanManagerTemplatePath($methodGetterBooleanManagerTemplatePath) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param string $methodGetterInterfaceManagerTemplatePath |
||
| 491 | * @return PropertyManager |
||
| 492 | */ |
||
| 493 | public function setMethodGetterInterfaceManagerTemplatePath($methodGetterInterfaceManagerTemplatePath) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param string $methodGetterManagerTemplatePath |
||
| 501 | * @return PropertyManager |
||
| 502 | */ |
||
| 503 | public function setMethodGetterManagerTemplatePath($methodGetterManagerTemplatePath) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public function getTestClassMethodManagerTemplatePath() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @param string $testClassMethodManagerTemplatePath |
||
| 519 | * @return PropertyManager |
||
| 520 | */ |
||
| 521 | public function setTestClassMethodManagerTemplatePath($testClassMethodManagerTemplatePath) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | public function getMethodSetterInterfaceManagerTemplatePath() |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function getMethodSetterManagerTemplatePath() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param string $methodSetterInterfaceManagerTemplatePath |
||
| 545 | * @return PropertyManager |
||
| 546 | */ |
||
| 547 | public function setMethodSetterInterfaceManagerTemplatePath($methodSetterInterfaceManagerTemplatePath) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param string $methodSetterManagerTemplatePath |
||
| 555 | * @return PropertyManager |
||
| 556 | */ |
||
| 557 | public function setMethodSetterManagerTemplatePath($methodSetterManagerTemplatePath) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @return boolean |
||
| 565 | */ |
||
| 566 | public function isObjectType() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @return string |
||
| 573 | * @throws Exception |
||
| 574 | */ |
||
| 575 | public function getTypeNameAbsoluteIfIsObjectTypeOrThrowException() |
||
| 583 | } |
||
| 584 |