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 NestedExpression extends AbstractExpression |
||
16 | { |
||
17 | /** |
||
18 | * |
||
19 | * @var integer |
||
20 | */ |
||
21 | const AND_OPERATOR = 6; |
||
22 | /** |
||
23 | * |
||
24 | * @var integer |
||
25 | */ |
||
26 | const OR_OPERATOR = 7; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | * |
||
31 | * @var int |
||
32 | */ |
||
33 | private $expressionOperator = self::AND_OPERATOR; |
||
34 | |||
35 | /** |
||
36 | * @var AbstractExpression[] |
||
37 | */ |
||
38 | private $expressions = []; |
||
39 | |||
40 | /** |
||
41 | * @param array $expressions |
||
42 | * @param null $expressionOperator |
||
43 | * @throws ArgumentNotNumericException |
||
44 | * @throws InvalidArgumentException |
||
45 | */ |
||
46 | 3 | public function __construct(array $expressions = null, $expressionOperator = null) |
|
56 | |||
57 | |||
58 | /** |
||
59 | * Флаг определяющий есть ли вложенные свойства |
||
60 | * |
||
61 | * @return boolean |
||
62 | */ |
||
63 | 3 | public function isNested() |
|
67 | |||
68 | /** |
||
69 | * Возвращает тип вложенного выражения |
||
70 | * |
||
71 | * @return int |
||
72 | */ |
||
73 | 3 | public function getExpressionOperator() |
|
77 | |||
78 | /** |
||
79 | * Устанавливает тип вложенного выражения |
||
80 | * |
||
81 | * @param int $expressionOperator |
||
82 | * @return $this |
||
83 | * @throws ArgumentNotNumericException |
||
84 | */ |
||
85 | 3 | View Code Duplication | public function setExpressionOperator($expressionOperator) |
96 | |||
97 | /** |
||
98 | * Возвращает выражения |
||
99 | * |
||
100 | * @return AbstractExpression[] |
||
101 | */ |
||
102 | 3 | public function getExpressions() |
|
106 | |||
107 | /** |
||
108 | * Устанавливает выражения |
||
109 | * |
||
110 | * @param AbstractExpression[] $expressions |
||
111 | * @return $this |
||
112 | * @throws InvalidArgumentException |
||
113 | */ |
||
114 | 3 | public function setExpressions(array $expressions = []) |
|
121 | |||
122 | /** |
||
123 | * Проверка корректности колекции выражений |
||
124 | * |
||
125 | * @param array $expressions |
||
126 | * @return void |
||
127 | * @throws InvalidArgumentException |
||
128 | */ |
||
129 | 3 | protected function validExpressions(array $expressions = []) |
|
138 | |||
139 | /** |
||
140 | * Колличество выражений |
||
141 | * |
||
142 | * @return int |
||
143 | */ |
||
144 | public function getExpressionCount() |
||
150 | } |
||
151 |
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.