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:
Complex classes like ProductParametersBehavior often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductParametersBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ProductParametersBehavior extends CModelBehavior |
||
17 | { |
||
18 | /** |
||
19 | * @var ProductParameterName[] |
||
20 | */ |
||
21 | protected $parameters; |
||
22 | |||
23 | protected $basketParameter; |
||
24 | |||
25 | /** |
||
26 | * @param null $key |
||
27 | * @param CDbCriteria $groupCriteria критерия группы параметров |
||
28 | * @param CDbCriteria|null|false $criteria критерия параметров |
||
29 | * |
||
30 | * @return ProductParameterName[] |
||
31 | */ |
||
32 | public function getParameters($key = null, CDbCriteria $groupCriteria = null, $criteria = null) |
||
61 | |||
62 | /** |
||
63 | * @param array $parameters |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function setParameters($parameters = array()) |
||
72 | |||
73 | /** |
||
74 | * @param $parameter |
||
75 | */ |
||
76 | public function addParameter($parameter) |
||
80 | |||
81 | /** |
||
82 | * @return ProductParameterName[] |
||
83 | */ |
||
84 | public function getProductOneParameters() |
||
88 | |||
89 | /** |
||
90 | * @return ProductParameterName[] |
||
91 | */ |
||
92 | public function getProductLineParameters() |
||
96 | |||
97 | /** |
||
98 | * @return ProductParameterName[] |
||
99 | */ |
||
100 | public function getProductTabletParameters() |
||
104 | |||
105 | /** |
||
106 | * @return ProductParameterName|null |
||
107 | */ |
||
108 | public function getBasketParameter() |
||
116 | |||
117 | /** |
||
118 | * @param string|array $key |
||
119 | * @param bool $notEmptyOnly |
||
120 | * |
||
121 | * @return null|ProductParameterName |
||
122 | */ |
||
123 | public function getParameterByKey($key, $notEmptyOnly = true) |
||
140 | |||
141 | /** |
||
142 | * @param $id |
||
143 | * @param bool $noEmpty |
||
144 | * |
||
145 | * @return null|ProductParameterName |
||
146 | */ |
||
147 | public function getParameterById($id, $noEmpty = true) |
||
162 | |||
163 | View Code Duplication | private function getCurrentProductParameterNameIds() |
|
173 | |||
174 | /** |
||
175 | * @param string $name |
||
176 | * @param string $value |
||
177 | * @param string $key |
||
178 | * |
||
179 | * @return ProductParameter|stdClass |
||
180 | */ |
||
181 | private function createFakeParameter($name, $value, $key = 'fake') |
||
191 | |||
192 | private function getParametersByAttributes(array $attributes, $notEmptyValue = true, $exceptionKeys = array()) |
||
226 | } |
||
227 |
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.