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 array |
||
12 | */ |
||
13 | protected $data; |
||
14 | |||
15 | /** |
||
16 | * @var array|\ArrayObject |
||
17 | */ |
||
18 | protected $session; |
||
19 | |||
20 | /** |
||
21 | * Session key for flash |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $key = 'flash'; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Class constructor |
||
29 | * |
||
30 | * @param array|\ArrayObject $session |
||
31 | */ |
||
32 | 9 | public function __construct(&$session) |
|
36 | |||
37 | /** |
||
38 | * Check if the flash is set. |
||
39 | * |
||
40 | * @return boolean |
||
41 | */ |
||
42 | 4 | public function isIssued() |
|
46 | |||
47 | /** |
||
48 | * Set the flash. |
||
49 | * |
||
50 | * @param string $type flash type, eg. 'error', 'notice' or 'success' |
||
51 | * @param mixed $message flash message |
||
52 | */ |
||
53 | 2 | public function set($type, $message) |
|
57 | |||
58 | /** |
||
59 | * Get the flash. |
||
60 | * |
||
61 | * @return object |
||
62 | */ |
||
63 | 7 | public function get() |
|
72 | |||
73 | /** |
||
74 | * Reissue the flash. |
||
75 | */ |
||
76 | 2 | public function reissue() |
|
84 | |||
85 | /** |
||
86 | * Clear the flash. |
||
87 | */ |
||
88 | 3 | public function clear() |
|
93 | |||
94 | /** |
||
95 | * Get the flash type |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | 2 | public function getType() |
|
104 | |||
105 | /** |
||
106 | * Get the flash message |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | 2 | public function getMessage() |
|
115 | |||
116 | /** |
||
117 | * Cast object to string |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 2 | public function __toString() |
|
125 | } |
||
126 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.