Conditions | 16 |
Paths | 2592 |
Total Lines | 81 |
Code Lines | 56 |
Lines | 16 |
Ratio | 19.75 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
104 | function b_news_archives_edit($options) |
||
105 | { |
||
106 | global $xoopsDB; |
||
107 | $syear = $smonth = $eyear = $emonth = $older = $recent = 0; |
||
108 | $selsyear = $selsmonth = $seleyear = $selemonth = 0; |
||
109 | $form = ''; |
||
110 | |||
111 | $selsyear = $options[1]; |
||
112 | $selsmonth = $options[2]; |
||
113 | $seleyear = $options[3]; |
||
114 | $selemonth = $options[4]; |
||
115 | |||
116 | $tmpstory = new NewsStory; |
||
117 | $tmpstory->GetOlderRecentNews($older, $recent); // We are searching for the module's older and more recent article's date |
||
118 | |||
119 | // Min and max value for the two dates selectors |
||
120 | // We are going to use the older news for the starting date |
||
121 | $syear = date('Y', $older); |
||
122 | $smonth = date('n', $older); |
||
123 | $eyear = date('Y', $recent); |
||
124 | $emonth = date('n', $recent); |
||
125 | // Verify parameters |
||
126 | if ($selsyear == 0 && $selsmonth == 0) { |
||
127 | $selsyear = $syear; |
||
128 | $selsmonth = $smonth; |
||
129 | } |
||
130 | if ($seleyear == 0 && $selemonth == 0) { |
||
131 | $seleyear = $eyear; |
||
132 | $selemonth = $emonth; |
||
133 | } |
||
134 | |||
135 | // Sort order ************************************************************* |
||
136 | // (0=older first, 1=newer first) |
||
137 | $form .= '<b>' . _MB_NEWS_ORDER . "</b> <select name='options[]'>"; |
||
138 | $form .= "<option value='0'"; |
||
139 | if ($options[0] == 0) { |
||
140 | $form .= " selected='selected'"; |
||
141 | } |
||
142 | $form .= '>' . _MB_NEWS_OLDER_FIRST . "</option>\n"; |
||
143 | $form .= "<option value='1'"; |
||
144 | if ($options[0] == 1) { |
||
145 | $form .= " selected='selected'"; |
||
146 | } |
||
147 | $form .= '>' . _MB_NEWS_RECENT_FIRST . '</option>'; |
||
148 | $form .= "</select>\n"; |
||
149 | |||
150 | // Starting and ending dates ********************************************** |
||
151 | $form .= '<br><br><b>' . _MB_NEWS_STARTING_DATE . '</b><br>'; |
||
152 | $form .= _MB_NEWS_CAL_YEAR . " <select name='options[]'>"; |
||
153 | View Code Duplication | for ($i = $syear; $i <= $eyear; ++$i) { |
|
154 | $selected = ($i == $selsyear) ? "selected='selected'" : ''; |
||
155 | $form .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>'; |
||
156 | } |
||
157 | $form .= '</select> ' . _MB_NEWS_CAL_MONTH . " <select name='options[]'>"; |
||
158 | View Code Duplication | for ($i = 1; $i <= 12; ++$i) { |
|
159 | $selected = ($i == $selsmonth) ? "selected='selected'" : ''; |
||
160 | $form .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>'; |
||
161 | } |
||
162 | $form .= '</select>'; |
||
163 | |||
164 | $form .= '<br><br><b>' . _MB_NEWS_ENDING_DATE . '</b><br>'; |
||
165 | $form .= _MB_NEWS_CAL_YEAR . " <select name='options[]'>"; |
||
166 | View Code Duplication | for ($i = $syear; $i <= $eyear; ++$i) { |
|
167 | $selected = ($i == $seleyear) ? "selected='selected'" : ''; |
||
168 | $form .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>'; |
||
169 | } |
||
170 | $form .= '</select> ' . _MB_NEWS_CAL_MONTH . " <select name='options[]'>"; |
||
171 | View Code Duplication | for ($i = 1; $i <= 12; ++$i) { |
|
172 | $selected = ($i == $selemonth) ? "selected='selected'" : ''; |
||
173 | $form .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>'; |
||
174 | } |
||
175 | $form .= '</select>'; |
||
176 | |||
177 | // Or until today ********************************************************* |
||
178 | $form .= '<br>'; |
||
179 | $checked = $options[5] == 1 ? " checked='checked'" : ''; |
||
180 | $form .= "<input type='checkbox' value='1' name='options[]'" . $checked . '>'; |
||
181 | $form .= ' <b>' . _MB_NEWS_UNTIL_TODAY . '</b>'; |
||
182 | |||
183 | return $form; |
||
184 | } |
||
185 | |||
198 |
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.