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 array */ |
||
| 28 | protected $types = []; |
||
| 29 | |||
| 30 | /** @var array */ |
||
| 31 | protected $arrayTypes = []; |
||
| 32 | |||
| 33 | /** @var mixed */ |
||
| 34 | protected $default; |
||
| 35 | |||
| 36 | /** @var mixed */ |
||
| 37 | protected $value; |
||
| 38 | |||
| 39 | /** @var ReflectionProperty */ |
||
| 40 | protected $reflection; |
||
| 41 | |||
| 42 | public static function fromReflection(ReflectionProperty $reflectionProperty): self |
||
| 46 | |||
| 47 | public function __construct(ReflectionProperty $reflectionProperty) |
||
| 53 | |||
| 54 | public function set($value) |
||
| 68 | |||
| 69 | public function setUninitialized() |
||
| 73 | |||
| 74 | public function getTypes(): array |
||
| 78 | |||
| 79 | public function getFqn(): string |
||
| 83 | |||
| 84 | public function isNullable(): bool |
||
| 88 | |||
| 89 | public function setNullable(bool $bool): void |
||
| 93 | |||
| 94 | protected function resolveTypeDefinition() |
||
| 121 | |||
| 122 | protected function isValidType($value): bool |
||
| 142 | |||
| 143 | protected function cast($value) |
||
| 163 | |||
| 164 | protected function castCollection(array $values) |
||
| 190 | |||
| 191 | protected function shouldBeCastToCollection(array $values): bool |
||
| 209 | |||
| 210 | protected function assertTypeEquals(string $type, $value): bool |
||
| 223 | |||
| 224 | protected function isValidGenericCollection(string $type, $collection): bool |
||
| 240 | |||
| 241 | public function getDefault() |
||
| 245 | |||
| 246 | public function setDefault($default): void |
||
| 250 | |||
| 251 | public function getValue() |
||
| 259 | |||
| 260 | public function getValueFromReflection($object) |
||
| 264 | |||
| 265 | public function getName() |
||
| 269 | |||
| 270 | public function __call($name, $arguments) |
||
| 274 | } |
||
| 275 |