Total Complexity | 40 |
Total Lines | 350 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like VideoHandler 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 VideoHandler, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
37 | class VideoHandler extends XoopsPersistableObjectHandler |
||
38 | { |
||
39 | public Helper $helper; |
||
40 | public $isAdmin; |
||
41 | |||
42 | /** |
||
43 | * Constructor |
||
44 | * @param \XoopsDatabase|null $xoopsDatabase |
||
45 | * @param \XoopsModules\Suico\Helper|null $helper |
||
46 | */ |
||
47 | public function __construct( |
||
48 | ?XoopsDatabase $xoopsDatabase = null, |
||
49 | $helper = null |
||
50 | ) { |
||
51 | /** @var \XoopsModules\Suico\Helper $this- >helper */ |
||
52 | if (null === $helper) { |
||
53 | $this->helper = Helper::getInstance(); |
||
54 | } else { |
||
55 | $this->helper = $helper; |
||
56 | } |
||
57 | $this->isAdmin = $this->helper->isUserAdmin(); |
||
58 | parent::__construct($xoopsDatabase, 'suico_videos', Video::class, 'video_id', 'video_title', 'video_desc'); |
||
|
|||
59 | } |
||
60 | |||
61 | /** |
||
62 | * create a new Groups |
||
63 | * |
||
64 | * @param bool $isNew flag the new objects as "new"? |
||
65 | * @return \XoopsObject Groups |
||
66 | */ |
||
67 | public function create( |
||
68 | $isNew = true |
||
69 | ) { |
||
70 | $obj = parent::create($isNew); |
||
71 | if ($isNew) { |
||
72 | $obj->setNew(); |
||
73 | } else { |
||
74 | $obj->unsetNew(); |
||
75 | } |
||
76 | if ($isNew) { |
||
77 | $obj->helper = $this->helper; |
||
78 | } |
||
79 | |||
80 | return $obj; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * retrieve a Video |
||
85 | * |
||
86 | * @param int|null $id of the Video |
||
87 | * @param null $fields |
||
88 | * @return false|\XoopsModules\Suico\Video reference to the {@link Video} object, FALSE if failed |
||
89 | */ |
||
90 | public function get2( |
||
91 | $id = null, |
||
92 | $fields = null |
||
93 | ) { |
||
94 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_videos') . ' WHERE video_id=' . $id; |
||
95 | if (!$result = $this->db->query($sql)) { |
||
96 | return false; |
||
97 | } |
||
98 | $numrows = $this->db->getRowsNum($result); |
||
99 | if (1 === $numrows) { |
||
100 | $video = new Video(); |
||
101 | $video->assignVars($this->db->fetchArray($result)); |
||
102 | |||
103 | return $video; |
||
104 | } |
||
105 | |||
106 | return false; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * insert a new Video in the database |
||
111 | * |
||
112 | * @param \XoopsObject $object reference to the {@link Video} |
||
113 | * object |
||
114 | * @param bool $force |
||
115 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
116 | */ |
||
117 | public function insert2( |
||
118 | XoopsObject $object, |
||
119 | $force = false |
||
120 | ) { |
||
121 | global $xoopsConfig; |
||
122 | if (!$object instanceof Video) { |
||
123 | return false; |
||
124 | } |
||
125 | if (!$object->isDirty()) { |
||
126 | return true; |
||
127 | } |
||
128 | if (!$object->cleanVars()) { |
||
129 | return false; |
||
130 | } |
||
131 | $uid_owner = ''; |
||
132 | $video_id = ''; |
||
133 | foreach ($object->cleanVars as $k => $v) { |
||
134 | ${$k} = $v; |
||
135 | } |
||
136 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
137 | if ($object->isNew()) { |
||
138 | // ajout/modification d'un Video |
||
139 | $object = new Video(); |
||
140 | $format = 'INSERT INTO %s (video_id, uid_owner, video_title, video_desc, youtube_code, featured_video)'; |
||
141 | $format .= 'VALUES (%u, %u, %s, %s, %s, %s)'; |
||
142 | $sql = \sprintf( |
||
143 | $format, |
||
144 | $this->db->prefix('suico_videos'), |
||
145 | $video_id, |
||
146 | $uid_owner, |
||
147 | $this->db->quoteString($video_title), |
||
148 | $this->db->quoteString($video_desc), |
||
149 | $this->db->quoteString($youtube_code), |
||
150 | $this->db->quoteString($featured_video) |
||
151 | ); |
||
152 | $force = true; |
||
153 | } else { |
||
154 | $format = 'UPDATE %s SET '; |
||
155 | $format .= 'video_id=%u, uid_owner=%u, video_title=%s, video_desc=%s, youtube_code=%s, featured_video=%s'; |
||
156 | $format .= ' WHERE video_id = %u'; |
||
157 | $sql = \sprintf( |
||
158 | $format, |
||
159 | $this->db->prefix('suico_videos'), |
||
160 | $video_id, |
||
161 | $uid_owner, |
||
162 | $this->db->quoteString($video_title), |
||
163 | $this->db->quoteString($video_desc), |
||
164 | $this->db->quoteString($youtube_code), |
||
165 | $this->db->quoteString($featured_video), |
||
166 | $video_id |
||
167 | ); |
||
168 | } |
||
169 | if ($force) { |
||
170 | $result = $this->db->queryF($sql); |
||
171 | } else { |
||
172 | $result = $this->db->query($sql); |
||
173 | } |
||
174 | if (!$result) { |
||
175 | return false; |
||
176 | } |
||
177 | if (empty($video_id)) { |
||
178 | $video_id = $this->db->getInsertId(); |
||
179 | } |
||
180 | $object->assignVar('video_id', $video_id); |
||
181 | |||
182 | return true; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * delete a Video from the database |
||
187 | * |
||
188 | * @param \XoopsObject $object reference to the Video to delete |
||
189 | * @param bool $force |
||
190 | * @return bool FALSE if failed. |
||
191 | */ |
||
192 | public function delete( |
||
193 | XoopsObject $object, |
||
194 | $force = false |
||
195 | ) { |
||
196 | if (!$object instanceof Video) { |
||
197 | return false; |
||
198 | } |
||
199 | $sql = \sprintf( |
||
200 | 'DELETE FROM %s WHERE video_id = %u', |
||
201 | $this->db->prefix('suico_videos'), |
||
202 | (int)$object->getVar('video_id') |
||
203 | ); |
||
204 | if ($force) { |
||
205 | $result = $this->db->queryF($sql); |
||
206 | } else { |
||
207 | $result = $this->db->query($sql); |
||
208 | } |
||
209 | if (!$result) { |
||
210 | return false; |
||
211 | } |
||
212 | |||
213 | return true; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * retrieve suico_videos from the database |
||
218 | * |
||
219 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met |
||
220 | * @param bool $id_as_key use the UID as key for the array? |
||
221 | * @param bool $as_object |
||
222 | * @return array array of {@link Video} objects |
||
223 | */ |
||
224 | public function &getObjects( |
||
225 | ?CriteriaElement $criteria = null, |
||
226 | $id_as_key = false, |
||
227 | $as_object = true |
||
228 | ) { |
||
229 | $ret = []; |
||
230 | $start = 0; |
||
231 | $limit = 0; |
||
232 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_videos'); |
||
233 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
234 | $sql .= ' ' . $criteria->renderWhere(); |
||
235 | $sort = 'video_id'; |
||
236 | $order = 'DESC'; |
||
237 | //if ('' !== $criteria->getSort()) { |
||
238 | // $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
239 | //} |
||
240 | if ('' !== $sort) { |
||
241 | $sql .= ' ORDER BY ' . $sort . ' ' . $order; |
||
242 | } |
||
243 | $limit = $criteria->getLimit(); |
||
244 | $start = $criteria->getStart(); |
||
245 | } |
||
246 | $result = $this->db->query($sql, $limit, $start); |
||
247 | if (!$result) { |
||
248 | return $ret; |
||
249 | } |
||
250 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
251 | $video = new Video(); |
||
252 | $video->assignVars($myrow); |
||
253 | if ($id_as_key) { |
||
254 | $ret[$myrow['video_id']] = &$video; |
||
255 | } else { |
||
256 | $ret[] = &$video; |
||
257 | } |
||
258 | unset($video); |
||
259 | } |
||
260 | |||
261 | return $ret; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * count suico_videos matching a condition |
||
266 | * |
||
267 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} to match |
||
268 | * @return int count of suico_videos |
||
269 | */ |
||
270 | public function getCount( |
||
271 | ?CriteriaElement $criteria = null |
||
272 | ) { |
||
273 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_videos'); |
||
274 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
275 | $sql .= ' ' . $criteria->renderWhere(); |
||
276 | } |
||
277 | $result = $this->db->query($sql); |
||
278 | if (!$result) { |
||
279 | return 0; |
||
280 | } |
||
281 | [$count] = $this->db->fetchRow($result); |
||
282 | |||
283 | return (int)$count; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * delete suico_videos matching a set of conditions |
||
288 | * |
||
289 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} |
||
290 | * @param bool $force |
||
291 | * @param bool $asObject |
||
292 | * @return bool FALSE if deletion failed |
||
293 | */ |
||
294 | public function deleteAll( |
||
295 | ?CriteriaElement $criteria = null, |
||
296 | $force = true, |
||
297 | $asObject = false |
||
298 | ) { |
||
299 | $sql = 'DELETE FROM ' . $this->db->prefix('suico_videos'); |
||
300 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
301 | $sql .= ' ' . $criteria->renderWhere(); |
||
302 | } |
||
303 | if (!$result = $this->db->query($sql)) { |
||
304 | return false; |
||
305 | } |
||
306 | |||
307 | return true; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Render a form to send videos |
||
312 | * |
||
313 | * @param \XoopsTpl $xoopsTpl the one in which the form will be rendered |
||
314 | * @return bool TRUE |
||
315 | * |
||
316 | * obs: Some functions wont work on php 4 so edit lines down under acording to your version |
||
317 | */ |
||
318 | public function renderFormSubmit( |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Render a form to edit the description of the pictures |
||
337 | * |
||
338 | * @param $title |
||
339 | * @param string $caption The description of the picture |
||
340 | * @param int $video_id the id of the image in database |
||
341 | * @param string $filename the url to the thumb of the image so it can be displayed |
||
342 | * @return bool TRUE |
||
343 | */ |
||
344 | public function renderFormEdit( |
||
345 | $title, |
||
346 | $caption, |
||
347 | $video_id, |
||
348 | $filename |
||
349 | ) { |
||
350 | $form = new XoopsThemeForm(\_MD_SUICO_EDIT_VIDEO, 'form_picture', 'editvideo.php', 'post', true); |
||
351 | $field_title = new XoopsFormText($title, 'title', 35, 55); |
||
352 | $field_desc = new XoopsFormText($caption, 'caption', 35, 55); |
||
353 | $form->setExtra('enctype="multipart/form-data"'); |
||
354 | $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_SUBMIT, 'submit'); |
||
355 | $field_warning = new XoopsFormLabel( |
||
356 | '<object width="425" height="353"> |
||
357 | <param name="movie" value="https://www.youtube.com/v/' . $filename . '"></param> |
||
358 | <param name="wmode" value="transparent"></param> |
||
359 | <embed src="https://www.youtube.com/v/' . $filename . '" type="application/x-shockwave-flash" wmode="transparent" width="425" height="353"></embed> |
||
360 | </object>' |
||
361 | ); |
||
362 | $field_video_id = new XoopsFormHidden('video_id', $video_id); |
||
363 | $field_marker = new XoopsFormHidden('marker', 1); |
||
364 | $form->addElement($field_warning); |
||
365 | $form->addElement($field_title); |
||
366 | $form->addElement($field_desc); |
||
367 | $form->addElement($field_video_id, true); |
||
368 | $form->addElement($field_marker); |
||
369 | $form->addElement($buttonSend); |
||
370 | $form->display(); |
||
371 | |||
372 | return true; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @param null $uid_owner |
||
377 | * @return bool |
||
378 | */ |
||
379 | public function unsetAllMainsbyID($uid_owner = null) |
||
387 | } |
||
388 | } |
||
389 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.