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 Specification extends ArrayCollection implements SpecificationInterface |
||
14 | { |
||
15 | const AND_X = 'andX'; |
||
16 | const OR_X = 'orX'; |
||
17 | |||
18 | protected static $types = [self::OR_X, self::AND_X]; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $type = self::AND_X; |
||
24 | |||
25 | /** |
||
26 | * @param SpecificationInterface[] $elements |
||
27 | */ |
||
28 | public function __construct(array $elements = []) |
||
34 | |||
35 | /** |
||
36 | * @param SpecificationInterface $value |
||
37 | * |
||
38 | * @throws InvalidArgumentException |
||
39 | * |
||
40 | * @return bool |
||
41 | */ |
||
42 | View Code Duplication | public function add($value) |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function modify(QueryBuilder $queryBuilder, $dqlAlias) |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function isSatisfiedBy($value) |
||
91 | |||
92 | /** |
||
93 | * @param SpecificationInterface[] $children |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | protected function setChildren(array $children) |
||
104 | |||
105 | /** |
||
106 | * Set the type of comparison. |
||
107 | * |
||
108 | * @param string $type |
||
109 | * |
||
110 | * @throws InvalidArgumentException |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | View Code Duplication | protected function setType($type) |
|
125 | } |
||
126 |
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.