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 extends ReflectionProperty |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * - The key is the type of the propoerty |
||
| 13 | * - The callable receives the value and may return the same or a different one |
||
| 14 | * @var array<string,callable(mixed $value)> |
||
|
|
|||
| 15 | */ |
||
| 16 | public static $make = [ |
||
| 17 | ]; |
||
| 18 | |||
| 19 | /** @var array */ |
||
| 20 | protected static $typeMapping = [ |
||
| 21 | 'int' => 'integer', |
||
| 22 | 'bool' => 'boolean', |
||
| 23 | 'float' => 'double', |
||
| 24 | ]; |
||
| 25 | |||
| 26 | /** @var \Spatie\DataTransferObject\DataTransferObject */ |
||
| 27 | protected $valueObject; |
||
| 28 | |||
| 29 | /** @var bool */ |
||
| 30 | protected $hasTypeDeclaration = false; |
||
| 31 | |||
| 32 | /** @var bool */ |
||
| 33 | protected $isNullable = false; |
||
| 34 | |||
| 35 | /** @var bool */ |
||
| 36 | protected $isInitialised = false; |
||
| 37 | |||
| 38 | /** @var array */ |
||
| 39 | protected $types = []; |
||
| 40 | |||
| 41 | /** @var array */ |
||
| 42 | protected $arrayTypes = []; |
||
| 43 | |||
| 44 | public static function fromReflection(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty) |
||
| 45 | { |
||
| 46 | return new self($valueObject, $reflectionProperty); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function __construct(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty) |
||
| 50 | { |
||
| 51 | parent::__construct($reflectionProperty->class, $reflectionProperty->getName()); |
||
| 52 | |||
| 53 | $this->valueObject = $valueObject; |
||
| 54 | |||
| 55 | $this->resolveTypeDefinition(); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function set($value) |
||
| 59 | { |
||
| 60 | if (is_array($value)) { |
||
| 61 | $value = $this->shouldBeCastToCollection($value) ? $this->castCollection($value) : $this->cast($value); |
||
| 62 | } else { |
||
| 63 | $value = $this->makeValue($value); |
||
| 64 | } |
||
| 65 | |||
| 66 | if (! $this->isValidType($value)) { |
||
| 67 | throw DataTransferObjectError::invalidType($this, $value); |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->isInitialised = true; |
||
| 71 | |||
| 72 | $this->valueObject->{$this->getName()} = $value; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function getTypes(): array |
||
| 76 | { |
||
| 77 | return $this->types; |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getFqn(): string |
||
| 81 | { |
||
| 82 | return "{$this->getDeclaringClass()->getName()}::{$this->getName()}"; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function isNullable(): bool |
||
| 86 | { |
||
| 87 | return $this->isNullable; |
||
| 88 | } |
||
| 89 | |||
| 90 | protected function resolveTypeDefinition() |
||
| 91 | { |
||
| 92 | $docComment = $this->getDocComment(); |
||
| 93 | |||
| 94 | if (! $docComment) { |
||
| 95 | $this->isNullable = true; |
||
| 96 | |||
| 97 | return; |
||
| 98 | } |
||
| 99 | |||
| 100 | preg_match('/\@var ((?:(?:[\w|\\\\<>])+(?:\[\])?)+)/', $docComment, $matches); |
||
| 101 | |||
| 102 | if (! count($matches)) { |
||
| 103 | $this->isNullable = true; |
||
| 104 | |||
| 105 | return; |
||
| 106 | } |
||
| 107 | |||
| 108 | $this->hasTypeDeclaration = true; |
||
| 109 | |||
| 110 | $varDocComment = end($matches); |
||
| 111 | |||
| 112 | $this->types = explode('|', $varDocComment); |
||
| 113 | $this->arrayTypes = str_replace('[]', '', $this->types); |
||
| 114 | |||
| 115 | if (preg_match_all('/iterable<([^|]*)>/', $varDocComment, $matches)) { |
||
| 116 | $this->arrayTypes = array_merge($this->arrayTypes, $matches[1]); |
||
| 117 | } |
||
| 118 | |||
| 119 | $this->isNullable = strpos($varDocComment, 'null') !== false; |
||
| 120 | } |
||
| 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 |
||
| 211 | { |
||
| 212 | if (strpos($type, '[]') !== false) { |
||
| 213 | return $this->isValidArray($type, $value); |
||
| 214 | } |
||
| 215 | |||
| 216 | if ($type === 'iterable' || strpos($type, 'iterable<') === 0) { |
||
| 227 | |||
| 228 | protected function isValidArray(string $type, $collection): bool |
||
| 238 | |||
| 239 | protected function isValidIterable(string $type, $collection): bool |
||
| 251 | |||
| 252 | protected function isValidGenericCollection(string $type, $collection): bool |
||
| 262 | |||
| 263 | protected function makeValue($value) |
||
| 277 | } |
||
| 278 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.