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 |
||
12 | class Property extends \ReflectionProperty |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected static $typeMapping = [ |
||
18 | 'int' => 'integer', |
||
19 | 'bool' => 'boolean', |
||
20 | ]; |
||
21 | |||
22 | /** |
||
23 | * @var Arrayy |
||
24 | */ |
||
25 | protected $valueObject; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | protected $hasTypeDeclaration = false; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $isNullable = false; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $isInitialised = false; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $types = []; |
||
46 | |||
47 | /** |
||
48 | * Property constructor. |
||
49 | * |
||
50 | * @param Arrayy $valueObject |
||
51 | * @param null|\ReflectionProperty $reflectionProperty |
||
52 | * @param object $fakeObject |
||
53 | * |
||
54 | * @throws \ReflectionException |
||
55 | */ |
||
56 | 2 | public function __construct(Arrayy $valueObject, $reflectionProperty, $fakeObject = null) |
|
62 | |||
63 | /** |
||
64 | * @param string $type |
||
65 | * @param mixed $value |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | 6 | protected function assertTypeEquals(string $type, $value): bool |
|
83 | |||
84 | 2 | public static function fromPhpDocumentorProperty(Arrayy $valueObject, \phpDocumentor\Reflection\DocBlock\Tags\Property $phpDocumentorReflectionProperty): self |
|
113 | |||
114 | /** |
||
115 | * @param Type $type |
||
116 | * |
||
117 | * @return string[]|string |
||
118 | */ |
||
119 | 2 | protected static function parseDocTypeObject(Type $type) |
|
176 | |||
177 | 2 | public function getTypes(): array |
|
181 | |||
182 | protected function isValidGenericCollection(string $type, $collection): bool |
||
198 | |||
199 | /** |
||
200 | * @param mixed $value |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | 6 | protected function isValidType($value): bool |
|
224 | |||
225 | 6 | public function set($value) |
|
235 | |||
236 | /** |
||
237 | * @param mixed $value |
||
238 | */ |
||
239 | 2 | protected function throwInvalidType($value) |
|
255 | } |
||
256 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: