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 |
||
16 | class UserRole extends DataObject |
||
17 | { |
||
18 | /** @var int */ |
||
19 | private $user; |
||
20 | /** @var string */ |
||
21 | private $role; |
||
22 | |||
23 | /** |
||
24 | * @param int $userId |
||
25 | * @param PdoDatabase $database |
||
26 | * |
||
27 | * @return UserRole[] |
||
28 | */ |
||
29 | View Code Duplication | public static function getForUser($userId, PdoDatabase $database) |
|
47 | |||
48 | /** |
||
49 | * Saves a data object to the database, either updating or inserting a record. |
||
50 | * |
||
51 | * @throws Exception |
||
52 | */ |
||
53 | View Code Duplication | public function save() |
|
74 | |||
75 | #region Properties |
||
76 | |||
77 | /** |
||
78 | * @return int |
||
79 | */ |
||
80 | public function getUser() |
||
84 | |||
85 | /** |
||
86 | * @param int $user |
||
87 | */ |
||
88 | public function setUser($user) |
||
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getRole() |
||
100 | |||
101 | /** |
||
102 | * @param string $role |
||
103 | */ |
||
104 | public function setRole($role) |
||
108 | #endregion |
||
109 | } |
||
110 |
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.