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 Profile |
||
6 | { |
||
7 | public $provider; |
||
8 | public $id; |
||
9 | public $email; |
||
10 | public $name; |
||
11 | public $first_name; |
||
12 | public $middle_name; |
||
13 | public $last_name; |
||
14 | public $username; |
||
15 | public $link; |
||
16 | public $raw_response; |
||
17 | public $avatar; |
||
18 | public $likes; |
||
19 | |||
20 | |||
21 | /** |
||
22 | * Create a new Profile object based on an array of attributes and a mapping |
||
23 | * from those attributes to the Profile object's attributes. |
||
24 | * The $mapping array should have this format (example for Facebook): |
||
25 | * |
||
26 | * $mapping = [ |
||
27 | * 'id' => 'id', |
||
28 | * 'email' => 'email', |
||
29 | * 'name' => 'name', |
||
30 | * 'first_name' => 'first_name', |
||
31 | * 'middle_name' => 'middle_name', |
||
32 | * 'last_name' => 'last_name', |
||
33 | * 'username' => 'username', |
||
34 | * 'link' => 'link' |
||
35 | * ]; |
||
36 | * |
||
37 | * The keys are the name of the Profile object attributes, while the values |
||
38 | * are the key of that attribute in the $attributes array. Like so: |
||
39 | * ['profile_object_attribute' => 'key_in_attributes_array'] |
||
40 | * |
||
41 | * @param array $mapping |
||
42 | * @param array $attributes |
||
43 | * @return static |
||
44 | */ |
||
45 | View Code Duplication | public static function create(array $mapping, array $attributes) |
|
55 | } |
||
56 |
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.