Total Complexity | 42 |
Total Lines | 329 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | abstract class Model |
||
6 | { |
||
7 | /** @var object|null */ |
||
8 | protected $data; |
||
9 | |||
10 | /** @var \PDOException|null */ |
||
11 | protected $fail; |
||
12 | |||
13 | /** @var string|null */ |
||
14 | protected $error; |
||
15 | |||
16 | /** @var string */ |
||
17 | |||
18 | protected $query; |
||
19 | /** @var string */ |
||
20 | |||
21 | protected $params; |
||
22 | /** @var string */ |
||
23 | |||
24 | /** @var string */ |
||
25 | protected $order; |
||
26 | |||
27 | /** @var int */ |
||
28 | protected $limit; |
||
29 | |||
30 | /** @var int */ |
||
31 | protected $offset; |
||
32 | |||
33 | /** @var string $entity database table */ |
||
34 | protected static $entity; |
||
35 | |||
36 | /** @var array $protected no update or create */ |
||
37 | protected static $protected; |
||
38 | |||
39 | /** @var array $entity database table */ |
||
40 | protected static $required; |
||
41 | |||
42 | /** |
||
43 | * Model constructor. |
||
44 | * @param string $entity database table name |
||
45 | * @param array $protected table protected columns |
||
46 | * @param array $required table required columns |
||
47 | */ |
||
48 | public function __construct(string $entity, array $protected, array $required) |
||
49 | { |
||
50 | self::$entity = $entity; |
||
51 | self::$protected = array_merge($protected, ['created_at', "updated_at"]); |
||
52 | self::$required = $required; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param $name |
||
57 | * @param $value |
||
58 | */ |
||
59 | public function __set($name, $value) |
||
60 | { |
||
61 | if (empty($this->data)) { |
||
62 | $this->data = new \stdClass(); |
||
63 | } |
||
64 | |||
65 | $this->data->$name = $value; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param $name |
||
70 | * @return bool |
||
71 | */ |
||
72 | public function __isset($name) |
||
73 | { |
||
74 | return isset($this->data->$name); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param $name |
||
79 | * @return null |
||
80 | */ |
||
81 | public function __get($name) |
||
82 | { |
||
83 | return ($this->data->$name ?? null); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return null|object |
||
88 | */ |
||
89 | public function data(): ?object |
||
90 | { |
||
91 | return $this->data; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return \PDOException |
||
96 | */ |
||
97 | public function fail(): ?\PDOException |
||
98 | { |
||
99 | return $this->fail; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @return null|string |
||
104 | */ |
||
105 | public function message(): ?string |
||
106 | { |
||
107 | return $this->error; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param |
||
112 | * @return Model|mixed |
||
113 | */ |
||
|
|||
114 | public function find(?string $terms = null, ?string $params = null, string $columns = "*") |
||
115 | { |
||
116 | if ($terms) { |
||
117 | $this->query = "SELECT {$columns} FROM " . static::$entity . " WHERE {$terms}"; |
||
118 | |||
119 | parse_str($params, $this->params); |
||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | $this->query = "SELECT {$columns} FROM " . static::$entity; |
||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param int $id |
||
129 | * @param string $columns |
||
130 | * @return null|mixed|Model |
||
131 | */ |
||
132 | public function findById(int $id, string $columns = "*"): ?Model |
||
133 | { |
||
134 | $find = $this->find("id = :id", "id={$id}", $columns); |
||
135 | |||
136 | return $find->fetch(); |
||
137 | } |
||
138 | |||
139 | public function findAll(int $limit = null, int $offset = null): ?array |
||
140 | { |
||
141 | $find = $this->find(); |
||
142 | |||
143 | if (!empty($limit)) { |
||
144 | $find = $find->limit($limit); |
||
145 | } |
||
146 | |||
147 | if (!empty($offset)) { |
||
148 | $find = $find->offset($offset); |
||
149 | } |
||
150 | |||
151 | $find = $find->fetch(true); |
||
152 | |||
153 | if (empty($find)) { |
||
154 | return null; |
||
155 | } |
||
156 | |||
157 | $return = []; |
||
158 | foreach ($find as $key => $value) { |
||
159 | $return[] = $value->data(); |
||
160 | } |
||
161 | |||
162 | return $return; |
||
163 | } |
||
164 | |||
165 | public function order(string $columnOrder): Model |
||
166 | { |
||
167 | $this->order = " ORDER BY {$columnOrder}"; |
||
168 | |||
169 | return $this; |
||
170 | } |
||
171 | |||
172 | public function limit(int $limit): Model |
||
173 | { |
||
174 | $this->limit = " LIMIT {$limit}"; |
||
175 | |||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | public function offset(int $offset): Model |
||
180 | { |
||
181 | $this->offset = " OFFSET {$offset}"; |
||
182 | |||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | public function fetch(bool $all = false) |
||
187 | { |
||
188 | try { |
||
189 | $stmt = Connect::getInstance()->prepare($this->query . $this->order . $this->limit . $this->offset); |
||
190 | $stmt->execute($this->params); |
||
191 | |||
192 | if (!$stmt->rowCount()) { |
||
193 | return null; |
||
194 | } |
||
195 | |||
196 | if ($all) { |
||
197 | return $stmt->fetchAll(\PDO::FETCH_CLASS, static::class); |
||
198 | } |
||
199 | |||
200 | return $stmt->fetchObject(static::class); |
||
201 | } catch (\PDOException $exception) { |
||
202 | $this->fail = $exception; |
||
203 | return null; |
||
204 | } |
||
205 | } |
||
206 | |||
207 | public function count(string $key = "id"): int |
||
208 | { |
||
209 | $stmt = Connect::getInstance()->prepare($this->query); |
||
210 | $stmt->execute($this->params); |
||
211 | |||
212 | return $stmt->rowCount(); |
||
213 | } |
||
214 | |||
215 | public function rawQuery(string $query): self |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param string $entity |
||
223 | * @param array $data |
||
224 | * @return int|null |
||
225 | */ |
||
226 | protected function create(array $data): ?int |
||
227 | { |
||
228 | try { |
||
229 | $columns = implode(", ", array_keys($data)); |
||
230 | $values = ":" . implode(", :", array_keys($data)); |
||
231 | |||
232 | $stmt = Connect::getInstance()->prepare("INSERT INTO " . static::$entity . " ({$columns}) VALUES ({$values})"); |
||
233 | $stmt->execute($this->filter($data)); |
||
234 | |||
235 | return Connect::getInstance()->lastInsertId(); |
||
236 | } catch (\PDOException $exception) { |
||
237 | $this->fail = $exception; |
||
238 | return null; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @param string $entity |
||
244 | * @param array $data |
||
245 | * @param string $terms |
||
246 | * @param string $params |
||
247 | * @return int|null |
||
248 | */ |
||
249 | protected function update(array $data, string $terms, string $params): ?int |
||
250 | { |
||
251 | try { |
||
252 | $dateSet = []; |
||
253 | foreach ($data as $bind => $value) { |
||
254 | $dateSet[] = "{$bind} = :{$bind}"; |
||
255 | } |
||
256 | $dateSet = implode(", ", $dateSet); |
||
257 | parse_str($params, $params); |
||
258 | |||
259 | $stmt = Connect::getInstance()->prepare("UPDATE " . static::$entity . " SET {$dateSet} WHERE {$terms}"); |
||
260 | $stmt->execute($this->filter(array_merge($data, $params))); |
||
261 | return ($stmt->rowCount() ?? 1); |
||
262 | } catch (\PDOException $exception) { |
||
263 | $this->fail = $exception; |
||
264 | return null; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | public function delete(string $key, string $value): bool |
||
279 | } |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @return array|null |
||
284 | */ |
||
285 | protected function safe(): ?array |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @param array $data |
||
298 | * @return array|null |
||
299 | */ |
||
300 | private function filter(array $data): ?array |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @return bool |
||
312 | */ |
||
313 | protected function required(): bool |
||
324 | } |
||
325 | |||
326 | public function getRequired(): array |
||
336 |