| Total Complexity | 47 |
| Total Lines | 275 |
| 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 Property[] | array */ |
||
| 27 | protected $properties = []; |
||
| 28 | |||
| 29 | /** @var bool */ |
||
| 30 | protected $immutable = false; |
||
| 31 | |||
| 32 | public function __construct(array $parameters) |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Boot the dto and process all parameters. |
||
| 39 | * @param array $parameters |
||
| 40 | * @throws \ReflectionException |
||
| 41 | */ |
||
| 42 | protected function boot(array $parameters): void |
||
| 68 | } |
||
| 69 | |||
| 70 | protected function determineImmutability() |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | public function setImmutable(): void |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | protected function chainPropertiesImmutable(PropertyContract $property) |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get all public properties from the current object through reflection. |
||
| 103 | * @return Property[] |
||
| 104 | * @throws \ReflectionException |
||
| 105 | */ |
||
| 106 | protected function getPublicProperties(): array |
||
| 107 | { |
||
| 108 | $class = new ReflectionClass(static::class); |
||
| 109 | |||
| 110 | $properties = []; |
||
| 111 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
||
| 112 | $properties[$reflectionProperty->getName()] = new Property($reflectionProperty); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $properties; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Check if property passes the basic conditions. |
||
| 120 | * @param PropertyContract $property |
||
| 121 | * @param array $parameters |
||
| 122 | */ |
||
| 123 | protected function validateProperty(PropertyContract $property, array $parameters): void |
||
| 124 | { |
||
| 125 | if (! array_key_exists($property->getName(), $parameters) |
||
| 126 | && is_null($property->getDefault()) |
||
| 127 | && ! $property->nullable() |
||
| 128 | ) { |
||
| 129 | throw new UninitialisedPropertyDtoException($property); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set the value if it's present in the array. |
||
| 135 | * @param PropertyContract $property |
||
| 136 | * @param array $parameters |
||
| 137 | */ |
||
| 138 | protected function setPropertyValue(PropertyContract $property, array $parameters): void |
||
| 139 | { |
||
| 140 | if (array_key_exists($property->getName(), $parameters)) { |
||
| 141 | $property->set($parameters[$property->getName()]); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set the value if it's present in the array. |
||
| 147 | * @param PropertyContract $property |
||
| 148 | */ |
||
| 149 | protected function setPropertyDefaultValue(PropertyContract $property): void |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Allows to mutate the property before it gets processed. |
||
| 156 | * @param PropertyContract $property |
||
| 157 | * @return PropertyContract |
||
| 158 | */ |
||
| 159 | protected function mutateProperty(PropertyContract $property): PropertyContract |
||
| 160 | { |
||
| 161 | return $property; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Check if there are additional parameters left. |
||
| 166 | * Throw error if there are. |
||
| 167 | * Additional properties are not allowed in a dto. |
||
| 168 | * @param array $parameters |
||
| 169 | * @throws UnknownPropertiesDtoException |
||
| 170 | */ |
||
| 171 | protected function processRemainingProperties(array $parameters) |
||
| 172 | { |
||
| 173 | if (count($parameters)) { |
||
| 174 | throw new UnknownPropertiesDtoException(array_keys($parameters), static::class); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Immutable behavior |
||
| 180 | * Throw error if a user tries to set a property. |
||
| 181 | * @param $name |
||
| 182 | * @param $value |
||
| 183 | * @throws ImmutableDtoException|ImmutablePropertyDtoException|PropertyNotFoundDtoException |
||
| 184 | */ |
||
| 185 | public function __set($name, $value) |
||
| 186 | { |
||
| 187 | if ($this->immutable) { |
||
| 188 | throw new ImmutableDtoException($name); |
||
| 189 | } |
||
| 190 | if (! isset($this->properties[$name])) { |
||
| 191 | throw new PropertyNotFoundDtoException($name, get_class($this)); |
||
| 192 | } |
||
| 193 | |||
| 194 | if ($this->properties[$name]->immutable()) { |
||
| 195 | throw new ImmutablePropertyDtoException($name); |
||
| 196 | } |
||
| 197 | $this->$name = $value; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Proxy through to the properties array. |
||
| 202 | * @param $name |
||
| 203 | * @return mixed |
||
| 204 | */ |
||
| 205 | public function __get($name) |
||
| 206 | { |
||
| 207 | return $this->properties[$name]->getValue(); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @deprecated |
||
| 212 | * @param array $data |
||
| 213 | * @return static |
||
| 214 | */ |
||
| 215 | public static function immutable(array $data): DtoContract |
||
| 216 | { |
||
| 217 | $dto = new static($data); |
||
| 218 | $dto->setImmutable(); |
||
| 219 | |||
| 220 | return $dto; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function isImmutable(): bool |
||
| 224 | { |
||
| 225 | return $this->immutable; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function all(): array |
||
| 229 | { |
||
| 230 | $data = []; |
||
| 231 | |||
| 232 | foreach ($this->properties as $property) { |
||
| 233 | $data[$property->getName()] = $property->getValue(); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $data; |
||
| 237 | } |
||
| 238 | |||
| 239 | public function only(string ...$keys): DtoContract |
||
| 240 | { |
||
| 241 | $this->onlyKeys = array_merge($this->onlyKeys, $keys); |
||
| 242 | |||
| 243 | return $this; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function except(string ...$keys): DtoContract |
||
| 247 | { |
||
| 248 | foreach ($keys as $key) { |
||
| 249 | $property = $this->properties[$key] ?? null; |
||
| 250 | if (isset($property)) { |
||
| 251 | $property->setVisible(false); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function toArray(): array |
||
| 274 | } |
||
| 275 | |||
| 276 | protected function parseArray(array $array): array |
||
| 277 | { |
||
| 278 | foreach ($array as $key => $value) { |
||
| 279 | if ( |
||
| 280 | $value instanceof DataTransferObject |
||
| 281 | || $value instanceof DataTransferObjectCollection |
||
| 282 | ) { |
||
| 283 | $array[$key] = $value->toArray(); |
||
| 296 | } |
||
| 297 | } |
||
| 298 |