Conditions | 10 |
Paths | 18 |
Total Lines | 56 |
Lines | 14 |
Ratio | 25 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
39 | function b_xnewsletter_catsubscr($options) |
||
40 | { |
||
41 | global $xoopsUser; |
||
42 | $helper = Xnewsletter\Helper::getInstance(); |
||
43 | $myts = \MyTextSanitizer::getInstance(); |
||
|
|||
44 | |||
45 | $catsubscrArray = []; |
||
46 | $type_block = $options[0]; |
||
47 | $nb_catsubscr = $options[1]; |
||
48 | $length_title = $options[2]; |
||
49 | |||
50 | array_shift($options); |
||
51 | array_shift($options); |
||
52 | array_shift($options); |
||
53 | |||
54 | $catsubscrCriteria = new \CriteriaCompo(); |
||
55 | View Code Duplication | switch ($type_block) { |
|
56 | // For the block: catsubscr recents |
||
57 | case 'recent': |
||
58 | $catsubscrCriteria->setSort('catsubscr_created'); |
||
59 | $catsubscrCriteria->setOrder('DESC'); |
||
60 | break; |
||
61 | // For the block: catsubscr of today |
||
62 | case 'day': |
||
63 | $catsubscrCriteria->add(new \Criteria('catsubscr_created', strtotime(date('Y/m/d')), '>=')); |
||
64 | $catsubscrCriteria->add(new \Criteria('catsubscr_created', strtotime(date('Y/m/d')) + 86400, '<=')); |
||
65 | $catsubscrCriteria->setSort('catsubscr_created'); |
||
66 | $catsubscrCriteria->setOrder('ASC'); |
||
67 | break; |
||
68 | } |
||
69 | |||
70 | $catsubscrCriteria->setLimit($nb_catsubscr); |
||
71 | $catsubscrObjs = $helper->getHandler('Catsubscr')->getAll($catsubscrCriteria); |
||
72 | foreach ($catsubscrObjs as $catsubscr_id => $catsubscrObj) { |
||
73 | $cat_id = $catsubscrObj->getVar('catsubscr_catid'); |
||
74 | if (in_array($cat_id, $options) || '0' == $options[0]) { |
||
75 | $subscr_id = $catsubscrObj->getVar('catsubscr_subscrid'); |
||
76 | $subscrObj = $helper->getHandler('Subscr')->get($subscr_id); |
||
77 | $email = $subscrObj->getVar('subscr_email'); |
||
78 | if ($length_title > 0 && mb_strlen($email) > $length_title) { |
||
79 | $email = mb_substr($email, 0, $length_title) . '...'; |
||
80 | } |
||
81 | $catsubscrArray[$catsubscr_id]['catsubscr_email'] = $email; |
||
82 | |||
83 | $catObj = $helper->getHandler('Cat')->get($cat_id); |
||
84 | $cat_name = $catObj->getVar('cat_name'); |
||
85 | if ($length_title > 0 && mb_strlen($cat_name) > $length_title) { |
||
86 | $cat_name = mb_substr($cat_name, 0, $length_title) . '...'; |
||
87 | } |
||
88 | $catsubscrArray[$catsubscr_id]['catsubscr_newsletter'] = $cat_name; |
||
89 | $catsubscrArray[$catsubscr_id]['catsubscr_created'] = formatTimestamp($catsubscrObj->getVar('catsubscr_created'), 'S'); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | return $catsubscrArray; |
||
94 | } |
||
95 | |||
127 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.