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 |
||
10 | { |
||
11 | /** @var array */ |
||
12 | protected static $typeMapping = [ |
||
13 | 'int' => 'integer', |
||
14 | 'bool' => 'boolean', |
||
15 | 'float' => 'double', |
||
16 | 'immutable' => immutable::class, |
||
17 | ]; |
||
18 | |||
19 | /** @var bool */ |
||
20 | protected $hasTypeDeclaration = false; |
||
21 | |||
22 | /** @var bool */ |
||
23 | protected $isNullable = false; |
||
24 | |||
25 | /** @var bool */ |
||
26 | protected $isInitialised = false; |
||
27 | |||
28 | /** @var bool */ |
||
29 | protected $isImmutable = false; |
||
30 | |||
31 | /** @var array */ |
||
32 | protected $types = []; |
||
33 | |||
34 | /** @var array */ |
||
35 | protected $arrayTypes = []; |
||
36 | |||
37 | /** @var mixed */ |
||
38 | protected $default; |
||
39 | |||
40 | /** @var mixed */ |
||
41 | protected $value; |
||
42 | |||
43 | /** @var ReflectionProperty */ |
||
44 | protected $reflection; |
||
45 | |||
46 | public static function fromReflection(ReflectionProperty $reflectionProperty): self |
||
50 | |||
51 | public function __construct(ReflectionProperty $reflectionProperty) |
||
57 | |||
58 | public function set($value) |
||
72 | |||
73 | public function setUninitialized() |
||
77 | |||
78 | public function isInitialized() |
||
82 | |||
83 | public function getTypes(): array |
||
87 | |||
88 | public function getFqn(): string |
||
92 | |||
93 | public function isNullable(): bool |
||
97 | |||
98 | public function setNullable(bool $bool): void |
||
102 | |||
103 | public function isImmutable(): bool |
||
107 | |||
108 | public function setIsImmutable(bool $isImmutable): void |
||
112 | |||
113 | protected function resolveTypeDefinition() |
||
146 | |||
147 | protected function isValidType($value): bool |
||
167 | |||
168 | protected function cast($value) |
||
188 | |||
189 | protected function castCollection(array $values) |
||
215 | |||
216 | protected function shouldBeCastToCollection(array $values): bool |
||
234 | |||
235 | protected function assertTypeEquals(string $type, $value): bool |
||
248 | |||
249 | protected function isValidGenericCollection(string $type, $collection): bool |
||
265 | |||
266 | public function getDefault() |
||
270 | |||
271 | public function setDefault($default): void |
||
275 | |||
276 | public function getValue() |
||
284 | |||
285 | public function getValueFromReflection($object) |
||
289 | |||
290 | public function getName() |
||
294 | } |
||
295 |