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 User extends ArrayCollection |
||
10 | { |
||
11 | /** |
||
12 | * @param UserEntity $user |
||
13 | * @return $this |
||
14 | */ |
||
15 | 1 | public function update(UserEntity $user) |
|
16 | { |
||
17 | 1 | $key = $this->findKey($user); |
|
18 | 1 | if($key) { |
|
19 | |||
20 | 1 | $this->offsetSet($key,$user); |
|
21 | 1 | return $this; |
|
22 | |||
23 | } |
||
24 | throw new LogicException('User was not in the collection.'); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param UserEntity $user |
||
29 | */ |
||
30 | 4 | public function append(UserEntity $user) |
|
34 | |||
35 | /** |
||
36 | * @return UserEntity|null |
||
37 | */ |
||
38 | 1 | public function current() |
|
42 | |||
43 | /** |
||
44 | * @param UserEntity $user |
||
45 | * @return bool|int |
||
46 | */ |
||
47 | 2 | View Code Duplication | public function findKey(UserEntity $user) |
59 | |||
60 | |||
61 | |||
62 | 2 | View Code Duplication | public function findById($id) |
75 | |||
76 | } |
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.