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 |
||
20 | class Group |
||
21 | { |
||
22 | /** |
||
23 | * Query. |
||
24 | * |
||
25 | * @var Query |
||
26 | */ |
||
27 | protected $query; |
||
28 | |||
29 | /** |
||
30 | * Constructor. |
||
31 | * |
||
32 | * @param Query $query |
||
33 | */ |
||
34 | public function __construct(Query $query) |
||
38 | |||
39 | /** |
||
40 | * Get groups. |
||
41 | * |
||
42 | * @static |
||
43 | * |
||
44 | * @param Client $client |
||
45 | * @param string $query |
||
46 | * |
||
47 | * @return LdapGroupProvider[] |
||
48 | */ |
||
49 | public static function getGroups(Client $client, $query) |
||
55 | |||
56 | /** |
||
57 | * Find groups. |
||
58 | * |
||
59 | * @param string $query |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | View Code Duplication | public function find($query) |
|
74 | |||
75 | /** |
||
76 | * Build groups list. |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | protected function build() |
||
90 | |||
91 | /** |
||
92 | * Ge the list of attributes to fetch when reading the LDAP group entry. |
||
93 | * |
||
94 | * Must returns array with index that start at 0 otherwise ldap_search returns a warning "Array initialization wrong" |
||
95 | * |
||
96 | * @return array |
||
97 | */ |
||
98 | public function getAttributes() |
||
104 | |||
105 | /** |
||
106 | * Get LDAP group name attribute. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getAttributeName() |
||
118 | |||
119 | /** |
||
120 | * Get LDAP group base DN. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getBasDn() |
||
132 | } |
||
133 |
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.