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 array */ |
||
31 | protected $types = []; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $arrayTypes = []; |
||
35 | |||
36 | public static function fromReflection(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty) |
||
40 | |||
41 | public function __construct(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty) |
||
49 | |||
50 | public function set($value) |
||
64 | |||
65 | public function getTypes(): array |
||
69 | |||
70 | public function getFqn(): string |
||
74 | |||
75 | public function isNullable(): bool |
||
79 | |||
80 | protected function resolveTypeDefinition() |
||
111 | |||
112 | protected function isValidType($value): bool |
||
132 | |||
133 | protected function cast($value) |
||
153 | |||
154 | protected function castCollection(array $values) |
||
180 | |||
181 | protected function shouldBeCastToCollection(array $values): bool |
||
199 | |||
200 | protected function assertTypeEquals(string $type, $value): bool |
||
217 | |||
218 | protected function isValidArray(string $type, $collection): bool |
||
228 | |||
229 | protected function isValidIterable(string $type, $collection): bool |
||
241 | |||
242 | protected function isValidGenericCollection(string $type, $collection): bool |
||
252 | } |
||
253 |