Total Complexity | 89 |
Total Lines | 553 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PostHandler 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 PostHandler, and based on these observations, apply Extract Interface, too.
1 | <?php namespace XoopsModules\Newbb; |
||
42 | class PostHandler extends \XoopsPersistableObjectHandler |
||
43 | { |
||
44 | /** |
||
45 | * @param \XoopsDatabase $db |
||
46 | */ |
||
47 | public function __construct(\XoopsDatabase $db) |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param mixed $id |
||
54 | * @param null $var |
||
55 | * @return null|\XoopsObject |
||
56 | */ |
||
57 | public function get($id = null, $var = null) //get($id) |
||
58 | { |
||
59 | $id = (int)$id; |
||
60 | $post = null; |
||
61 | $sql = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' t ON p.post_id=t.post_id WHERE p.post_id=' . $id; |
||
62 | if ($array = $this->db->fetchArray($this->db->query($sql))) { |
||
63 | $post = $this->create(false); |
||
64 | $post->assignVars($array); |
||
65 | } |
||
66 | |||
67 | return $post; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param int $limit |
||
72 | * @param int $start |
||
73 | * @param \CriteriaElement $criteria |
||
74 | * @param null $fields |
||
75 | * @param bool $asObject |
||
76 | * @param int $topic_id |
||
77 | * @param int $approved |
||
78 | * @return array |
||
79 | */ |
||
80 | // public function getByLimit($topic_id, $limit, $approved = 1) |
||
81 | public function &getByLimit( |
||
82 | $limit = 0, |
||
83 | $start = 0, |
||
84 | \CriteriaElement $criteria = null, |
||
85 | $fields = null, |
||
86 | $asObject = true, |
||
87 | $topic_id = 0, |
||
88 | $approved = 1 |
||
89 | ) { |
||
90 | $sql = 'SELECT p.*, t.*, tp.topic_status FROM ' |
||
91 | . $this->db->prefix('newbb_posts') |
||
92 | . ' p LEFT JOIN ' |
||
93 | . $this->db->prefix('newbb_posts_text') |
||
94 | . ' t ON p.post_id=t.post_id LEFT JOIN ' |
||
95 | . $this->db->prefix('newbb_topics') |
||
96 | . ' tp ON tp.topic_id=p.topic_id WHERE p.topic_id=' |
||
97 | . $topic_id |
||
98 | . ' AND p.approved =' |
||
99 | . $approved |
||
100 | . ' ORDER BY p.post_time DESC'; |
||
101 | $result = $this->db->query($sql, $limit, 0); |
||
102 | $ret = []; |
||
103 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
104 | $post = $this->create(false); |
||
105 | $post->assignVars($myrow); |
||
106 | |||
107 | $ret[$myrow['post_id']] = $post; |
||
108 | unset($post); |
||
109 | } |
||
110 | |||
111 | return $ret; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param Post $post |
||
116 | * @return mixed |
||
117 | */ |
||
118 | public function getPostForPDF(&$post) |
||
119 | { |
||
120 | return $post->getPostBody(true); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param Post $post |
||
125 | * @return mixed |
||
126 | */ |
||
127 | public function getPostForPrint(&$post) |
||
128 | { |
||
129 | return $post->getPostBody(); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param Post $post |
||
134 | * @param bool $force |
||
135 | * @return bool |
||
136 | */ |
||
137 | public function approve(&$post, $force = false) |
||
138 | { |
||
139 | if (empty($post)) { |
||
140 | return false; |
||
141 | } |
||
142 | if (is_numeric($post)) { |
||
143 | $post = $this->get($post); |
||
144 | } |
||
145 | $post_id = $post->getVar('post_id'); |
||
146 | |||
147 | $wasApproved = $post->getVar('approved'); |
||
148 | // irmtfan approve post if the approved = 0 (pending) or -1 (deleted) |
||
149 | if (empty($force) && $wasApproved > 0) { |
||
150 | return true; |
||
151 | } |
||
152 | $post->setVar('approved', 1); |
||
153 | $this->insert($post, true); |
||
154 | |||
155 | /** @var Newbb\TopicHandler $topicHandler */ |
||
156 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
157 | $topicObject = $topicHandler->get($post->getVar('topic_id')); |
||
158 | if ($topicObject->getVar('topic_last_post_id') < $post->getVar('post_id')) { |
||
159 | $topicObject->setVar('topic_last_post_id', $post->getVar('post_id')); |
||
160 | } |
||
161 | if ($post->isTopic()) { |
||
162 | $topicObject->setVar('approved', 1); |
||
163 | } else { |
||
164 | $topicObject->setVar('topic_replies', $topicObject->getVar('topic_replies') + 1); |
||
165 | } |
||
166 | $topicHandler->insert($topicObject, true); |
||
167 | |||
168 | /** @var Newbb\ForumHandler $forumHandler */ |
||
169 | $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum'); |
||
170 | $forumObject = $forumHandler->get($post->getVar('forum_id')); |
||
171 | if ($forumObject->getVar('forum_last_post_id') < $post->getVar('post_id')) { |
||
172 | $forumObject->setVar('forum_last_post_id', $post->getVar('post_id')); |
||
173 | } |
||
174 | $forumObject->setVar('forum_posts', $forumObject->getVar('forum_posts') + 1); |
||
175 | if ($post->isTopic()) { |
||
176 | $forumObject->setVar('forum_topics', $forumObject->getVar('forum_topics') + 1); |
||
177 | } |
||
178 | $forumHandler->insert($forumObject, true); |
||
179 | |||
180 | // Update user stats |
||
181 | if ($post->getVar('uid') > 0) { |
||
182 | /** @var \XoopsMemberHandler $memberHandler */ |
||
183 | $memberHandler = xoops_getHandler('member'); |
||
184 | $poster = $memberHandler->getUser($post->getVar('uid')); |
||
185 | if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) { |
||
186 | $poster->setVar('posts', $poster->getVar('posts') + 1); |
||
187 | $res = $memberHandler->insertUser($poster, true); |
||
188 | unset($poster); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | // Update forum stats |
||
193 | /** @var StatsHandler $statsHandler */ |
||
194 | $statsHandler = Newbb\Helper::getInstance()->getHandler('Stats'); |
||
195 | $statsHandler->update($post->getVar('forum_id'), 'post'); |
||
196 | if ($post->isTopic()) { |
||
197 | $statsHandler->update($post->getVar('forum_id'), 'topic'); |
||
198 | } |
||
199 | |||
200 | return true; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param \XoopsObject $post |
||
205 | * @param bool $force |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function insert(\XoopsObject $post, $force = true) //insert(&$post, $force = true) |
||
209 | { |
||
210 | $topicObject = null; |
||
211 | // Set the post time |
||
212 | // The time should be "publish" time. To be adjusted later |
||
213 | if (!$post->getVar('post_time')) { |
||
214 | $post->setVar('post_time', time()); |
||
215 | } |
||
216 | |||
217 | /** @var Newbb\TopicHandler $topicHandler */ |
||
218 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
219 | // Verify the topic ID |
||
220 | if ($topic_id = $post->getVar('topic_id')) { |
||
221 | $topicObject = $topicHandler->get($topic_id); |
||
222 | // Invalid topic OR the topic is no approved and the post is not top post |
||
223 | if (!$topicObject// || (!$post->isTopic() && $topicObject->getVar("approved") < 1) |
||
224 | ) { |
||
225 | return false; |
||
226 | } |
||
227 | } |
||
228 | if (empty($topic_id)) { |
||
229 | $post->setVar('topic_id', 0); |
||
230 | $post->setVar('pid', 0); |
||
231 | $post->setNew(); |
||
232 | $topicObject = $topicHandler->create(); |
||
233 | } |
||
234 | $textHandler = Newbb\Helper::getInstance()->getHandler('Text'); |
||
235 | $post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr']; |
||
236 | if ($post->isNew()) { |
||
237 | if (!$topic_id = $post->getVar('topic_id')) { |
||
238 | $topicObject->setVar('topic_title', $post->getVar('subject', 'n')); |
||
239 | $topicObject->setVar('topic_poster', $post->getVar('uid')); |
||
240 | $topicObject->setVar('forum_id', $post->getVar('forum_id')); |
||
241 | $topicObject->setVar('topic_time', $post->getVar('post_time')); |
||
242 | $topicObject->setVar('poster_name', $post->getVar('poster_name')); |
||
243 | $topicObject->setVar('approved', $post->getVar('approved')); |
||
244 | |||
245 | if (!$topic_id = $topicHandler->insert($topicObject, $force)) { |
||
246 | $post->deleteAttachment(); |
||
247 | $post->setErrors('insert topic error'); |
||
248 | |||
249 | //xoops_error($topicObject->getErrors()); |
||
250 | return false; |
||
251 | } |
||
252 | $post->setVar('topic_id', $topic_id); |
||
253 | |||
254 | $pid = 0; |
||
255 | $post->setVar('pid', 0); |
||
256 | } elseif (!$post->getVar('pid')) { |
||
257 | $pid = $topicHandler->getTopPostId($topic_id); |
||
258 | $post->setVar('pid', $pid); |
||
259 | } |
||
260 | |||
261 | $textObject = $textHandler->create(); |
||
262 | foreach ($post_text_vars as $key) { |
||
263 | $textObject->vars[$key] = $post->vars[$key]; |
||
264 | } |
||
265 | $post->destroyVars($post_text_vars); |
||
266 | |||
267 | // if (!$post_id = parent::insert($post, $force)) { |
||
268 | // return false; |
||
269 | // } |
||
270 | |||
271 | if (!$post_id = parent::insert($post, $force)) { |
||
272 | return false; |
||
273 | } else { |
||
274 | $post->unsetNew(); |
||
275 | } |
||
276 | |||
277 | $textObject->setVar('post_id', $post_id); |
||
278 | if (!$textHandler->insert($textObject, $force)) { |
||
279 | $this->delete($post); |
||
280 | $post->setErrors('post text insert error'); |
||
281 | |||
282 | //xoops_error($textObject->getErrors()); |
||
283 | return false; |
||
284 | } |
||
285 | if ($post->getVar('approved') > 0) { |
||
286 | $this->approve($post, true); |
||
287 | } |
||
288 | $post->setVar('post_id', $post_id); |
||
289 | } else { |
||
290 | if ($post->isTopic()) { |
||
291 | if ($post->getVar('subject') !== $topicObject->getVar('topic_title')) { |
||
292 | $topicObject->setVar('topic_title', $post->getVar('subject', 'n')); |
||
293 | } |
||
294 | if ($post->getVar('approved') !== $topicObject->getVar('approved')) { |
||
295 | $topicObject->setVar('approved', $post->getVar('approved')); |
||
296 | } |
||
297 | $topicObject->setDirty(); |
||
298 | if (!$result = $topicHandler->insert($topicObject, $force)) { |
||
299 | $post->setErrors('update topic error'); |
||
300 | |||
301 | //xoops_error($topicObject->getErrors()); |
||
302 | return false; |
||
303 | } |
||
304 | } |
||
305 | $textObject = $textHandler->get($post->getVar('post_id')); |
||
306 | $textObject->setDirty(); |
||
307 | foreach ($post_text_vars as $key) { |
||
308 | $textObject->vars[$key] = $post->vars[$key]; |
||
309 | } |
||
310 | $post->destroyVars($post_text_vars); |
||
311 | if (!$post_id = parent::insert($post, $force)) { |
||
312 | //xoops_error($post->getErrors()); |
||
313 | return false; |
||
314 | } else { |
||
315 | $post->unsetNew(); |
||
316 | } |
||
317 | if (!$textHandler->insert($textObject, $force)) { |
||
318 | $post->setErrors('update post text error'); |
||
319 | |||
320 | //xoops_error($textObject->getErrors()); |
||
321 | return false; |
||
322 | } |
||
323 | } |
||
324 | |||
325 | return $post->getVar('post_id'); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @param \XoopsObject $post |
||
330 | * @param bool $isDeleteOne |
||
331 | * @param bool $force |
||
332 | * @return bool |
||
333 | */ |
||
334 | public function delete(\XoopsObject $post, $isDeleteOne = true, $force = false) |
||
335 | { |
||
336 | if (!is_object($post) || 0 == $post->getVar('post_id')) { |
||
337 | return false; |
||
338 | } |
||
339 | |||
340 | if ($isDeleteOne) { |
||
341 | if ($post->isTopic()) { |
||
342 | $criteria = new \CriteriaCompo(new \Criteria('topic_id', $post->getVar('topic_id'))); |
||
343 | $criteria->add(new \Criteria('approved', 1)); |
||
344 | $criteria->add(new \Criteria('pid', 0, '>')); |
||
345 | if ($this->getPostCount($criteria) > 0) { |
||
346 | return false; |
||
347 | } |
||
348 | } |
||
349 | |||
350 | return $this->myDelete($post, $force); |
||
351 | } else { |
||
352 | require_once $GLOBALS['xoops']->path('class/xoopstree.php'); |
||
353 | $mytree = new \XoopsTree($this->db->prefix('newbb_posts'), 'post_id', 'pid'); |
||
354 | $arr = $mytree->getAllChild($post->getVar('post_id')); |
||
355 | // irmtfan - delete childs in a reverse order |
||
356 | for ($i = count($arr) - 1; $i >= 0; $i--) { |
||
357 | $childpost = $this->create(false); |
||
358 | $childpost->assignVars($arr[$i]); |
||
359 | $this->myDelete($childpost, $force); |
||
360 | unset($childpost); |
||
361 | } |
||
362 | $this->myDelete($post, $force); |
||
363 | } |
||
364 | |||
365 | return true; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @param Post $post |
||
370 | * @param bool $force |
||
371 | * @return bool |
||
372 | */ |
||
373 | public function myDelete(Post $post, $force = false) |
||
374 | { |
||
375 | global $xoopsModule; |
||
376 | |||
377 | if (!is_object($post) || 0 == $post->getVar('post_id')) { |
||
378 | return false; |
||
379 | } |
||
380 | |||
381 | /* Set active post as deleted */ |
||
382 | if ($post->getVar('approved') > 0 && empty($force)) { |
||
383 | $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id'); |
||
384 | if (!$result = $this->db->queryF($sql)) { |
||
385 | } |
||
386 | /* delete pending post directly */ |
||
387 | } else { |
||
388 | $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts'), $post->getVar('post_id')); |
||
389 | if (!$result = $this->db->queryF($sql)) { |
||
390 | $post->setErrors('delete post error: ' . $sql); |
||
391 | |||
392 | return false; |
||
393 | } |
||
394 | $post->deleteAttachment(); |
||
395 | |||
396 | $sql = sprintf('DELETE FROM %s WHERE post_id = %u', $this->db->prefix('newbb_posts_text'), $post->getVar('post_id')); |
||
397 | if (!$result = $this->db->queryF($sql)) { |
||
398 | $post->setErrors('Could not remove post text: ' . $sql); |
||
399 | |||
400 | return false; |
||
401 | } |
||
402 | } |
||
403 | |||
404 | if ($post->isTopic()) { |
||
405 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
406 | /** @var Topic $topicObject */ |
||
407 | $topicObject = $topicHandler->get($post->getVar('topic_id')); |
||
408 | if (is_object($topicObject) && $topicObject->getVar('approved') > 0 && empty($force)) { |
||
409 | $topiccount_toupdate = 1; |
||
410 | $topicObject->setVar('approved', -1); |
||
411 | $topicHandler->insert($topicObject); |
||
412 | xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id')); |
||
413 | } else { |
||
414 | if (is_object($topicObject)) { |
||
415 | if ($topicObject->getVar('approved') > 0) { |
||
416 | xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id')); |
||
417 | } |
||
418 | |||
419 | $poll_id = $topicObject->getVar('poll_id'); |
||
420 | // START irmtfan poll_module |
||
421 | $topicObject->deletePoll($poll_id); |
||
422 | // END irmtfan poll_module |
||
423 | } |
||
424 | |||
425 | $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->db->prefix('newbb_topics'), $post->getVar('topic_id')); |
||
426 | if (!$result = $this->db->queryF($sql)) { |
||
427 | //xoops_error($this->db->error()); |
||
428 | } |
||
429 | $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->db->prefix('newbb_votedata'), $post->getVar('topic_id')); |
||
430 | if (!$result = $this->db->queryF($sql)) { |
||
431 | //xoops_error($this->db->error()); |
||
432 | } |
||
433 | } |
||
434 | } else { |
||
435 | $sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . ' t |
||
436 | LEFT JOIN ' . $this->db->prefix('newbb_posts') . ' p ON p.topic_id = t.topic_id |
||
437 | SET t.topic_last_post_id = p.post_id |
||
438 | WHERE t.topic_last_post_id = ' . $post->getVar('post_id') . ' |
||
439 | AND p.post_id = (SELECT MAX(post_id) FROM ' . $this->db->prefix('newbb_posts') . ' WHERE topic_id=t.topic_id)'; |
||
440 | if (!$result = $this->db->queryF($sql)) { |
||
441 | } |
||
442 | } |
||
443 | |||
444 | $postcount_toupdate = $post->getVar('approved'); |
||
445 | |||
446 | if ($postcount_toupdate > 0) { |
||
447 | |||
448 | // Update user stats |
||
449 | if ($post->getVar('uid') > 0) { |
||
450 | /** @var \XoopsMemberHandler $memberHandler */ |
||
451 | $memberHandler = xoops_getHandler('member'); |
||
452 | $poster = $memberHandler->getUser($post->getVar('uid')); |
||
453 | if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) { |
||
454 | $poster->setVar('posts', $poster->getVar('posts') - 1); |
||
455 | $res = $memberHandler->insertUser($poster, true); |
||
456 | unset($poster); |
||
457 | } |
||
458 | } |
||
459 | // irmtfan - just update the pid for approved posts when the post is not topic (pid=0) |
||
460 | if (!$post->isTopic()) { |
||
461 | $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE approved=1 AND pid=' . $post->getVar('post_id'); |
||
462 | if (!$result = $this->db->queryF($sql)) { |
||
463 | //xoops_error($this->db->error()); |
||
464 | } |
||
465 | } |
||
466 | } |
||
467 | |||
468 | return true; |
||
469 | } |
||
470 | |||
471 | // START irmtfan enhance getPostCount when there is join (read_mode = 2) |
||
472 | |||
473 | /** |
||
474 | * @param null $criteria |
||
475 | * @param null $join |
||
476 | * @return int|null |
||
477 | */ |
||
478 | public function getPostCount($criteria = null, $join = null) |
||
500 | } |
||
501 | // END irmtfan enhance getPostCount when there is join (read_mode = 2) |
||
502 | /* |
||
503 | * TODO: combining viewtopic.php |
||
504 | */ |
||
505 | /** |
||
506 | * @param null $criteria |
||
507 | * @param int $limit |
||
508 | * @param int $start |
||
509 | * @param null $join |
||
510 | * @return array |
||
511 | */ |
||
512 | public function getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null) |
||
513 | { |
||
514 | $ret = []; |
||
515 | $sql = 'SELECT p.*, t.* ' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id'; |
||
516 | if (!empty($join)) { |
||
517 | $sql .= $join; |
||
518 | } |
||
519 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
520 | $sql .= ' ' . $criteria->renderWhere(); |
||
521 | if ('' !== $criteria->getSort()) { |
||
522 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
523 | } |
||
524 | } |
||
525 | $result = $this->db->query($sql, (int)$limit, (int)$start); |
||
526 | if (!$result) { |
||
527 | //xoops_error($this->db->error()); |
||
528 | return $ret; |
||
529 | } |
||
530 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
531 | $post = $this->create(false); |
||
532 | $post->assignVars($myrow); |
||
533 | $ret[$myrow['post_id']] = $post; |
||
534 | unset($post); |
||
535 | } |
||
536 | |||
537 | return $ret; |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * @return bool |
||
542 | */ |
||
543 | public function synchronization() |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * clean orphan items from database |
||
551 | * |
||
552 | * @param string $table_link |
||
553 | * @param string $field_link |
||
554 | * @param string $field_object |
||
555 | * @return bool true on success |
||
556 | */ |
||
557 | public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan() |
||
558 | { |
||
559 | $this->deleteAll(new \Criteria('post_time', 0), true, true); |
||
560 | parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id'); |
||
561 | parent::cleanOrphan($this->db->prefix('newbb_posts_text'), 'post_id'); |
||
562 | |||
563 | $sql = 'DELETE FROM ' . $this->db->prefix('newbb_posts_text') . ' WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM ' . $this->table . ') )'; |
||
564 | if (!$result = $this->db->queryF($sql)) { |
||
565 | //xoops_error($this->db->error()); |
||
566 | return false; |
||
567 | } |
||
568 | |||
569 | return true; |
||
570 | } |
||
571 | |||
572 | /** |
||
573 | * clean expired objects from database |
||
574 | * |
||
575 | * @param int $expire time limit for expiration |
||
576 | * @return bool true on success |
||
577 | */ |
||
578 | public function cleanExpires($expire = 0) |
||
595 | } |
||
596 | } |
||
597 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.