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 ConstraintRepositoryStore implements ConstraintStore { |
||
14 | |||
15 | /** @var ILoadBalancer */ |
||
16 | private $lb; |
||
17 | |||
18 | /** @var string|false */ |
||
19 | private $dbName; |
||
20 | |||
21 | /** |
||
22 | * @param ILoadBalancer $lb Load balancer for database connections. |
||
23 | * Must match $dbName, i.e. if $dbName is not false, |
||
24 | * then using the main DBLoadBalancer service may be incorrect. |
||
25 | * @param string|false $dbName Database name ($domain for ILoadBalancer methods). |
||
26 | */ |
||
27 | public function __construct( ILoadBalancer $lb, $dbName ) { |
||
31 | |||
32 | private function encodeConstraintParameters( array $constraintParameters ) { |
||
41 | |||
42 | /** |
||
43 | * @param Constraint[] $constraints |
||
44 | * |
||
45 | * @throws DBUnexpectedError |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function insertBatch( array $constraints ) { |
||
64 | |||
65 | /** |
||
66 | * Delete all constraints for the property ID. |
||
67 | * |
||
68 | * @param PropertyId $propertyId |
||
69 | * |
||
70 | * @throws DBUnexpectedError |
||
71 | */ |
||
72 | View Code Duplication | public function deleteForProperty( PropertyId $propertyId ) { |
|
81 | |||
82 | } |
||
83 |
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.