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 implements PropertyContract |
||
13 | { |
||
14 | /** @var array */ |
||
15 | private const TYPE_MAPPING = [ |
||
16 | 'int' => 'integer', |
||
17 | 'bool' => 'boolean', |
||
18 | 'float' => 'double', |
||
19 | ]; |
||
20 | |||
21 | /** @var bool */ |
||
22 | protected $hasTypeDeclaration = false; |
||
23 | |||
24 | /** @var bool */ |
||
25 | protected $nullable = false; |
||
26 | |||
27 | /** @var bool */ |
||
28 | protected $initialised = false; |
||
29 | |||
30 | /** @var bool */ |
||
31 | protected $immutable = false; |
||
32 | |||
33 | /** @var bool */ |
||
34 | protected $visible = true; |
||
35 | |||
36 | /** @var array */ |
||
37 | protected $types = []; |
||
38 | |||
39 | /** @var array */ |
||
40 | protected $arrayTypes = []; |
||
41 | |||
42 | /** @var mixed */ |
||
43 | protected $default; |
||
44 | |||
45 | /** @var mixed */ |
||
46 | protected $value; |
||
47 | |||
48 | /** @var ReflectionProperty */ |
||
49 | protected $reflection; |
||
50 | |||
51 | public function __construct(ReflectionProperty $reflectionProperty) |
||
57 | |||
58 | protected function resolveTypeDefinition() |
||
94 | |||
95 | protected function isValidType($value): bool |
||
115 | |||
116 | protected function cast($value) |
||
136 | |||
137 | protected function castCollection(array $values) |
||
163 | |||
164 | protected function shouldBeCastToCollection(array $values): bool |
||
182 | |||
183 | protected function assertTypeEquals(string $type, $value): bool |
||
196 | |||
197 | protected function isValidGenericCollection(string $type, $collection): bool |
||
213 | |||
214 | public function set($value) :void |
||
228 | |||
229 | public function setInitialized(bool $bool):void |
||
233 | |||
234 | public function isInitialized() :bool |
||
238 | |||
239 | public function getTypes(): array |
||
243 | |||
244 | public function getFqn(): string |
||
248 | |||
249 | public function nullable(): bool |
||
253 | |||
254 | public function setNullable(bool $bool): void |
||
258 | |||
259 | public function immutable(): bool |
||
263 | |||
264 | public function setImmutable(bool $immutable): void |
||
268 | |||
269 | public function getDefault() |
||
273 | |||
274 | public function setDefault($default): void |
||
278 | |||
279 | public function isVisible(): bool |
||
283 | |||
284 | public function setVisible(bool $bool): bool |
||
288 | |||
289 | public function getValue() |
||
297 | |||
298 | public function getValueFromReflection($object) |
||
302 | |||
303 | public function getName() :string |
||
307 | } |
||
308 |