Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like System 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 System, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
58 | class System |
||
59 | { |
||
60 | /** |
||
61 | * returns the commandline arguments of a function |
||
62 | * |
||
63 | * @param string $argv the commandline |
||
64 | * @param string $short_options the allowed option short-tags |
||
65 | * @param string $long_options the allowed option long-tags |
||
66 | * @return array the given options and there values |
||
67 | */ |
||
68 | public static function _parseArgs($argv, $short_options, $long_options = null) |
||
101 | |||
102 | /** |
||
103 | * Output errors with PHP trigger_error(). You can silence the errors |
||
104 | * with prefixing a "@" sign to the function call: @System::mkdir(..); |
||
105 | * |
||
106 | * @param mixed $error a PEAR error or a string with the error message |
||
107 | * @return bool false |
||
108 | */ |
||
109 | protected static function raiseError($error) |
||
117 | |||
118 | /** |
||
119 | * Creates a nested array representing the structure of a directory |
||
120 | * |
||
121 | * System::_dirToStruct('dir1', 0) => |
||
122 | * Array |
||
123 | * ( |
||
124 | * [dirs] => Array |
||
125 | * ( |
||
126 | * [0] => dir1 |
||
127 | * ) |
||
128 | * |
||
129 | * [files] => Array |
||
130 | * ( |
||
131 | * [0] => dir1/file2 |
||
132 | * [1] => dir1/file3 |
||
133 | * ) |
||
134 | * ) |
||
135 | * @param string $sPath Name of the directory |
||
136 | * @param integer $maxinst max. deep of the lookup |
||
137 | * @param integer $aktinst starting deep of the lookup |
||
138 | * @param bool $silent if true, do not emit errors. |
||
139 | * @return array the structure of the dir |
||
140 | */ |
||
141 | protected static function _dirToStruct($sPath, $maxinst, $aktinst = 0, $silent = false) |
||
175 | |||
176 | /** |
||
177 | * Creates a nested array representing the structure of a directory and files |
||
178 | * |
||
179 | * @param array $files Array listing files and dirs |
||
180 | * @return array |
||
181 | * @static |
||
182 | * @see System::_dirToStruct() |
||
183 | */ |
||
184 | protected static function _multipleToStruct($files) |
||
200 | |||
201 | /** |
||
202 | * The rm command for removing files. |
||
203 | * Supports multiple files and dirs and also recursive deletes |
||
204 | * |
||
205 | * @param string $args the arguments for rm |
||
206 | * @return mixed PEAR_Error or true for success |
||
207 | * @static |
||
208 | * @access public |
||
209 | */ |
||
210 | public static function rm($args) |
||
246 | |||
247 | /** |
||
248 | * Make directories. |
||
249 | * |
||
250 | * The -p option will create parent directories |
||
251 | * @param string $args the name of the director(y|ies) to create |
||
252 | * @return bool True for success |
||
253 | */ |
||
254 | public static function mkDir($args) |
||
309 | |||
310 | /** |
||
311 | * Concatenate files |
||
312 | * |
||
313 | * Usage: |
||
314 | * 1) $var = System::cat('sample.txt test.txt'); |
||
315 | * 2) System::cat('sample.txt test.txt > final.txt'); |
||
316 | * 3) System::cat('sample.txt test.txt >> final.txt'); |
||
317 | * |
||
318 | * Note: as the class use fopen, urls should work also (test that) |
||
319 | * |
||
320 | * @param string $args the arguments |
||
321 | * @return boolean true on success |
||
322 | */ |
||
323 | public static function &cat($args) |
||
372 | |||
373 | /** |
||
374 | * Creates temporary files or directories. This function will remove |
||
375 | * the created files when the scripts finish its execution. |
||
376 | * |
||
377 | * Usage: |
||
378 | * 1) $tempfile = System::mktemp("prefix"); |
||
379 | * 2) $tempdir = System::mktemp("-d prefix"); |
||
380 | * 3) $tempfile = System::mktemp(); |
||
381 | * 4) $tempfile = System::mktemp("-t /var/tmp prefix"); |
||
382 | * |
||
383 | * prefix -> The string that will be prepended to the temp name |
||
384 | * (defaults to "tmp"). |
||
385 | * -d -> A temporary dir will be created instead of a file. |
||
386 | * -t -> The target dir where the temporary (file|dir) will be created. If |
||
387 | * this param is missing by default the env vars TMP on Windows or |
||
388 | * TMPDIR in Unix will be used. If these vars are also missing |
||
389 | * c:\windows\temp or /tmp will be used. |
||
390 | * |
||
391 | * @param string $args The arguments |
||
392 | * @return mixed the full path of the created (file|dir) or false |
||
393 | * @see System::tmpdir() |
||
394 | */ |
||
395 | public static function mktemp($args = null) |
||
440 | |||
441 | /** |
||
442 | * Remove temporary files created my mkTemp. This function is executed |
||
443 | * at script shutdown time |
||
444 | */ |
||
445 | public static function _removeTmpFiles() |
||
454 | |||
455 | /** |
||
456 | * Get the path of the temporal directory set in the system |
||
457 | * by looking in its environments variables. |
||
458 | * Note: php.ini-recommended removes the "E" from the variables_order setting, |
||
459 | * making unavaible the $_ENV array, that s why we do tests with _ENV |
||
460 | * |
||
461 | * @return string The temporary directory on the system |
||
462 | */ |
||
463 | public static function tmpdir() |
||
485 | |||
486 | /** |
||
487 | * The "which" command (show the full path of a command) |
||
488 | * |
||
489 | * @param string $program The command to search for |
||
490 | * @param mixed $fallback Value to return if $program is not found |
||
491 | * |
||
492 | * @return mixed A string with the full path or false if not found |
||
493 | * @author Stig Bakken <[email protected]> |
||
494 | */ |
||
495 | public static function which($program, $fallback = false) |
||
537 | |||
538 | /** |
||
539 | * The "find" command |
||
540 | * |
||
541 | * Usage: |
||
542 | * |
||
543 | * System::find($dir); |
||
544 | * System::find("$dir -type d"); |
||
545 | * System::find("$dir -type f"); |
||
546 | * System::find("$dir -name *.php"); |
||
547 | * System::find("$dir -name *.php -name *.htm*"); |
||
548 | * System::find("$dir -maxdepth 1"); |
||
549 | * |
||
550 | * Params implemented: |
||
551 | * $dir -> Start the search at this directory |
||
552 | * -type d -> return only directories |
||
553 | * -type f -> return only files |
||
554 | * -maxdepth <n> -> max depth of recursion |
||
555 | * -name <pattern> -> search pattern (bash style). Multiple -name param allowed |
||
556 | * |
||
557 | * @param mixed Either array or string with the command line |
||
558 | * @return array Array of found files |
||
559 | */ |
||
560 | public static function find($args) |
||
622 | } |
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.