Complex classes like Property 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 Property, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Property extends ReflectionProperty |
||
| 10 | { |
||
| 11 | /** @var array */ |
||
| 12 | protected static $typeMapping = [ |
||
| 13 | 'int' => 'integer', |
||
| 14 | 'bool' => 'boolean', |
||
| 15 | 'float' => 'double', |
||
| 16 | ]; |
||
| 17 | |||
| 18 | /** @var \Spatie\DataTransferObject\DataTransferObject */ |
||
| 19 | protected $valueObject; |
||
| 20 | |||
| 21 | /** @var bool */ |
||
| 22 | protected $hasTypeDeclaration = false; |
||
| 23 | |||
| 24 | /** @var bool */ |
||
| 25 | protected $isNullable = false; |
||
| 26 | |||
| 27 | /** @var bool */ |
||
| 28 | protected $isInitialised = false; |
||
| 29 | |||
| 30 | /** @var bool */ |
||
| 31 | protected $isImmutable = false; |
||
| 32 | |||
| 33 | /** @var array */ |
||
| 34 | protected $types = []; |
||
| 35 | |||
| 36 | /** @var array */ |
||
| 37 | protected $arrayTypes = []; |
||
| 38 | |||
| 39 | public static function fromReflection(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty) |
||
| 43 | |||
| 44 | public function __construct(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty) |
||
| 52 | |||
| 53 | public function set($value) |
||
| 67 | |||
| 68 | public function getTypes(): array |
||
| 72 | |||
| 73 | public function getFqn(): string |
||
| 77 | |||
| 78 | public function isNullable(): bool |
||
| 82 | |||
| 83 | public function isImmutable(): bool |
||
| 87 | |||
| 88 | public function isInitialised(): bool |
||
| 92 | |||
| 93 | public function markImmutable(): Property |
||
| 99 | |||
| 100 | protected function resolveTypeDefinition() |
||
| 138 | |||
| 139 | protected function isValidType($value): bool |
||
| 159 | |||
| 160 | protected function cast($value) |
||
| 180 | |||
| 181 | protected function castCollection(array $values) |
||
| 207 | |||
| 208 | protected function shouldBeCastToCollection(array $values): bool |
||
| 226 | |||
| 227 | protected function assertTypeEquals(string $type, $value): bool |
||
| 240 | |||
| 241 | protected function isValidGenericCollection(string $type, $collection): bool |
||
| 257 | } |
||
| 258 |