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 |
||
9 | class Notice |
||
10 | { |
||
11 | /** |
||
12 | * @var Application |
||
13 | */ |
||
14 | protected $app; |
||
15 | |||
16 | public function __construct( Application $app ) |
||
20 | |||
21 | /** |
||
22 | * @method void addError( mixed $messages, array $args ) |
||
23 | * @method void addInfo( mixed $messages, array $args ) |
||
24 | * @method void addSuccess( mixed $messages, array $args ) |
||
25 | * @method void addWarning( mixed $messages, array $args ) |
||
26 | */ |
||
27 | public function __call( $name, $args ) |
||
36 | |||
37 | /** |
||
38 | * @property array $all |
||
39 | */ |
||
40 | public function __get( $property ) |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | View Code Duplication | public function activateButton( array $plugin ) |
|
61 | |||
62 | /** |
||
63 | * @param string $title |
||
64 | * @param string $url |
||
65 | * @param string $attributes |
||
66 | * @return string |
||
67 | */ |
||
68 | public function button( $title, array $atts = [] ) |
||
80 | |||
81 | /** |
||
82 | * @return void |
||
83 | */ |
||
84 | public function generate( array $notice, $unset = true ) |
||
94 | |||
95 | /** |
||
96 | * @return string |
||
97 | */ |
||
98 | View Code Duplication | public function installButton( array $plugin ) |
|
108 | |||
109 | /** |
||
110 | * @return string |
||
111 | */ |
||
112 | View Code Duplication | public function updateButton( array $plugin ) |
|
122 | |||
123 | /** |
||
124 | * @param string $string |
||
125 | * @return string |
||
126 | */ |
||
127 | public function title( $string ) |
||
131 | |||
132 | /** |
||
133 | * @param string $type |
||
134 | * @param string|array $messages |
||
135 | * @param bool $dismissible |
||
136 | * @return void |
||
137 | */ |
||
138 | protected function addNotice( $type, $messages, $dismissible = true ) |
||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | */ |
||
151 | protected function buildMessage( array $messages ) |
||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | protected function buildNotice( array $notice ) |
||
171 | } |
||
172 |
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.