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 \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 | /** @var ReflectionProperty */ |
||
49 | protected $reflection; |
||
50 | |||
51 | public static function fromReflection(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty) |
||
55 | |||
56 | public function __construct(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty) |
||
64 | |||
65 | public function set($value) |
||
79 | |||
80 | public function setUninitialized() |
||
84 | |||
85 | public function getTypes(): array |
||
89 | |||
90 | public function getFqn(): string |
||
94 | |||
95 | public function isNullable(): bool |
||
99 | |||
100 | /** |
||
101 | * @return bool |
||
102 | */ |
||
103 | public function isRequired(): bool |
||
107 | |||
108 | public function setRequired(bool $bool): void |
||
112 | |||
113 | public function setNullable(bool $bool): void |
||
117 | |||
118 | /** |
||
119 | * @return bool |
||
120 | */ |
||
121 | public function isOptional(): bool |
||
125 | |||
126 | protected function resolveTypeDefinition() |
||
153 | |||
154 | protected function isValidType($value): bool |
||
174 | |||
175 | protected function cast($value) |
||
195 | |||
196 | protected function castCollection(array $values) |
||
222 | |||
223 | protected function shouldBeCastToCollection(array $values): bool |
||
241 | |||
242 | protected function assertTypeEquals(string $type, $value): bool |
||
255 | |||
256 | protected function isValidGenericCollection(string $type, $collection): bool |
||
272 | |||
273 | /** |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getRules(): string |
||
280 | |||
281 | /** |
||
282 | * @return mixed |
||
283 | */ |
||
284 | public function getDefault() |
||
288 | |||
289 | /** |
||
290 | * @param mixed $default |
||
291 | */ |
||
292 | public function setDefault($default): void |
||
296 | |||
297 | /** |
||
298 | * @param string $rules |
||
299 | */ |
||
300 | public function addRule(string $rules): void |
||
308 | |||
309 | /** |
||
310 | * @return mixed |
||
311 | */ |
||
312 | public function getActualValue() |
||
320 | |||
321 | public function __call($name, $arguments) |
||
325 | } |
||
326 |