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:
1 | <?php |
||
20 | View Code Duplication | class FunctionPatcher extends AbstractPatcher |
|
21 | { |
||
22 | private static $lock_function_list = false; |
||
23 | |||
24 | /** |
||
25 | * @var array list of function names (in lower case) which you patch |
||
26 | */ |
||
27 | private static $whitelist = [ |
||
28 | 'mt_rand', |
||
29 | 'rand', |
||
30 | 'uniqid', |
||
31 | 'hash_hmac', |
||
32 | 'md5', |
||
33 | 'sha1', |
||
34 | 'hash', |
||
35 | 'time', |
||
36 | 'microtime', |
||
37 | 'date', |
||
38 | 'function_exists', |
||
39 | 'header', |
||
40 | 'setcookie', |
||
41 | // Functions that have param called by reference |
||
42 | // Need to prepare method in FunctionPatcher\Proxy class |
||
43 | 'openssl_random_pseudo_bytes', |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * @var array list of function names (in lower case) which can't be patched |
||
48 | */ |
||
49 | private static $blacklist = [ |
||
50 | // Segmentation fault |
||
51 | 'call_user_func_array', |
||
52 | 'exit__', |
||
53 | // Error: Only variables should be assigned by reference |
||
54 | 'get_instance', |
||
55 | 'get_config', |
||
56 | 'load_class', |
||
57 | 'get_mimes', |
||
58 | '_get_validation_object', |
||
59 | // has reference param |
||
60 | 'preg_replace', |
||
61 | 'preg_match', |
||
62 | 'preg_match_all', |
||
63 | 'array_unshift', |
||
64 | 'array_shift', |
||
65 | 'sscanf', |
||
66 | 'ksort', |
||
67 | 'krsort', |
||
68 | 'str_ireplace', |
||
69 | 'str_replace', |
||
70 | 'is_callable', |
||
71 | 'flock', |
||
72 | 'end', |
||
73 | 'idn_to_ascii', |
||
74 | // Special functions for ci-phpunit-test |
||
75 | 'show_404', |
||
76 | 'show_error', |
||
77 | 'redirect', |
||
78 | ]; |
||
79 | |||
80 | public static $replacement; |
||
81 | |||
82 | public function __construct() |
||
86 | |||
87 | protected static function checkLock($error_msg) |
||
94 | |||
95 | public static function addWhitelists(array $function_list) |
||
104 | |||
105 | /** |
||
106 | * @return array |
||
107 | */ |
||
108 | public static function getFunctionWhitelist() |
||
112 | |||
113 | public static function addBlacklist($function_name) |
||
119 | |||
120 | public static function removeBlacklist($function_name) |
||
127 | |||
128 | public static function lockFunctionList() |
||
132 | |||
133 | /** |
||
134 | * @param string $name function name |
||
135 | * @return boolean |
||
136 | */ |
||
137 | public static function isWhitelisted($name) |
||
146 | |||
147 | /** |
||
148 | * @param string $name function name |
||
149 | * @return boolean |
||
150 | */ |
||
151 | public static function isBlacklisted($name) |
||
160 | |||
161 | protected static function generateNewSource($source) |
||
209 | } |
||
210 |
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.