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 Route 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 Route, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Route extends \yii\base\Object |
||
20 | { |
||
21 | const CACHE_TAG = 'mdm.admin.route'; |
||
22 | |||
23 | /** |
||
24 | * Assign or remove items |
||
25 | * @param array $routes |
||
26 | * @return array |
||
27 | */ |
||
28 | public function addNew($routes) |
||
59 | |||
60 | /** |
||
61 | * Assign or remove items |
||
62 | * @param array $routes |
||
63 | * @return array |
||
64 | */ |
||
65 | public function remove($routes) |
||
78 | |||
79 | /** |
||
80 | * Get available and assigned routes |
||
81 | * @return array |
||
82 | */ |
||
83 | public function getRoutes() |
||
100 | |||
101 | /** |
||
102 | * Get list of application routes |
||
103 | * @return array |
||
104 | */ |
||
105 | public function getAppRoutes($module = null) |
||
126 | |||
127 | /** |
||
128 | * Get route(s) recursive |
||
129 | * @param \yii\base\Module $module |
||
130 | * @param array $result |
||
131 | */ |
||
132 | protected function getRouteRecursive($module, &$result) |
||
133 | { |
||
134 | $token = "Get Route of '" . get_class($module) . "' with id '" . $module->uniqueId . "'"; |
||
135 | Yii::beginProfile($token, __METHOD__); |
||
136 | try { |
||
137 | foreach ($module->getModules() as $id => $child) { |
||
138 | if (($child = $module->getModule($id)) !== null) { |
||
139 | $this->getRouteRecursive($child, $result); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | foreach ($module->controllerMap as $id => $type) { |
||
144 | $this->getControllerActions($type, $id, $module, $result); |
||
145 | } |
||
146 | |||
147 | $namespace = trim($module->controllerNamespace, '\\') . '\\'; |
||
148 | $this->getControllerFiles($module, $namespace, '', $result); |
||
149 | $all = '/' . ltrim($module->uniqueId . '/*', '/'); |
||
150 | $result[$all] = $all; |
||
151 | } catch (\Exception $exc) { |
||
152 | Yii::error($exc->getMessage(), __METHOD__); |
||
153 | } |
||
154 | Yii::endProfile($token, __METHOD__); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Get list controller under module |
||
159 | * @param \yii\base\Module $module |
||
160 | * @param string $namespace |
||
161 | * @param string $prefix |
||
162 | * @param mixed $result |
||
163 | * @return mixed |
||
164 | */ |
||
165 | protected function getControllerFiles($module, $namespace, $prefix, &$result) |
||
195 | |||
196 | /** |
||
197 | * Get list action of controller |
||
198 | * @param mixed $type |
||
199 | * @param string $id |
||
200 | * @param \yii\base\Module $module |
||
201 | * @param string $result |
||
202 | */ |
||
203 | protected function getControllerActions($type, $id, $module, &$result) |
||
218 | |||
219 | /** |
||
220 | * Get route of action |
||
221 | * @param \yii\base\Controller $controller |
||
222 | * @param array $result all controller action. |
||
223 | */ |
||
224 | protected function getActionRoutes($controller, &$result) |
||
247 | |||
248 | /** |
||
249 | * Ivalidate cache |
||
250 | */ |
||
251 | public static function invalidate() |
||
257 | |||
258 | /** |
||
259 | * Set default rule of parameterize route. |
||
260 | */ |
||
261 | protected function setDefaultRule() |
||
267 | } |
||
268 |
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.