| Total Complexity | 58 |
| Total Lines | 234 |
| 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 |
||
| 20 | abstract class DataTransferObject implements DtoContract |
||
| 21 | { |
||
| 22 | |||
| 23 | /** @var array */ |
||
| 24 | protected $onlyKeys = []; |
||
| 25 | |||
| 26 | /** @var array */ |
||
| 27 | protected $with = []; |
||
| 28 | |||
| 29 | /** @var Property[] */ |
||
| 30 | protected $properties = []; |
||
| 31 | |||
| 32 | /** @var bool */ |
||
| 33 | protected $immutable = false; |
||
| 34 | |||
| 35 | |||
| 36 | public function __construct(array $parameters) |
||
| 37 | { |
||
| 38 | $this->boot($parameters); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Boot the dto and process all parameters. |
||
| 43 | * @param array $parameters |
||
| 44 | * @throws \ReflectionException |
||
| 45 | */ |
||
| 46 | protected function boot(array $parameters): void |
||
| 47 | { |
||
| 48 | $this->properties = (new PropertyFactory($this))->build($parameters); |
||
| 49 | $this->determineImmutability(); |
||
| 50 | if($this instanceof AutomaticValidation) |
||
| 51 | $this->validate(); |
||
| 52 | } |
||
| 53 | |||
| 54 | protected function determineImmutability() |
||
| 55 | { |
||
| 56 | /* If the dto itself is not immutable but some properties are chain them immutable */ |
||
| 57 | foreach ($this->properties as $property) { |
||
| 58 | if ($property->isImmutable()) { |
||
| 59 | $this->chainPropertyImmutable($property); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | protected function setImmutable(): void |
||
| 65 | { |
||
| 66 | if (!$this->isImmutable()) { |
||
| 67 | $this->immutable = true; |
||
| 68 | foreach ($this->properties as $property) { |
||
| 69 | $this->chainPropertyImmutable($property); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | protected function chainPropertyImmutable(Property $property) |
||
| 75 | { |
||
| 76 | $dto = $property->getValue(); |
||
| 77 | if ($dto instanceof DataTransferObject) { |
||
| 78 | $dto->setImmutable(); |
||
| 79 | } elseif (is_iterable($dto)) { |
||
| 80 | foreach ($dto as $aPotentialDto) { |
||
| 81 | if ($aPotentialDto instanceof DataTransferObject) { |
||
| 82 | $aPotentialDto->setImmutable(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * Immutable behavior |
||
| 91 | * Throw error if a user tries to set a property. |
||
| 92 | * @param $name |
||
| 93 | * @param $value |
||
| 94 | * @throws ImmutableDtoException|ImmutablePropertyDtoException|PropertyNotFoundDtoException |
||
| 95 | */ |
||
| 96 | public function __set($name, $value) |
||
| 97 | { |
||
| 98 | if ($this->immutable) { |
||
| 99 | throw new ImmutableDtoException($name); |
||
| 100 | } |
||
| 101 | if (!isset($this->properties[$name])) { |
||
| 102 | throw new PropertyNotFoundDtoException($name, get_class($this)); |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($this->properties[$name]->isImmutable()) { |
||
| 106 | throw new ImmutablePropertyDtoException($name); |
||
| 107 | } |
||
| 108 | $this->$name = $value; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function &__get($name) |
||
| 112 | { |
||
| 113 | return $this->properties[$name]->value; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function isImmutable(): bool |
||
| 117 | { |
||
| 118 | return $this->immutable; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function all(): array |
||
| 131 | } |
||
| 132 | |||
| 133 | public function only(string ...$keys): DtoContract |
||
| 134 | { |
||
| 135 | $this->onlyKeys = array_merge($this->onlyKeys, $keys); |
||
| 136 | |||
| 137 | return $this; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function except(string ...$keys): DtoContract |
||
| 141 | { |
||
| 142 | foreach ($keys as $key) { |
||
| 143 | if (array_key_exists($key, $this->with)) { |
||
| 144 | unset($this->with[$key]); |
||
| 145 | } |
||
| 146 | $property = $this->properties[$key] ?? null; |
||
| 147 | if (isset($property)) { |
||
| 148 | $property->setVisible(false); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | |||
| 154 | public function with(string $key, $value): DtoContract |
||
| 155 | { |
||
| 156 | if (array_key_exists($key, $this->properties)) { |
||
| 157 | throw new PropertyAlreadyExistsException($key); |
||
| 158 | } |
||
| 159 | return $this->override($key, $value); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function override(string $key, $value): DtoContract |
||
| 163 | { |
||
| 164 | if ($this->isImmutable()) { |
||
| 165 | throw new ImmutableDtoException($key); |
||
| 166 | } |
||
| 167 | if (($propertyExists = array_key_exists($key, $this->properties) && $this->properties[$key]->isImmutable())) { |
||
| 168 | throw new ImmutablePropertyDtoException($key); |
||
| 169 | } |
||
| 170 | |||
| 171 | if ($propertyExists) { |
||
|
|
|||
| 172 | $property = $this->properties[$key]; |
||
| 173 | $property->set($value); |
||
| 174 | if ($this instanceof AutomaticValidation) |
||
| 175 | $this->validate(); |
||
| 176 | } else { |
||
| 177 | $this->with[$key] = $value; |
||
| 178 | } |
||
| 179 | |||
| 180 | return $this; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function toArray(): array |
||
| 199 | } |
||
| 200 | |||
| 201 | protected function parseArray(array $array): array |
||
| 221 | } |
||
| 222 | |||
| 223 | public function throwValidationException() |
||
| 240 | } |
||
| 241 | |||
| 242 | public function validate() |
||
| 254 | |||
| 255 | } |
||
| 256 | } |
||
| 257 |