Conditions | 21 |
Paths | 318 |
Total Lines | 82 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
184 | public function discovery() |
||
185 | { |
||
186 | $addNum = 0; |
||
187 | $updateNum = 0; |
||
188 | |||
189 | foreach (Route::getRoutes()->getRoutesByName() as $k => $v) { |
||
190 | if (Str::startsWith($k, 'admin::')) { |
||
191 | // 取方法的第一行注释作为菜单的名称、分组名。格式:分组名称-菜单名称。未写分组名称,则注释直接作为菜单名称。未写注释则选用uri作为菜单名称。 |
||
192 | $action = explode('@', $v->getActionName()); |
||
193 | if (!method_exists($action[0], $action[1])) { |
||
194 | continue; |
||
195 | } |
||
196 | $reflection = new \ReflectionMethod($action[0], $action[1]); |
||
197 | $comment = trim(array_get(explode("\n", $reflection->getDocComment()), 1, ''), " \t\n\r\0\x0B*"); |
||
|
|||
198 | if ($comment === '') { |
||
199 | $data['name'] = $v->uri; |
||
200 | $data['group'] = ''; |
||
201 | } else { |
||
202 | if (Str::contains($comment, '-')) { |
||
203 | $arr = explode('-', $comment); |
||
204 | $data['name'] = trim($arr[1]); |
||
205 | $data['group'] = trim($arr[0]); |
||
206 | } else { |
||
207 | $data['name'] = trim($comment); |
||
208 | $data['group'] = ''; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | $data['route'] = $k; |
||
213 | $data['guard_name'] = 'admin'; |
||
214 | if (in_array('GET', $v->methods) && !Str::contains($v->uri, '{')) { |
||
215 | $data['status'] = Menu::STATUS_ENABLE; |
||
216 | } else { |
||
217 | $data['status'] = Menu::STATUS_DISABLE; |
||
218 | } |
||
219 | try { |
||
220 | $data['url'] = route($k, [], false); |
||
221 | } catch (UrlGenerationException $e) { |
||
222 | $data['url'] = ''; |
||
223 | } |
||
224 | |||
225 | try { |
||
226 | $model = MenuRepository::exist($k); |
||
227 | if ($model) { |
||
228 | if (($model->is_lock_name == Menu::UNLOCK_NAME && |
||
229 | ($model->name != $data['name'] || $model->group != $data['group'])) || |
||
230 | ($data['url'] != '' && $model->url != $data['url'])) { |
||
231 | unset($data['status']); |
||
232 | MenuRepository::update($model->id, $data); |
||
233 | $updateNum++; |
||
234 | } |
||
235 | } else { |
||
236 | MenuRepository::add($data); |
||
237 | $addNum++; |
||
238 | } |
||
239 | } catch (QueryException $e) { |
||
240 | if ($addNum > 0 || $updateNum > 0) { |
||
241 | event(new MenuUpdated()); |
||
242 | } |
||
243 | |||
244 | if ($e->getCode() == 23000) { |
||
245 | return [ |
||
246 | 'code' => 1, |
||
247 | 'msg' => "唯一性冲突:请检查菜单名称或路由名称。name: {$data['name']} route: {$data['route']}", |
||
248 | ]; |
||
249 | } else { |
||
250 | return [ |
||
251 | 'code' => 2, |
||
252 | 'msg' => $e->getMessage(), |
||
253 | ]; |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | |||
259 | if ($addNum > 0 || $updateNum > 0) { |
||
260 | event(new MenuUpdated()); |
||
261 | } |
||
262 | return [ |
||
263 | 'code' => 0, |
||
264 | 'msg' => "更新成功。新增菜单数:{$addNum},更新菜单数:{$updateNum}。", |
||
265 | 'redirect' => true |
||
266 | ]; |
||
350 |