| Conditions | 49 |
| Paths | > 20000 |
| Total Lines | 160 |
| Code Lines | 109 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 168 | function parse_amendments() { |
||
| 169 | global $amendments, $bill, $clauses, $title; |
||
| 170 | foreach ($amendments as $num => $amendment) { |
||
| 171 | # Page 8, line 4 [Clause 13], leave out `21-day' and insert `30-day' |
||
| 172 | if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) { |
||
| 173 | $page = $m[1]; $line = $m[2]; $clause = $m[3]; |
||
| 174 | $delete = $m[4]; $insert = $m[5]; |
||
| 175 | unset($amendments[$num]); |
||
| 176 | $bill[$page][$line] = preg_replace("#(.*)$delete#", "$1<del title='$num'>$delete</del><ins title='$num'>$insert</ins>", $bill[$page][$line]); |
||
| 177 | } |
||
| 178 | # Page 2, line 32 [Clause 3], leave out from `make' to end of line 35 and insert `...' |
||
| 179 | if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to end of line (\d+) and insert(?:--)?\s+`(.*?)\'#s', $amendment, $m)) { |
||
| 180 | $page = $m[1]; $line = $m[2]; $clause = $m[3]; |
||
| 181 | $from_text = $m[4]; $end_line = $m[5]; $insert = $m[6]; |
||
| 182 | unset($amendments[$num]); |
||
| 183 | $bill[$page][$line] = str_replace($from_text, "$from_text <del title='$num'>", $bill[$page][$line]) . '</del><ins title="'.$num.'">' . $insert . '</ins>'; |
||
| 184 | for ($i=$line+1; $i<=$end_line; $i++) { |
||
| 185 | $bill[$page][$i] = '<del title="'.$num.'">' . $bill[$page][$i] . '</del>'; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | # Page 4, line 9 [Clause 6], leave out from `under' to `creating' and insert `this Part making provision' |
||
| 189 | if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) { |
||
| 190 | $page = $m[1]; $line = $m[2]; $clause = $m[3]; |
||
| 191 | $from_text = $m[4]; $to_text = $m[5]; $insert = $m[6]; |
||
| 192 | unset($amendments[$num]); |
||
| 193 | $bill[$page][$line] = preg_replace("#$from_text(.*?)$to_text#", "$from_text <del title='$num'>$1</del><ins title='$num'>$insert</ins> $to_text", $bill[$page][$line]); |
||
| 194 | } |
||
| 195 | # Page 7, line 1 [Clause 12], leave out from `of' to `the' in line 2 and insert `...' |
||
| 196 | if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to `(.*?)\' in line (\d+) and insert `(.*?)\'#s', $amendment, $m)) { |
||
| 197 | $page = $m[1]; $from_line = $m[2]; $clause = $m[3]; |
||
| 198 | $from_text = $m[4]; $to_text = $m[5]; $to_line = $m[6]; |
||
| 199 | $insert = $m[7]; |
||
| 200 | unset($amendments[$num]); |
||
| 201 | $bill[$page][$from_line] = str_replace($from_text, "$from_text <del title='$num'>", $bill[$page][$from_line]) . '</del>'; |
||
| 202 | for ($i=$from_line+1; $i<$to_line; $i++) { |
||
| 203 | $bill[$page][$i] = '<del title="'.$num.'">' . $bill[$page][$i] . '</del>'; |
||
| 204 | } |
||
| 205 | $bill[$page][$to_line] = '<del title="'.$num.'">' . str_replace($to_text, "</del><ins title='$num'>$insert</ins> $to_text", $bill[$page][$to_line]); |
||
| 206 | } |
||
| 207 | # Page 3, line 13 [Clause 4], leave out from beginning to `confer' and insert `An order under this Part may not make provision to' |
||
| 208 | if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from beginning to `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) { |
||
| 209 | $page = $m[1]; $line = $m[2]; $clause = $m[3]; |
||
| 210 | $to_text = $m[4]; $insert = $m[5]; |
||
| 211 | unset($amendments[$num]); |
||
| 212 | $bill[$page][$line] = "<ins title='$num'>$insert</ins><del title='$num'>" . str_replace($to_text, "</del> $to_text", $bill[$page][$line]); |
||
| 213 | } |
||
| 214 | # Page 19, line 2 [Clause 34], leave out from `under' to the end of the line and insert `...' |
||
| 215 | if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to the end of the line and insert\s+`(.*?)\'#s', $amendment, $m)) { |
||
| 216 | $page = $m[1]; $line = $m[2]; $clause = $m[3]; |
||
| 217 | $from_text = $m[4]; $insert = $m[5]; |
||
| 218 | unset($amendments[$num]); |
||
| 219 | $bill[$page][$line] = str_replace($from_text, "$from_text <del title='$num'>", $bill[$page][$line]) . "</del><ins title='$num'>$insert</ins>"; |
||
| 220 | } |
||
| 221 | # Page 4 [Clause 7], leave out line 26 and insert `An order under this Part may not make provision to--' |
||
| 222 | if (preg_match('#Page\s+(\d+) \[Clause (\d+)\], leave out line (\d+)(?: and insert `(.*?)\')?#', $amendment, $m)) { |
||
| 223 | $page = $m[1]; $line = $m[3]; $clause = $m[2]; |
||
| 224 | $insert = isset($m[4]) ? $m[4] : null; |
||
| 225 | unset($amendments[$num]); |
||
| 226 | $bill[$page][$line] = '<del title="'.$num.'">'.$bill[$page][$line].'</del>'; |
||
| 227 | if ($insert) $bill[$page][$line] .= '<ins title="'.$num.'">'.$insert.'</ins>'; |
||
| 228 | } |
||
| 229 | # Page 8, line 24 [Clause 14], at end insert-- `...' |
||
| 230 | if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], (?:at end|after subsection \(\d+\)) insert--\s+`(.*?)\'#s', $amendment, $m)) { |
||
| 231 | $page = $m[1]; $line = $m[2]; $clause = $m[3]; |
||
| 232 | $insert = $m[4]; |
||
| 233 | unset($amendments[$num]); |
||
| 234 | $bill[$page][$line] .= '<ins title="'.$num.'">'.$insert.'</ins>'; |
||
| 235 | } |
||
| 236 | # Title, line 1, leave out `reforming legislation' and insert `...' |
||
| 237 | if (preg_match('#Title, line.*?, leave out `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) { |
||
| 238 | $delete = $m[1]; $insert = $m[2]; |
||
| 239 | unset($amendments[$num]); |
||
| 240 | $title = str_replace($delete, "<del title='$num'>$delete</del><ins title='$num'>$insert</ins>", $title); |
||
| 241 | } |
||
| 242 | # Page 4, line 23 [Clause 6], leave out paragraph (b) |
||
| 243 | if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out paragraph \((.*?)\)(?: and insert-- `(.*?)\')?#', $amendment, $m)) { |
||
| 244 | $page = $m[1]; $line = $m[2]; $clause = $m[3]; |
||
| 245 | $paragraph = $m[4]; |
||
| 246 | $insert = isset($m[5]) ? $m[5] : null; |
||
| 247 | foreach ($clauses[$clause] as $subclause_num => $subclause) { |
||
| 248 | foreach ($subclause as $subsubclause_num => $subsubclause) { |
||
| 249 | $startP = $subsubclause['startP']; |
||
| 250 | if ($startP==$page && $subsubclause['startL']==$line) { |
||
| 251 | if ($startP == $subsubclause['endP']) { |
||
| 252 | unset($amendments[$num]); |
||
| 253 | for ($i = $subsubclause['startL']; $i<=$subsubclause['endL']; $i++) { |
||
| 254 | $bill[$page][$i] = '<del title="'.$num.'">' . $bill[$page][$i] . '</del>'; |
||
| 255 | } |
||
| 256 | if ($insert) { |
||
| 257 | $bill[$page][$i-1] .= "<ins title='$num'>$insert</ins>"; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | # Page 6, line 40 [Clause 12], leave out subsection (3) |
||
| 265 | if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out subsection \((.*?)\)(?: and insert-- `(.*?)\')?#', $amendment, $m)) { |
||
| 266 | $page = $m[1]; $line = $m[2]; $clause = $m[3]; |
||
| 267 | $subsection = $m[4]; |
||
| 268 | $insert = isset($m[5]) ? $m[5] : null; |
||
| 269 | $finished = false; |
||
| 270 | foreach ($clauses[$clause] as $subclause_num => $subclause) { |
||
| 271 | foreach ($subclause as $subsubclause_num => $subsubclause) { |
||
| 272 | $startP = $subsubclause['startP']; |
||
| 273 | if ($startP==$page && $subsubclause['startL']==$line) { |
||
| 274 | if ($startP == $subsubclause['endP']) { |
||
| 275 | unset($amendments[$num]); |
||
| 276 | $finished = true; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | if ($finished) { |
||
| 280 | for ($i = $subsubclause['startL']; $i<=$subsubclause['endL']; $i++) { |
||
| 281 | $bill[$page][$i] = '<del title="'.$num.'">' . $bill[$page][$i] . '</del>'; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | } |
||
| 285 | if ($finished) { |
||
| 286 | if ($insert) { |
||
| 287 | $bill[$page][$i-1] .= "<ins title='$num'>$insert</ins>"; |
||
| 288 | } |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | # Page 12, line 17, leave out clause 24 |
||
| 294 | if (preg_match('#Page\s+(\d+), line (\d+), leave out clause (\d+)#', $amendment, $m)) { |
||
| 295 | $page = $m[1]; $line = $m[2]; $clause = $m[3]; |
||
| 296 | $finished = false; |
||
| 297 | foreach ($clauses[$clause] as $subclause_num => $subclause) { |
||
| 298 | foreach ($subclause as $subsubclause_num => $subsubclause) { |
||
| 299 | if ($subsubclause['startP']==$page && $subsubclause['startL']==$line) { |
||
| 300 | unset($amendments[$num]); |
||
| 301 | $finished = true; |
||
| 302 | } |
||
| 303 | if ($finished) { |
||
| 304 | for ($p = $subsubclause['startP']; $p<=$subsubclause['endP']; $p++) { |
||
| 305 | if ($p>$subsubclause['startP']) $starti = 1; |
||
| 306 | else $starti = $subsubclause['startL']; |
||
| 307 | for ($i = $starti; $i<=$subsubclause['endL']; $i++) { # XXX Doesn't really work spanning pages |
||
| 308 | $bill[$p][$i] = '<del title="'.$num.'">' . $bill[$p][$i] . '</del>'; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | } |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | # New clause |
||
| 317 | if (preg_match('#^\*(.*?)\*\s+(.*?)$#s', $amendment, $m)) { |
||
| 318 | unset($amendments[$num]); |
||
| 319 | $page = 0; |
||
| 320 | $line = 0; |
||
| 321 | foreach ($clauses[$clause] as $subclause_num => $subclause) { |
||
| 322 | foreach ($subclause as $subsubclause_num => $subsubclause) { |
||
| 323 | if ($subsubclause['endP'] > $page) { $page = $subsubclause['endP']; $line = 0; } |
||
| 324 | if ($subsubclause['endL'] > $line) $line = $subsubclause['endL']; |
||
| 325 | } |
||
| 326 | } |
||
| 327 | $bill[$page][$line] .= "<ins title='$num'>$amendment</ins>\n\n"; |
||
| 328 | } |
||
| 331 |
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.