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 |
||
| 52 | { |
||
| 53 | return new static($reflectionProperty); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function __construct(ReflectionProperty $reflectionProperty) |
||
| 57 | { |
||
| 58 | $this->reflection = $reflectionProperty; |
||
| 59 | |||
| 60 | $this->resolveTypeDefinition(); |
||
| 61 | } |
||
| 62 | |||
| 63 | protected function resolveTypeDefinition() |
||
| 64 | { |
||
| 65 | $docComment = $this->reflection->getDocComment(); |
||
| 66 | |||
| 67 | if (! $docComment) { |
||
| 68 | $this->setNullable(true); |
||
| 69 | |||
| 70 | return; |
||
| 71 | } |
||
| 72 | |||
| 73 | preg_match('/\@var ((?:(?:[\w|\\\\])+(?:\[\])?)+)/', $docComment, $matches); |
||
| 74 | |||
| 75 | if (! count($matches)) { |
||
| 76 | $this->setNullable(true); |
||
| 77 | |||
| 78 | return; |
||
| 79 | } |
||
| 80 | |||
| 81 | $varDocComment = end($matches); |
||
| 82 | |||
| 83 | $this->types = explode('|', $varDocComment); |
||
| 84 | $this->arrayTypes = str_replace('[]', '', $this->types); |
||
| 85 | |||
| 86 | if (in_array('immutable', $this->types) || in_array('Immutable', $this->types)) { |
||
| 87 | $this->setImmutable(true); |
||
| 88 | unset($this->types['immutable'], $this->types['Immutable']); |
||
| 89 | |||
| 90 | if (empty($this->types)) { |
||
| 91 | return; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->hasTypeDeclaration = true; |
||
| 96 | |||
| 97 | $this->setNullable(strpos($varDocComment, 'null') !== false); |
||
| 98 | } |
||
| 99 | |||
| 100 | protected function isValidType($value): bool |
||
| 101 | { |
||
| 102 | if (! $this->hasTypeDeclaration) { |
||
| 103 | return true; |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($this->nullable() && $value === null) { |
||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | foreach ($this->types as $currentType) { |
||
| 111 | $isValidType = $this->assertTypeEquals($currentType, $value); |
||
| 112 | |||
| 113 | if ($isValidType) { |
||
| 114 | return true; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return false; |
||
| 119 | } |
||
| 120 | |||
| 121 | protected function cast($value) |
||
| 122 | { |
||
| 123 | $castTo = null; |
||
| 124 | |||
| 125 | foreach ($this->types as $type) { |
||
| 126 | if (! is_subclass_of($type, DtoContract::class)) { |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | |||
| 130 | $castTo = $type; |
||
| 131 | |||
| 132 | break; |
||
| 133 | } |
||
| 134 | |||
| 135 | if (! $castTo) { |
||
| 136 | return $value; |
||
| 137 | } |
||
| 138 | |||
| 139 | return new $castTo($value); |
||
| 140 | } |
||
| 141 | |||
| 142 | protected function castCollection(array $values) |
||
| 143 | { |
||
| 144 | $castTo = null; |
||
| 145 | |||
| 146 | foreach ($this->arrayTypes as $type) { |
||
| 147 | if (! is_subclass_of($type, DtoContract::class)) { |
||
| 148 | continue; |
||
| 149 | } |
||
| 150 | |||
| 151 | $castTo = $type; |
||
| 152 | |||
| 153 | break; |
||
| 154 | } |
||
| 155 | |||
| 156 | if (! $castTo) { |
||
| 157 | return $values; |
||
| 158 | } |
||
| 159 | |||
| 160 | $casts = []; |
||
| 161 | |||
| 162 | foreach ($values as $value) { |
||
| 163 | $casts[] = new $castTo($value); |
||
| 164 | } |
||
| 165 | |||
| 166 | return $casts; |
||
| 167 | } |
||
| 168 | |||
| 169 | protected function shouldBeCastToCollection(array $values): bool |
||
| 170 | { |
||
| 171 | if (empty($values)) { |
||
| 172 | return false; |
||
| 173 | } |
||
| 174 | |||
| 175 | foreach ($values as $key => $value) { |
||
| 176 | if (is_string($key)) { |
||
| 177 | return false; |
||
| 178 | } |
||
| 179 | |||
| 180 | if (! is_array($value)) { |
||
| 181 | return false; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | return true; |
||
| 186 | } |
||
| 187 | |||
| 188 | protected function assertTypeEquals(string $type, $value): bool |
||
| 201 | |||
| 202 | protected function isValidGenericCollection(string $type, $collection): bool |
||
| 218 | |||
| 219 | public function set($value) :void |
||
| 220 | { |
||
| 221 | if (is_array($value)) { |
||
| 222 | $value = $this->shouldBeCastToCollection($value) ? $this->castCollection($value) : $this->cast($value); |
||
| 223 | } |
||
| 224 | |||
| 225 | if (! $this->isValidType($value)) { |
||
| 233 | |||
| 234 | public function setInitialized(bool $bool):void |
||
| 238 | |||
| 239 | public function isInitialized() :bool |
||
| 243 | |||
| 244 | public function getTypes(): array |
||
| 248 | |||
| 249 | public function getFqn(): string |
||
| 253 | |||
| 254 | public function nullable(): bool |
||
| 258 | |||
| 259 | public function setNullable(bool $bool): void |
||
| 263 | |||
| 264 | public function immutable(): bool |
||
| 268 | |||
| 269 | public function setImmutable(bool $immutable): void |
||
| 273 | |||
| 274 | public function getDefault() |
||
| 278 | |||
| 279 | public function setDefault($default): void |
||
| 283 | |||
| 284 | public function isVisible(): bool |
||
| 288 | |||
| 289 | public function setVisible(bool $bool): bool |
||
| 293 | |||
| 294 | public function getValue() |
||
| 302 | |||
| 303 | public function getValueFromReflection($object) |
||
| 307 | |||
| 308 | public function getName() :string |
||
| 312 | |||
| 313 | public function getReflection(): ReflectionProperty |
||
| 317 | } |
||
| 318 |