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 |
||
13 | class Server implements ServerInterface |
||
14 | { |
||
15 | /** |
||
16 | * Retrieves a value defined in the superglobal $_SERVER. |
||
17 | * |
||
18 | * @param string $value The key's name. |
||
19 | * @return string|mixed |
||
20 | */ |
||
21 | public static function getValue($value, $default = '') |
||
29 | |||
30 | /** |
||
31 | * Retrieves all values defined in the superglobal $_SERVER. |
||
32 | * |
||
33 | * @return string|mixed |
||
34 | */ |
||
35 | public static function getValues() |
||
39 | |||
40 | |||
41 | /** |
||
42 | * Returns a value from the $_GET superglobal. |
||
43 | * Null if none exists. |
||
44 | * |
||
45 | * @param string $name The value's name |
||
46 | * @return mixed |
||
47 | */ |
||
48 | public static function get($name) |
||
56 | |||
57 | /** |
||
58 | * Returns all values from the $_GET superglobal. |
||
59 | * Null if none exists. |
||
60 | * |
||
61 | * @param string $name |
||
62 | * @return mixed |
||
63 | */ |
||
64 | View Code Duplication | public static function gets(array $values = null) |
|
77 | |||
78 | /** |
||
79 | * Returns a value from the $_POST superglobal. |
||
80 | * Null if none exists. |
||
81 | * |
||
82 | * @param string $name The value's name |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public static function post($name) |
||
93 | |||
94 | /** |
||
95 | * Returns all values from the $_POST superglobal. |
||
96 | * Null if none exists. |
||
97 | * |
||
98 | * @param string $name |
||
99 | * @return mixed |
||
100 | */ |
||
101 | View Code Duplication | public static function posts(array $values = null) |
|
114 | |||
115 | /** |
||
116 | * Returns a value from the $_FILES superglobal. |
||
117 | * Null if none exists. |
||
118 | * |
||
119 | * @param string $name The value's name |
||
120 | * @return mixed |
||
121 | */ |
||
122 | public static function file($name) |
||
130 | |||
131 | /** |
||
132 | * Returns all values from the $_FILES superglobal. |
||
133 | * Null if none exists. |
||
134 | * |
||
135 | * @param string $name |
||
136 | * @return mixed |
||
137 | */ |
||
138 | View Code Duplication | public static function files(array $values = null) |
|
151 | |||
152 | } |
||
153 |
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: