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 |
||
15 | class ConstraintRepository implements ConstraintLookup { |
||
16 | |||
17 | /** @var ILoadBalancer */ |
||
18 | private $lb; |
||
19 | |||
20 | public function __construct( ILoadBalancer $lb ) { |
||
23 | |||
24 | /** |
||
25 | * @param PropertyId $propertyId |
||
26 | * |
||
27 | * @return Constraint[] |
||
28 | */ |
||
29 | View Code Duplication | public function queryConstraintsForProperty( PropertyId $propertyId ) { |
|
40 | |||
41 | private function encodeConstraintParameters( array $constraintParameters ) { |
||
50 | |||
51 | /** |
||
52 | * @param Constraint[] $constraints |
||
53 | * |
||
54 | * @throws DBUnexpectedError |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function insertBatch( array $constraints ) { |
||
73 | |||
74 | /** |
||
75 | * Delete all constraints for the property ID. |
||
76 | * |
||
77 | * @param PropertyId $propertyId |
||
78 | * |
||
79 | * @throws DBUnexpectedError |
||
80 | */ |
||
81 | View Code Duplication | public function deleteForProperty( PropertyId $propertyId ) { |
|
90 | |||
91 | /** |
||
92 | * @param IResultWrapper $results |
||
93 | * |
||
94 | * @return Constraint[] |
||
95 | */ |
||
96 | private function convertToConstraints( IResultWrapper $results ) { |
||
122 | |||
123 | } |
||
124 |
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.