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 RouteModel 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 RouteModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class RouteModel extends Object |
||
18 | { |
||
19 | /** |
||
20 | * @var string cache tag |
||
21 | */ |
||
22 | const CACHE_TAG = 'yii2mod.rbac.route'; |
||
23 | |||
24 | /** |
||
25 | * @var \yii\caching\Cache |
||
26 | */ |
||
27 | public $cache; |
||
28 | |||
29 | /** |
||
30 | * @var int cache duration |
||
31 | */ |
||
32 | public $cacheDuration = 3600; |
||
33 | |||
34 | /** |
||
35 | * @var array list of module IDs that will be excluded |
||
36 | */ |
||
37 | public $excludeModules = []; |
||
38 | |||
39 | /** |
||
40 | * @var \yii\rbac\ManagerInterface |
||
41 | */ |
||
42 | protected $manager; |
||
43 | |||
44 | /** |
||
45 | * RouteModel constructor. |
||
46 | * |
||
47 | * @param array $config |
||
48 | */ |
||
49 | public function __construct(array $config = []) |
||
50 | { |
||
51 | $this->cache = Yii::$app->cache; |
||
52 | $this->manager = Yii::$app->authManager; |
||
53 | |||
54 | parent::__construct($config); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Assign items |
||
59 | * |
||
60 | * @param array $routes |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | View Code Duplication | public function addNew(array $routes): bool |
|
|
|||
65 | { |
||
66 | foreach ($routes as $route) { |
||
67 | $this->manager->add($this->manager->createPermission('/' . trim($route, ' /'))); |
||
68 | } |
||
69 | |||
70 | $this->invalidate(); |
||
71 | |||
72 | return true; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Remove items |
||
77 | * |
||
78 | * @param array $routes |
||
79 | * |
||
80 | * @return bool |
||
81 | */ |
||
82 | View Code Duplication | public function remove(array $routes): bool |
|
83 | { |
||
84 | foreach ($routes as $route) { |
||
85 | $item = $this->manager->createPermission('/' . trim($route, '/')); |
||
86 | $this->manager->remove($item); |
||
87 | } |
||
88 | $this->invalidate(); |
||
89 | |||
90 | return true; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Get available and assigned routes |
||
95 | * |
||
96 | * @return array |
||
97 | */ |
||
98 | public function getAvailableAndAssignedRoutes(): array |
||
99 | { |
||
100 | $routes = $this->getAppRoutes(); |
||
101 | $exists = []; |
||
102 | |||
103 | foreach (array_keys($this->manager->getPermissions()) as $name) { |
||
104 | if ($name[0] !== '/') { |
||
105 | continue; |
||
106 | } |
||
107 | $exists[] = $name; |
||
108 | unset($routes[$name]); |
||
109 | } |
||
110 | |||
111 | return [ |
||
112 | 'available' => array_keys($routes), |
||
113 | 'assigned' => $exists, |
||
114 | ]; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Get list of application routes |
||
119 | * |
||
120 | * @param null|string $module |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | public function getAppRoutes(string $module = null): array |
||
125 | { |
||
126 | if ($module === null) { |
||
127 | $module = Yii::$app; |
||
128 | } else { |
||
129 | $module = Yii::$app->getModule($module); |
||
130 | } |
||
131 | |||
132 | $key = [__METHOD__, $module->getUniqueId()]; |
||
133 | $result = (($this->cache !== null) ? $this->cache->get($key) : false); |
||
134 | |||
135 | if ($result === false) { |
||
136 | $result = []; |
||
137 | $this->getRouteRecursive($module, $result); |
||
138 | if ($this->cache !== null) { |
||
139 | $this->cache->set($key, $result, $this->cacheDuration, new TagDependency([ |
||
140 | 'tags' => self::CACHE_TAG, |
||
141 | ])); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | return $result; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Invalidate the cache |
||
150 | */ |
||
151 | public function invalidate() |
||
152 | { |
||
153 | if ($this->cache !== null) { |
||
154 | TagDependency::invalidate($this->cache, self::CACHE_TAG); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Get route(s) recursive |
||
160 | * |
||
161 | * @param Module $module |
||
162 | * @param array $result |
||
163 | */ |
||
164 | protected function getRouteRecursive(Module $module, &$result) |
||
165 | { |
||
166 | if (!in_array($module->id, $this->excludeModules)) { |
||
167 | $token = "Get Route of '" . get_class($module) . "' with id '" . $module->uniqueId . "'"; |
||
168 | Yii::beginProfile($token, __METHOD__); |
||
169 | |||
170 | try { |
||
171 | foreach ($module->getModules() as $id => $child) { |
||
172 | if (($child = $module->getModule($id)) !== null) { |
||
173 | $this->getRouteRecursive($child, $result); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | foreach ($module->controllerMap as $id => $type) { |
||
178 | $this->getControllerActions($type, $id, $module, $result); |
||
179 | } |
||
180 | |||
181 | $namespace = trim($module->controllerNamespace, '\\') . '\\'; |
||
182 | $this->getControllerFiles($module, $namespace, '', $result); |
||
183 | $all = '/' . ltrim($module->uniqueId . '/*', '/'); |
||
184 | $result[$all] = $all; |
||
185 | } catch (\Exception $exc) { |
||
186 | Yii::error($exc->getMessage(), __METHOD__); |
||
187 | } |
||
188 | |||
189 | Yii::endProfile($token, __METHOD__); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Get list controllers under module |
||
195 | * |
||
196 | * @param Module $module |
||
197 | * @param string $namespace |
||
198 | * @param string $prefix |
||
199 | * @param mixed $result |
||
200 | */ |
||
201 | protected function getControllerFiles(Module $module, string $namespace, string $prefix, &$result) |
||
234 | |||
235 | /** |
||
236 | * Get list actions of controller |
||
237 | * |
||
238 | * @param mixed $type |
||
239 | * @param string $id |
||
240 | * @param Module $module |
||
241 | * @param mixed $result |
||
242 | */ |
||
243 | protected function getControllerActions($type, $id, Module $module, &$result) |
||
260 | |||
261 | /** |
||
262 | * Get route of action |
||
263 | * |
||
264 | * @param Controller $controller |
||
265 | * @param array $result all controller action |
||
266 | */ |
||
267 | protected function getActionRoutes(Controller $controller, &$result) |
||
293 | } |
||
294 |
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.