Total Complexity | 60 |
Total Lines | 344 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PersistableObjectHandler 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 PersistableObjectHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class PersistableObjectHandler extends \XoopsPersistableObjectHandler //XoopsObjectHandler |
||
33 | { |
||
34 | /**#@+ |
||
35 | * Information about the class, the handler is managing |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | // public $table; |
||
40 | // public $keyName; |
||
41 | // public $className; |
||
42 | // public $identifierName; |
||
43 | /**#@-*/ |
||
44 | |||
45 | /** |
||
46 | * Constructor - called from child classes |
||
47 | * |
||
48 | * @param \XoopsDatabase|null $db {@link XoopsDatabase} |
||
49 | * object |
||
50 | * @param string $tablename Name of database table |
||
51 | * @param string $classname Name of Class, this handler is managing |
||
52 | * @param string $keyname Name of the property, holding the key |
||
53 | * |
||
54 | * @param bool $idenfierName |
||
55 | */ |
||
56 | public function __construct(\XoopsDatabase $db, $tablename, $classname, $keyname, $idenfierName = false) |
||
57 | { |
||
58 | parent::__construct($db); |
||
59 | // $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
60 | $this->table = $db->prefix($tablename); |
||
61 | $this->keyName = $keyname; |
||
62 | // $this->className = '\\XoopsModules\\Extgallery\\' .$classname; |
||
63 | $this->className = $classname; |
||
64 | if (false !== $idenfierName) { |
||
65 | $this->identifierName = $idenfierName; |
||
|
|||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * create a new user |
||
71 | * |
||
72 | * @param bool $isNew Flag the new objects as "new"? |
||
73 | * |
||
74 | * @return \XoopsObject |
||
75 | */ |
||
76 | public function create($isNew = true) |
||
77 | { |
||
78 | $temp = '\\XoopsModules\\Extgallery\\' . $this->className; |
||
79 | $obj = new $temp(); |
||
80 | |||
81 | if (true === $isNew) { |
||
82 | $obj->setNew(); |
||
83 | } |
||
84 | |||
85 | return $obj; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * delete an object from the database |
||
90 | * |
||
91 | * @param mixed $id id of the object to delete |
||
92 | * @param bool $force |
||
93 | * @return bool FALSE if failed. |
||
94 | */ |
||
95 | public function deleteById($id, $force = false) |
||
96 | { |
||
97 | if (\is_array($this->keyName)) { |
||
98 | $clause = []; |
||
99 | for ($i = 0, $iMax = \count($this->keyName); $i < $iMax; ++$i) { |
||
100 | $clause[] = $this->keyName[$i] . ' = ' . $id[$i]; |
||
101 | } |
||
102 | $whereclause = \implode(' AND ', $clause); |
||
103 | } else { |
||
104 | $whereclause = $this->keyName . ' = ' . $id; |
||
105 | } |
||
106 | $sql = 'DELETE FROM ' . $this->table . ' WHERE ' . $whereclause; |
||
107 | if (false !== $force) { |
||
108 | $result = $this->db->queryF($sql); |
||
109 | } else { |
||
110 | $result = $this->db->query($sql); |
||
111 | } |
||
112 | if (!$result) { |
||
113 | return false; |
||
114 | } |
||
115 | |||
116 | return true; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param $fieldname |
||
121 | * @param $fieldvalue |
||
122 | * @param null $criteria |
||
123 | * @param bool $force |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function updateFieldValue($fieldname, $fieldvalue, $criteria = null, $force = true) |
||
128 | { |
||
129 | $sql = 'UPDATE ' . $this->table . ' SET ' . $fieldname . ' = ' . $fieldvalue; |
||
130 | if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) { |
||
131 | $sql .= ' ' . $criteria->renderWhere(); |
||
132 | } |
||
133 | $result = false !== $force ? $this->db->queryF($sql) : $this->db->query($sql); |
||
134 | if (!$result) { |
||
135 | return false; |
||
136 | } |
||
137 | |||
138 | return true; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param $data |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | public function _toObject($data) |
||
147 | { |
||
148 | if (\is_array($data)) { |
||
149 | $ret = []; |
||
150 | foreach ($data as $v) { |
||
151 | $object = new $this->className(); |
||
152 | $object->assignVars($v); |
||
153 | $ret[] = $object; |
||
154 | } |
||
155 | |||
156 | return $ret; |
||
157 | } |
||
158 | $object = new $this->className(); |
||
159 | $object->assignVars($v); |
||
160 | |||
161 | return $object; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param $objects |
||
166 | * @param array $externalKeys |
||
167 | * @param string $format |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | public function objectToArray($objects, $externalKeys = [], $format = 's') |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @param $object |
||
231 | * @param string $format |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | public function objectToArrayWithoutExternalKey($object, $format = 's') |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param $fieldname |
||
250 | * @param $criteria |
||
251 | * @param string $op |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function updateCounter($fieldname, $criteria, $op = '+') |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param null|\CriteriaElement $criteria |
||
269 | * @param string $sum |
||
270 | * |
||
271 | * @return array|int|string |
||
272 | */ |
||
273 | public function getSum(\CriteriaElement $criteria = null, $sum = '*') |
||
274 | { |
||
275 | $field = ''; |
||
276 | $groupby = false; |
||
277 | if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) { |
||
278 | if ('' != $criteria->groupby) { |
||
279 | $groupby = true; |
||
280 | $field = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used |
||
281 | } |
||
282 | } |
||
283 | $sql = 'SELECT ' . $field . "SUM($sum) FROM " . $this->table; |
||
284 | if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) { |
||
285 | $sql .= ' ' . $criteria->renderWhere(); |
||
286 | if ('' != $criteria->groupby) { |
||
287 | $sql .= $criteria->getGroupby(); |
||
288 | } |
||
289 | } |
||
290 | $result = $this->db->query($sql); |
||
291 | if (!$result) { |
||
292 | return 0; |
||
293 | } |
||
294 | if (false === $groupby) { |
||
295 | [$sum] = $this->db->fetchRow($result); |
||
296 | |||
297 | return $sum; |
||
298 | } |
||
299 | $ret = []; |
||
300 | while (list($id, $sum) = $this->db->fetchRow($result)) { |
||
301 | $ret[$id] = $sum; |
||
302 | } |
||
303 | |||
304 | return $ret; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @param \CriteriaElement $criteria |
||
309 | * @param string $max |
||
310 | * |
||
311 | * @return array|int|string |
||
312 | */ |
||
313 | public function getMax(\CriteriaElement $criteria = null, $max = '*') |
||
314 | { |
||
315 | $field = ''; |
||
316 | $groupby = false; |
||
317 | if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) { |
||
318 | if ('' != $criteria->groupby) { |
||
319 | $groupby = true; |
||
320 | $field = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used |
||
321 | } |
||
322 | } |
||
323 | $sql = 'SELECT ' . $field . "MAX($max) FROM " . $this->table; |
||
324 | if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) { |
||
325 | $sql .= ' ' . $criteria->renderWhere(); |
||
326 | if ('' != $criteria->groupby) { |
||
327 | $sql .= $criteria->getGroupby(); |
||
328 | } |
||
329 | } |
||
330 | $result = $this->db->query($sql); |
||
331 | if (!$result) { |
||
332 | return 0; |
||
333 | } |
||
334 | if (false === $groupby) { |
||
335 | [$max] = $this->db->fetchRow($result); |
||
336 | |||
337 | return $max; |
||
338 | } |
||
339 | $ret = []; |
||
340 | while (list($id, $max) = $this->db->fetchRow($result)) { |
||
341 | $ret[$id] = $max; |
||
342 | } |
||
343 | |||
344 | return $ret; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @param \CriteriaElement $criteria |
||
349 | * @param string $avg |
||
350 | * |
||
351 | * @return int |
||
352 | */ |
||
353 | public function getAvg(\CriteriaElement $criteria = null, $avg = '*') |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @return mixed |
||
372 | */ |
||
373 | public function getInsertId() |
||
376 | } |
||
377 | } |
||
378 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.