Total Complexity | 50 |
Total Lines | 229 |
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 |
||
37 | }; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @throws ReflectionException |
||
42 | */ |
||
43 | protected function setter(string $method, array $args): self |
||
44 | { |
||
45 | $property = $this->getProperty($method); |
||
46 | if (property_exists($this, $property) && !is_null($args[0])) { |
||
47 | $this->$property = $this->normalizeValue($property, $this->getPropertyType($property), $args[0]); |
||
48 | } |
||
49 | |||
50 | return $this; |
||
51 | } |
||
52 | |||
53 | protected function getter(string $method): mixed |
||
54 | { |
||
55 | $property = $this->getProperty($method); |
||
56 | if (property_exists($this, $property)) { |
||
57 | return $this->$property; |
||
58 | } |
||
59 | |||
60 | return null; |
||
61 | } |
||
62 | |||
63 | protected function isser(string $method): bool |
||
64 | { |
||
65 | $property = $this->getProperty($method); |
||
66 | if (property_exists($this, $property)) { |
||
67 | return (bool) $this->$property; |
||
68 | } |
||
69 | |||
70 | $property = "is_" . $property; |
||
71 | if (property_exists($this, $property)) { |
||
72 | return (bool) $this->$property; |
||
73 | } |
||
74 | |||
75 | return false; |
||
76 | } |
||
77 | |||
78 | protected function adder(string $method, array $args): self |
||
79 | { |
||
80 | $property = Inflect::pluralize($this->getProperty($method)); |
||
81 | if (property_exists($this, $property) && $this->$property instanceof Collection) { |
||
82 | $found = $this->$property->find($args[0]); |
||
83 | if (!$found) { |
||
84 | $this->$property->add($args[0]); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | return $this; |
||
89 | } |
||
90 | |||
91 | protected function remover(string $method, array $args): self |
||
92 | { |
||
93 | $property = Inflect::pluralize($this->getProperty($method)); |
||
94 | if (property_exists($this, $property) && $this->$property instanceof Collection) { |
||
95 | $found = $this->$property->find($args[0]); |
||
96 | if ($found) { |
||
97 | $key = $this->$property->indexOf($found); |
||
98 | $this->$property->remove($key); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | protected function getSetter(string $property): string |
||
108 | } |
||
109 | |||
110 | protected function getGetter(string $property): string |
||
111 | { |
||
112 | return Inflect::GETTER_PREFIX . Inflect::camelize($property); |
||
113 | } |
||
114 | |||
115 | protected function getIsser(string $property): string |
||
116 | { |
||
117 | return Inflect::ISSER_PREFIX . Inflect::camelize($property); |
||
118 | } |
||
119 | |||
120 | protected function getAdder(string $property): string |
||
121 | { |
||
122 | return Inflect::ADDER_PREFIX . Inflect::camelize($property); |
||
123 | } |
||
124 | |||
125 | protected function getRemover(string $property): string |
||
126 | { |
||
127 | return Inflect::REMOVER_PREFIX . Inflect::camelize($property); |
||
128 | } |
||
129 | |||
130 | protected function getProperty(string $method): string |
||
131 | { |
||
132 | $test = preg_match('/[A-Z]/', $method, $matches, PREG_OFFSET_CAPTURE); |
||
133 | if ($test) { |
||
134 | return Inflect::snakeize(substr($method, $matches[0][1])); |
||
135 | } |
||
136 | |||
137 | return $method; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @throws ReflectionException |
||
142 | */ |
||
143 | protected function getPropertyType(string $property): string |
||
144 | { |
||
145 | $p = new ReflectionProperty($this, $property); |
||
146 | return $p->getType()?->getName(); |
||
|
|||
147 | } |
||
148 | |||
149 | protected function normalizeValue(string $property, string $type, mixed $raw_value): mixed |
||
150 | { |
||
151 | if ($this->isRelatedModel($type)) { |
||
152 | if ($raw_value instanceof $type) { |
||
153 | return $raw_value; |
||
154 | } |
||
155 | |||
156 | $value = new $type(); |
||
157 | $value->fromArray($raw_value); |
||
158 | |||
159 | return $value; |
||
160 | } |
||
161 | |||
162 | if (is_array($raw_value) && $this->isCollection($type)) { |
||
163 | return $this->populateRelation($property, $type, $raw_value); |
||
164 | } |
||
165 | |||
166 | if ($raw_value instanceof Collection && $this->isCollection($type)) { |
||
167 | if (get_class($raw_value) !== $type) { |
||
168 | if (method_exists($raw_value, "getElements")) { |
||
169 | return new $type($raw_value->getElements()); |
||
170 | } |
||
171 | |||
172 | return new $type($raw_value->toArray()); |
||
173 | } |
||
174 | |||
175 | return $raw_value; |
||
176 | } |
||
177 | |||
178 | if ($type === CarbonImmutable::class) { |
||
179 | if ($raw_value instanceof CarbonImmutable) { |
||
180 | return $raw_value; |
||
181 | } |
||
182 | |||
183 | if ($raw_value instanceof DateTimeImmutable) { |
||
184 | return CarbonImmutable::createFromTimestamp($raw_value->getTimestamp()); |
||
185 | } |
||
186 | |||
187 | $timestamp = strtotime($raw_value); |
||
188 | return Carbon::createFromTimestamp($timestamp)->toImmutable(); |
||
189 | } |
||
190 | |||
191 | if ($this->isDirectType($type)) { |
||
192 | return $raw_value; |
||
193 | } |
||
194 | |||
195 | return null; |
||
196 | } |
||
197 | |||
198 | protected function isDirectType(string $type): bool |
||
201 | } |
||
202 | |||
203 | protected function populateRelation(string $property, string $type, array $raw_value): ?ArrayCollection |
||
226 | } |
||
227 | |||
228 | protected function isRelatedModel(string $type): bool |
||
229 | { |
||
230 | if (class_exists($type)) { |
||
231 | $interfaces = class_implements($type); |
||
232 | return $interfaces && in_array(ModelInterface::class, $interfaces, true); |
||
233 | } |
||
234 | |||
235 | return false; |
||
236 | } |
||
237 | |||
238 | protected function isCollection(string $type): bool |
||
246 | } |
||
247 | } |
||
248 |