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 $isRequired = true; |
||
29 | |||
30 | /** @var bool */ |
||
31 | protected $isInitialised = false; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $types = []; |
||
35 | |||
36 | /** @var array */ |
||
37 | protected $arrayTypes = []; |
||
38 | |||
39 | /** @var string */ |
||
40 | protected $rules; |
||
41 | |||
42 | /** @var mixed */ |
||
43 | protected $default; |
||
44 | |||
45 | /** @var mixed */ |
||
46 | protected $actualValue; |
||
47 | |||
48 | public static function fromReflection(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty) |
||
52 | |||
53 | public function __construct(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty) |
||
61 | |||
62 | public function set($value) |
||
76 | |||
77 | public function setUninitialized() |
||
83 | |||
84 | public function getTypes(): array |
||
88 | |||
89 | public function getFqn(): string |
||
93 | |||
94 | public function isNullable(): bool |
||
98 | |||
99 | /** |
||
100 | * @return bool |
||
101 | */ |
||
102 | public function isRequired(): bool |
||
106 | |||
107 | public function setRequired(bool $bool): void |
||
111 | |||
112 | public function setNullable(bool $bool): void |
||
116 | |||
117 | /** |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function isOptional(): bool |
||
124 | |||
125 | protected function resolveTypeDefinition() |
||
152 | |||
153 | protected function isValidType($value): bool |
||
173 | |||
174 | protected function cast($value) |
||
194 | |||
195 | protected function castCollection(array $values) |
||
221 | |||
222 | protected function shouldBeCastToCollection(array $values): bool |
||
240 | |||
241 | protected function assertTypeEquals(string $type, $value): bool |
||
254 | |||
255 | protected function isValidGenericCollection(string $type, $collection): bool |
||
271 | |||
272 | /** |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getRules(): string |
||
279 | |||
280 | /** |
||
281 | * @return mixed |
||
282 | */ |
||
283 | public function getDefault() |
||
287 | |||
288 | /** |
||
289 | * @param mixed $default |
||
290 | */ |
||
291 | public function setDefault($default): void |
||
295 | |||
296 | /** |
||
297 | * @param string $rules |
||
298 | */ |
||
299 | public function addRule(string $rules): void |
||
307 | |||
308 | /** |
||
309 | * @return mixed |
||
310 | */ |
||
311 | public function getActualValue() |
||
317 | } |
||
318 |