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 | ]; |
||
17 | |||
18 | /** @var bool */ |
||
19 | protected $hasTypeDeclaration = false; |
||
20 | |||
21 | /** @var bool */ |
||
22 | protected $isNullable = false; |
||
23 | |||
24 | /** @var bool */ |
||
25 | protected $isInitialised = false; |
||
26 | |||
27 | /** @var bool */ |
||
28 | protected $isImmutable = false; |
||
29 | |||
30 | /** @var array */ |
||
31 | protected $types = []; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $arrayTypes = []; |
||
35 | |||
36 | /** @var mixed */ |
||
37 | protected $default; |
||
38 | |||
39 | /** @var mixed */ |
||
40 | protected $value; |
||
41 | |||
42 | /** @var ReflectionProperty */ |
||
43 | protected $reflection; |
||
44 | |||
45 | public static function fromReflection(ReflectionProperty $reflectionProperty): self |
||
49 | |||
50 | public function __construct(ReflectionProperty $reflectionProperty) |
||
56 | |||
57 | public function set($value) |
||
71 | |||
72 | public function setUninitialized() |
||
76 | |||
77 | public function isInitialized() |
||
81 | |||
82 | public function getTypes(): array |
||
86 | |||
87 | public function getFqn(): string |
||
91 | |||
92 | public function isNullable(): bool |
||
96 | |||
97 | public function setNullable(bool $bool): void |
||
101 | |||
102 | public function isImmutable(): bool |
||
106 | |||
107 | public function setIsImmutable(bool $isImmutable): void |
||
111 | |||
112 | protected function resolveTypeDefinition() |
||
150 | |||
151 | protected function isValidType($value): bool |
||
171 | |||
172 | protected function cast($value) |
||
192 | |||
193 | protected function castCollection(array $values) |
||
219 | |||
220 | protected function shouldBeCastToCollection(array $values): bool |
||
238 | |||
239 | protected function assertTypeEquals(string $type, $value): bool |
||
252 | |||
253 | protected function isValidGenericCollection(string $type, $collection): bool |
||
269 | |||
270 | public function getDefault() |
||
274 | |||
275 | public function setDefault($default): void |
||
279 | |||
280 | public function getValue() |
||
288 | |||
289 | public function getValueFromReflection($object) |
||
293 | |||
294 | public function getName() |
||
298 | |||
299 | public function getReflection(): ReflectionProperty |
||
303 | |||
304 | |||
305 | } |
||
306 |