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 |
||
8 | class Flash |
||
9 | { |
||
10 | /** |
||
11 | * @var object |
||
12 | */ |
||
13 | protected static $data = null; |
||
14 | |||
15 | /** |
||
16 | * Check if the flash is set. |
||
17 | * |
||
18 | * @return boolean |
||
19 | */ |
||
20 | 2 | public static function isIssued() |
|
24 | |||
25 | /** |
||
26 | * Set the flash. |
||
27 | * |
||
28 | * @param mixed $type flash type, eg. 'error', 'notice' or 'success' |
||
29 | * @param mixed $message flash message |
||
30 | */ |
||
31 | 4 | public static function set($type, $message) |
|
41 | |||
42 | /** |
||
43 | * Get the flash. |
||
44 | * |
||
45 | * @return object |
||
46 | */ |
||
47 | 3 | View Code Duplication | public static function get() |
56 | |||
57 | /** |
||
58 | * Reissue the flash. |
||
59 | */ |
||
60 | 2 | View Code Duplication | public static function reissue() |
68 | |||
69 | /** |
||
70 | * Clear the flash. |
||
71 | */ |
||
72 | 3 | public static function clear() |
|
77 | |||
78 | /** |
||
79 | * Get the flash type |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | 2 | public static function getType() |
|
88 | |||
89 | /** |
||
90 | * Get the flash message |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | 2 | public static function getMessage() |
|
99 | |||
100 | /** |
||
101 | * Cast object to string |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 2 | public function __toString() |
|
109 | } |
||
110 |
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: