| Total Complexity | 55 |
| Total Lines | 227 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ModelTrait 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 ModelTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | trait ModelTrait |
||
| 18 | { |
||
| 19 | protected static array $direct_types = [ |
||
| 20 | "int", "string", "float", "array", "bool", "mixed" |
||
| 21 | ]; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @throws ReflectionException |
||
| 25 | */ |
||
| 26 | public function __call(string $method, array $args): mixed |
||
| 27 | { |
||
| 28 | if ($this->isSetter($method)) { |
||
| 29 | $property = $this->getProperty($method); |
||
| 30 | if (property_exists($this, $property) && !is_null($args[0])) { |
||
| 31 | $this->$property = $this->normalizeValue($property, $this->getPropertyType($property), $args[0]); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | if ($this->isGetter($method)) { |
||
| 36 | $property = $this->getProperty($method); |
||
| 37 | if (property_exists($this, $property)) { |
||
| 38 | return $this->$property; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | if ($this->isIsser($method)) { |
||
| 43 | $property = $this->getProperty($method); |
||
| 44 | if (property_exists($this, $property)) { |
||
| 45 | return $this->$property; |
||
| 46 | } |
||
| 47 | |||
| 48 | $property = "is_" . $property; |
||
| 49 | if (property_exists($this, $property)) { |
||
| 50 | return $this->$property; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | if ($this->isAdder($method)) { |
||
| 55 | $property = Inflect::pluralize($this->getProperty($method)); |
||
| 56 | if (property_exists($this, $property) && $this->$property instanceof Collection) { |
||
| 57 | $found = $this->$property->find($args[0]); |
||
| 58 | if (!$found) { |
||
| 59 | $this->$property->add($args[0]); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($this->isRemover($method)) { |
||
| 65 | $property = Inflect::pluralize($this->getProperty($method)); |
||
| 66 | if (property_exists($this, $property) && $this->$property instanceof Collection) { |
||
| 67 | $found = $this->$property->find($args[0]); |
||
| 68 | if ($found) { |
||
| 69 | $key = $this->$property->indexOf($found); |
||
| 70 | $this->$property->remove($key); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | return null; |
||
| 76 | } |
||
| 77 | |||
| 78 | protected function isSetter(string $method): bool |
||
| 81 | } |
||
| 82 | |||
| 83 | protected function isGetter(string $method): bool |
||
| 84 | { |
||
| 85 | return str_starts_with($method, "get"); |
||
| 86 | } |
||
| 87 | |||
| 88 | protected function isIsser(string $method): bool |
||
| 89 | { |
||
| 90 | return str_starts_with($method, "is"); |
||
| 91 | } |
||
| 92 | |||
| 93 | protected function isAdder(string $method): bool |
||
| 94 | { |
||
| 95 | return str_starts_with($method, "add"); |
||
| 96 | } |
||
| 97 | |||
| 98 | protected function isRemover(string $method): bool |
||
| 99 | { |
||
| 100 | return str_starts_with($method, "remove"); |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function getSetter(string $property): string |
||
| 106 | } |
||
| 107 | |||
| 108 | protected function getGetter(string $property): string |
||
| 109 | { |
||
| 110 | return "get" . Inflect::camelize($property); |
||
| 111 | } |
||
| 112 | |||
| 113 | protected function getIsser(string $property): string |
||
| 114 | { |
||
| 115 | return "is" . Inflect::camelize($property); |
||
| 116 | } |
||
| 117 | |||
| 118 | protected function getAdder(string $property): string |
||
| 119 | { |
||
| 120 | return "add" . Inflect::camelize($property); |
||
| 121 | } |
||
| 122 | |||
| 123 | protected function getRemover(string $property): string |
||
| 124 | { |
||
| 125 | return "remove" . Inflect::camelize($property); |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function getProperty(string $method): string |
||
| 129 | { |
||
| 130 | $test = preg_match('/[A-Z]/', $method, $matches, PREG_OFFSET_CAPTURE); |
||
| 131 | if ($test) { |
||
| 132 | return Inflect::snakeize(substr($method, $matches[0][1])); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $method; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @throws ReflectionException |
||
| 140 | */ |
||
| 141 | protected function getPropertyType(string $property): string |
||
| 142 | { |
||
| 143 | $p = new ReflectionProperty($this, $property); |
||
| 144 | return $p->getType()?->getName(); |
||
|
|
|||
| 145 | } |
||
| 146 | |||
| 147 | protected function normalizeValue(string $property, string $type, mixed $raw_value): mixed |
||
| 148 | { |
||
| 149 | if ($this->isRelatedModel($type)) { |
||
| 150 | if ($raw_value instanceof $type) { |
||
| 151 | return $raw_value; |
||
| 152 | } |
||
| 153 | |||
| 154 | $value = new $type(); |
||
| 155 | $value->fromArray($raw_value); |
||
| 156 | |||
| 157 | return $value; |
||
| 158 | } |
||
| 159 | |||
| 160 | if (is_array($raw_value) && $this->isCollection($type)) { |
||
| 161 | return $this->populateRelation($property, $type, $raw_value); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($raw_value instanceof Collection && $this->isCollection($type)) { |
||
| 165 | if (get_class($raw_value) !== $type) { |
||
| 166 | if (method_exists($raw_value, "getElements")) { |
||
| 167 | return new $type($raw_value->getElements()); |
||
| 168 | } |
||
| 169 | |||
| 170 | return new $type($raw_value->toArray()); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $raw_value; |
||
| 174 | } |
||
| 175 | |||
| 176 | if ($type === CarbonImmutable::class) { |
||
| 177 | if ($raw_value instanceof CarbonImmutable) { |
||
| 178 | return $raw_value; |
||
| 179 | } |
||
| 180 | |||
| 181 | if ($raw_value instanceof DateTimeImmutable) { |
||
| 182 | return CarbonImmutable::createFromTimestamp($raw_value->getTimestamp()); |
||
| 183 | } |
||
| 184 | |||
| 185 | $timestamp = strtotime($raw_value); |
||
| 186 | return Carbon::createFromTimestamp($timestamp)->toImmutable(); |
||
| 187 | } |
||
| 188 | |||
| 189 | if ($this->isDirectType($type)) { |
||
| 190 | return $raw_value; |
||
| 191 | } |
||
| 192 | |||
| 193 | return null; |
||
| 194 | } |
||
| 195 | |||
| 196 | protected function isDirectType(string $type): bool |
||
| 199 | } |
||
| 200 | |||
| 201 | protected function populateRelation(string $property, string $type, array $raw_value): ?ArrayCollection |
||
| 224 | } |
||
| 225 | |||
| 226 | protected function isRelatedModel(string $type): bool |
||
| 227 | { |
||
| 228 | if (class_exists($type)) { |
||
| 229 | $interfaces = class_implements($type); |
||
| 230 | return $interfaces && in_array(ModelInterface::class, $interfaces, true); |
||
| 231 | } |
||
| 232 | |||
| 233 | return false; |
||
| 234 | } |
||
| 235 | |||
| 236 | protected function isCollection(string $type): bool |
||
| 244 | } |
||
| 245 | } |
||
| 246 |