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 |
||
13 | class LDAPFakeGateway extends LDAPGateway implements TestOnly |
||
14 | { |
||
15 | public function __construct() |
||
19 | |||
20 | private static $data = [ |
||
21 | 'groups' => [ |
||
22 | 'CN=Users,DC=playpen,DC=local' => [ |
||
23 | ['dn' => 'CN=Group1,CN=Users,DC=playpen,DC=local'], |
||
24 | ['dn' => 'CN=Group2,CN=Users,DC=playpen,DC=local'], |
||
25 | ['dn' => 'CN=Group3,CN=Users,DC=playpen,DC=local'], |
||
26 | ['dn' => 'CN=Group4,CN=Users,DC=playpen,DC=local'], |
||
27 | ['dn' => 'CN=Group5,CN=Users,DC=playpen,DC=local'] |
||
28 | ], |
||
29 | 'CN=Others,DC=playpen,DC=local' => [ |
||
30 | ['dn' => 'CN=Group6,CN=Others,DC=playpen,DC=local'], |
||
31 | ['dn' => 'CN=Group7,CN=Others,DC=playpen,DC=local'], |
||
32 | ['dn' => 'CN=Group8,CN=Others,DC=playpen,DC=local'] |
||
33 | ] |
||
34 | ], |
||
35 | 'users' => [ |
||
36 | '123' => [ |
||
37 | 'distinguishedname' => 'CN=Joe,DC=playpen,DC=local', |
||
38 | 'objectguid' => '123', |
||
39 | 'cn' => 'jbloggs', |
||
40 | 'useraccountcontrol' => '1', |
||
41 | 'givenname' => 'Joe', |
||
42 | 'sn' => 'Bloggs', |
||
43 | 'mail' => '[email protected]', |
||
44 | 'password' => 'mockPassword', |
||
45 | 'canonicalName'=>'mockCanonicalName', |
||
46 | 'userprincipalname' => '[email protected]', |
||
47 | 'samaccountname' => 'joe' |
||
48 | ] |
||
49 | ] |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | */ |
||
55 | public function authenticate($username, $password) |
||
71 | |||
72 | public function getNodes($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
75 | |||
76 | public function getGroups($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
82 | |||
83 | public function getNestedGroups($dn, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
86 | |||
87 | public function getGroupByGUID($guid, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
90 | |||
91 | public function getUsers($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
94 | |||
95 | public function getUserByGUID($guid, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
99 | |||
100 | public function update($dn, array $attributes) |
||
103 | |||
104 | public function delete($dn, $recursively = false) |
||
107 | |||
108 | public function move($fromDn, $toDn, $recursively = false) |
||
111 | |||
112 | public function add($dn, array $attributes) |
||
115 | |||
116 | protected function search($filter, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
145 | |||
146 | /** |
||
147 | * Mock to search trough dummy $data. |
||
148 | * |
||
149 | * @param string $email |
||
150 | * @param null $baseDn |
||
151 | * @param int $scope |
||
152 | * @param array $attributes |
||
153 | * @return array |
||
154 | */ |
||
155 | View Code Duplication | public function getUserByEmail($email, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
|
167 | |||
168 | /** |
||
169 | * Mock to search trough dummy $data. |
||
170 | * |
||
171 | * @param string $username |
||
172 | * @param null $baseDn |
||
173 | * @param int $scope |
||
174 | * @param array $attributes |
||
175 | * @return array |
||
176 | * @internal param string $email |
||
177 | */ |
||
178 | View Code Duplication | public function getUserByUsername($username, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
|
190 | } |
||
191 |
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.