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 |
||
5 | class Blog |
||
6 | { |
||
7 | public $id = ''; |
||
8 | public $title = ''; |
||
9 | public $link = ''; |
||
10 | public $posts = 0; |
||
11 | public $name = ''; |
||
12 | public $updated; |
||
13 | public $description = ''; |
||
14 | public $ask = false; |
||
15 | public $ask_anon; |
||
16 | public $followers = 0; |
||
17 | |||
18 | /** |
||
19 | * Create a new Blog object based on an array of attributes and a mapping |
||
20 | * from those attributes to the Blog object's attributes. |
||
21 | * |
||
22 | * The keys are the name of the Blog object attributes, while the values |
||
23 | * are the key of that attribute in the $attributes array. Like so: |
||
24 | * ['blog_object_attribute' => 'key_in_attributes_array'] |
||
25 | * |
||
26 | * @param array $mapping |
||
27 | * @param array $attributes |
||
28 | * @return static |
||
29 | */ |
||
30 | View Code Duplication | public static function create(array $mapping, array $attributes) |
|
40 | } |
||
41 |
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.