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 |
||
10 | class KintBootup |
||
11 | { |
||
12 | /** |
||
13 | * init |
||
14 | */ |
||
15 | public static function init() |
||
|
|||
16 | { |
||
17 | if (defined('KINT_DIR')) { |
||
18 | return; |
||
19 | } |
||
20 | |||
21 | define('KINT_DIR', __DIR__ . '/'); |
||
22 | |||
23 | require KINT_DIR . 'config.default.php'; |
||
24 | |||
25 | # init settings |
||
26 | if (!empty($GLOBALS['_kint_settings'])) { |
||
27 | Kint::enabled($GLOBALS['_kint_settings']['enabled']); |
||
28 | |||
29 | foreach ($GLOBALS['_kint_settings'] as $key => $val) { |
||
30 | if (property_exists('Kint', $key)) { |
||
31 | Kint::$$key = $val; |
||
32 | } |
||
33 | } |
||
34 | |||
35 | unset($GLOBALS['_kint_settings'], $key, $val); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | public static function initFunctions() |
||
40 | { |
||
41 | /** |
||
42 | * Alias of Kint::dump() |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | function d() |
||
50 | |||
51 | |||
52 | /** |
||
53 | * Alias of Kint::dump() |
||
54 | * [!!!] IMPORTANT: execution will halt after call to this function |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | View Code Duplication | function dd() |
|
67 | |||
68 | /** |
||
69 | * Alias of Kint::dump() |
||
70 | * [!!!] IMPORTANT: execution will halt after call to this function |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | View Code Duplication | function ddd() |
|
83 | |||
84 | /** |
||
85 | * Alias of Kint::dump(), however the output is delayed until the end of the script |
||
86 | * |
||
87 | * @see d(); |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | function de() |
||
100 | |||
101 | /** |
||
102 | * Alias of Kint::dump(), however the output is in plain html-escaped text and some minor visibility enhancements |
||
103 | * added. If run in CLI mode, output is pure whitespace. |
||
104 | * |
||
105 | * To force rendering mode without auto-detecting anything: |
||
106 | * |
||
107 | * Kint::enabled( Kint::MODE_PLAIN ); |
||
108 | * Kint::dump( $variable ); |
||
109 | * |
||
110 | * [!!!] IMPORTANT: execution will halt after call to this function |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | function s() |
||
135 | |||
136 | /** |
||
137 | * @see s() |
||
138 | * |
||
139 | * [!!!] IMPORTANT: execution will halt after call to this function |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | function sd() |
||
159 | |||
160 | /** |
||
161 | * @see s() |
||
162 | * @see de() |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | function se() |
||
189 | |||
190 | /** |
||
191 | * Alias of Kint::dump(), however the output is dumped to the javascript console and |
||
192 | * added to the global array `kintDump`. If run in CLI mode, output is pure whitespace. |
||
193 | * |
||
194 | * To force rendering mode without autodetecting anything: |
||
195 | * |
||
196 | * Kint::enabled( Kint::MODE_JS ); |
||
197 | * Kint::dump( $variable ); |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | function j() |
||
219 | |||
220 | /** |
||
221 | * @see j() |
||
222 | * |
||
223 | * [!!!] IMPORTANT: execution will halt after call to this function |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | function jd() |
||
241 | |||
242 | /** |
||
243 | * @see j() |
||
244 | * @see de() |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | function je() |
||
268 | } |
||
269 | |||
270 | } |
||
271 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: