| Total Complexity | 58 |
| Total Lines | 300 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DataTransferObject 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.
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 DataTransferObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | abstract class DataTransferObject implements DtoContract |
||
| 22 | { |
||
| 23 | /** @var array */ |
||
| 24 | protected $onlyKeys = []; |
||
| 25 | |||
| 26 | /** @var array */ |
||
| 27 | protected $with = []; |
||
| 28 | |||
| 29 | /** @var Property[] | array */ |
||
| 30 | protected $properties = []; |
||
| 31 | |||
| 32 | /** @var bool */ |
||
| 33 | protected $immutable = false; |
||
| 34 | |||
| 35 | public function __construct(array $parameters) |
||
| 36 | { |
||
| 37 | $this->boot($parameters); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Boot the dto and process all parameters. |
||
| 42 | * @param array $parameters |
||
| 43 | * @throws \ReflectionException |
||
| 44 | */ |
||
| 45 | protected function boot(array $parameters): void |
||
| 71 | } |
||
| 72 | |||
| 73 | protected function determineImmutability() |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | protected function setImmutable(): void |
||
| 84 | { |
||
| 85 | if (!$this->isImmutable()) { |
||
| 86 | $this->immutable = true; |
||
| 87 | foreach ($this->properties as $property) { |
||
| 88 | $this->chainPropertyImmutable($property); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | protected function chainPropertyImmutable(PropertyContract $property) |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get all public properties from the current object through reflection. |
||
| 109 | * @return Property[] |
||
| 110 | * @throws \ReflectionException |
||
| 111 | */ |
||
| 112 | protected function getPublicProperties(): array |
||
| 113 | { |
||
| 114 | $class = new ReflectionClass(static::class); |
||
| 115 | |||
| 116 | $properties = []; |
||
| 117 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
||
| 118 | $properties[$reflectionProperty->getName()] = new Property($reflectionProperty); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $properties; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Check if property passes the basic conditions. |
||
| 126 | * @param PropertyContract $property |
||
| 127 | * @param array $parameters |
||
| 128 | */ |
||
| 129 | protected function validateProperty(PropertyContract $property, array $parameters): void |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the value if it's present in the array. |
||
| 142 | * @param PropertyContract $property |
||
| 143 | * @param array $parameters |
||
| 144 | */ |
||
| 145 | protected function setPropertyValue(PropertyContract $property, array $parameters): void |
||
| 146 | { |
||
| 147 | if (array_key_exists($property->getName(), $parameters)) { |
||
| 148 | $property->set($parameters[$property->getName()]); |
||
| 149 | $property->validate(); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set the value if it's present in the array. |
||
| 155 | * @param PropertyContract $property |
||
| 156 | */ |
||
| 157 | protected function setPropertyDefaultValue(PropertyContract $property): void |
||
| 158 | { |
||
| 159 | $property->setDefault($property->getValueFromReflection($this)); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Allows to mutate the property before it gets processed. |
||
| 164 | * @param PropertyContract $property |
||
| 165 | * @return PropertyContract |
||
| 166 | */ |
||
| 167 | protected function mutateProperty(PropertyContract $property): PropertyContract |
||
| 168 | { |
||
| 169 | return $property; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Check if there are additional parameters left. |
||
| 174 | * Throw error if there are. |
||
| 175 | * Additional properties are not allowed in a dto. |
||
| 176 | * @param array $parameters |
||
| 177 | * @throws UnknownPropertiesDtoException |
||
| 178 | */ |
||
| 179 | protected function processRemainingProperties(array $parameters) |
||
| 180 | { |
||
| 181 | if (count($parameters)) { |
||
| 182 | throw new UnknownPropertiesDtoException(array_keys($parameters), static::class); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Immutable behavior |
||
| 188 | * Throw error if a user tries to set a property. |
||
| 189 | * @param $name |
||
| 190 | * @param $value |
||
| 191 | * @throws ImmutableDtoException|ImmutablePropertyDtoException|PropertyNotFoundDtoException |
||
| 192 | */ |
||
| 193 | public function __set($name, $value) |
||
| 194 | { |
||
| 195 | if ($this->immutable) { |
||
| 196 | throw new ImmutableDtoException($name); |
||
| 197 | } |
||
| 198 | if (!isset($this->properties[$name])) { |
||
| 199 | throw new PropertyNotFoundDtoException($name, get_class($this)); |
||
| 200 | } |
||
| 201 | |||
| 202 | if ($this->properties[$name]->immutable()) { |
||
| 203 | throw new ImmutablePropertyDtoException($name); |
||
| 204 | } |
||
| 205 | $this->$name = $value; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Proxy through to the properties array. |
||
| 210 | * @param $name |
||
| 211 | * @return mixed |
||
| 212 | */ |
||
| 213 | public function &__get($name) |
||
| 214 | { |
||
| 215 | return $this->properties[$name]->value; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function isImmutable(): bool |
||
| 219 | { |
||
| 220 | return $this->immutable; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function all(): array |
||
| 232 | } |
||
| 233 | |||
| 234 | public function only(string ...$keys): DtoContract |
||
| 235 | { |
||
| 236 | $this->onlyKeys = array_merge($this->onlyKeys, $keys); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function except(string ...$keys): DtoContract |
||
| 242 | { |
||
| 243 | foreach ($keys as $key) { |
||
| 244 | if (array_key_exists($key, $this->with)) { |
||
| 245 | unset($this->with[$key]); |
||
| 246 | } |
||
| 247 | $property = $this->properties[$key] ?? null; |
||
| 248 | if (isset($property)) { |
||
| 249 | $property->setVisible(false); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | return $this; |
||
| 253 | } |
||
| 254 | |||
| 255 | public function with(string $key, $value): DtoContract |
||
| 256 | { |
||
| 257 | if (array_key_exists($key, $this->properties)) { |
||
| 258 | throw new PropertyAlreadyExistsException($key); |
||
| 259 | } |
||
| 260 | return $this->override($key, $value); |
||
| 261 | } |
||
| 262 | |||
| 263 | public function override(string $key, $value): DtoContract |
||
| 264 | { |
||
| 265 | if ($this->isImmutable()) { |
||
| 266 | throw new ImmutableDtoException($key); |
||
| 267 | } |
||
| 268 | if (($propertyExists = array_key_exists($key, $this->properties) && $this->properties[$key]->immutable())) { |
||
| 269 | throw new ImmutablePropertyDtoException($key); |
||
| 270 | } |
||
| 271 | |||
| 272 | if ($propertyExists) { |
||
|
|
|||
| 273 | $property = $this->properties[$key]; |
||
| 274 | $property->set($value); |
||
| 275 | $property->validate(); |
||
| 276 | } else { |
||
| 277 | $this->with[$key] = $value; |
||
| 278 | } |
||
| 279 | |||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function toArray(): array |
||
| 299 | } |
||
| 300 | |||
| 301 | protected function parseArray(array $array): array |
||
| 321 | } |
||
| 322 | } |
||
| 323 |