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 | protected static $typeMapping = [ |
||
| 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 static function fromReflection(ReflectionProperty $reflectionProperty): self |
||
| 55 | |||
| 56 | public function __construct(ReflectionProperty $reflectionProperty) |
||
| 62 | |||
| 63 | protected function resolveTypeDefinition() |
||
| 101 | |||
| 102 | protected function isValidType($value): bool |
||
| 122 | |||
| 123 | protected function cast($value) |
||
| 143 | |||
| 144 | protected function castCollection(array $values) |
||
| 170 | |||
| 171 | protected function shouldBeCastToCollection(array $values): bool |
||
| 189 | |||
| 190 | protected function assertTypeEquals(string $type, $value): bool |
||
| 203 | |||
| 204 | protected function isValidGenericCollection(string $type, $collection): bool |
||
| 220 | |||
| 221 | public function set($value) :void |
||
| 235 | |||
| 236 | public function setInitialized(bool $bool):void |
||
| 240 | |||
| 241 | public function isInitialized() :bool |
||
| 245 | |||
| 246 | public function getTypes(): array |
||
| 250 | |||
| 251 | public function getFqn(): string |
||
| 255 | |||
| 256 | public function nullable(): bool |
||
| 260 | |||
| 261 | public function setNullable(bool $bool): void |
||
| 265 | |||
| 266 | public function immutable(): bool |
||
| 270 | |||
| 271 | public function setImmutable(bool $immutable): void |
||
| 275 | |||
| 276 | public function getDefault() |
||
| 280 | |||
| 281 | public function setDefault($default): void |
||
| 285 | |||
| 286 | public function isVisible(): bool |
||
| 290 | |||
| 291 | public function setVisible(bool $bool): bool |
||
| 295 | |||
| 296 | public function getValue() |
||
| 304 | |||
| 305 | public function getValueFromReflection($object) |
||
| 309 | |||
| 310 | public function getName() :string |
||
| 314 | |||
| 315 | public function getReflection(): ReflectionProperty |
||
| 319 | |||
| 320 | } |
||
| 321 |