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 ProductParametersBehavior extends CModelBehavior |
||
17 | { |
||
18 | /** |
||
19 | * @var ProductParameterName[] |
||
20 | */ |
||
21 | protected $parameters; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $cache; |
||
27 | |||
28 | /** |
||
29 | * @param null $key |
||
30 | * @param CDbCriteria $groupCriteria критерия группы параметров |
||
31 | * @param CDbCriteria|null|false $criteria критерия параметров |
||
32 | * |
||
33 | * @return ProductParameterName[] |
||
34 | */ |
||
35 | 2 | public function getParameters($key = null, CDbCriteria $groupCriteria = null, $criteria = null) |
|
64 | |||
65 | /** |
||
66 | * @param array $parameters |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | 2 | public function setParameters($parameters = array()) |
|
75 | |||
76 | /** |
||
77 | * @param $parameter |
||
78 | */ |
||
79 | 2 | public function addParameter($parameter) |
|
83 | |||
84 | /** |
||
85 | * @param $id |
||
86 | * @param bool $notEmptyOnly |
||
87 | * |
||
88 | * @return null|ProductParameterName |
||
89 | */ |
||
90 | public function getParameterById($id, $notEmptyOnly = true) |
||
94 | |||
95 | /** |
||
96 | * @param array $idList |
||
97 | * @param bool|true $notEmptyOnly |
||
98 | * |
||
99 | * @return null|ProductParameterName[] |
||
100 | */ |
||
101 | public function getParametersByIdList(array $idList, $notEmptyOnly = true) |
||
105 | |||
106 | /** |
||
107 | * @param string|array $key |
||
108 | * @param bool $notEmptyOnly |
||
109 | * |
||
110 | * @return null|ProductParameterName |
||
111 | */ |
||
112 | public function getParameterByKey($key, $notEmptyOnly = true ) |
||
116 | |||
117 | /** |
||
118 | * @param string|array $keys |
||
119 | * @param bool $notEmptyOnly |
||
120 | * |
||
121 | * @return null|ProductParameterName[] |
||
122 | */ |
||
123 | public function getParametersByKeys(array $keys, $notEmptyOnly = true) |
||
127 | |||
128 | /** |
||
129 | * @return ProductParameterName[] |
||
130 | */ |
||
131 | 1 | public function getParametersCard() |
|
135 | |||
136 | /** |
||
137 | * @return ProductParameterName[] |
||
138 | */ |
||
139 | public function getParametersLine() |
||
143 | |||
144 | /** |
||
145 | * @return ProductParameterName[] |
||
146 | */ |
||
147 | public function getParametersTablet() |
||
151 | |||
152 | /** |
||
153 | * @return ProductParameterName|null |
||
154 | */ |
||
155 | public function getParametersBasket() |
||
159 | |||
160 | 1 | View Code Duplication | private function getCurrentProductParameterNameIds() |
170 | |||
171 | /** |
||
172 | * @param string $name |
||
173 | * @param string $value |
||
174 | * @param string $key |
||
175 | * |
||
176 | * @return ProductParameter|stdClass |
||
177 | */ |
||
178 | private function createFakeParameter($name, $value, $key = 'fake') |
||
188 | |||
189 | /** |
||
190 | * Выбирает параметры по заданным атрибутам |
||
191 | * Пример: |
||
192 | * $this->getParametersByAttributes(array('section_list' => 1)); |
||
193 | * $this->getParametersByAttributes(array('key' => array('test', 'test2'))); |
||
194 | * $this->getParametersByAttributes(array('id' => 'test'), true); |
||
195 | * |
||
196 | * @param array $attributes |
||
197 | * @param bool $onlyOne только один параметр |
||
198 | * @param bool|true $notEmptyValue |
||
199 | * @param array $exceptionKeys |
||
200 | * |
||
201 | * @return array |
||
202 | * @throws CHttpException |
||
203 | */ |
||
204 | private function getParametersByAttributes(array $attributes, $onlyOne = false, $notEmptyValue = true, $exceptionKeys = array()) |
||
252 | } |
||
253 |
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.