Conditions | 1 |
Paths | 1 |
Total Lines | 84 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 1 | 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 |
||
34 | public function __invoke(ContainerInterface $container) |
||
35 | { |
||
36 | $routes = [ |
||
37 | // Lonely pages of site |
||
38 | Route::get('/', [SiteController::class, 'index']) |
||
39 | ->name('site/index'), |
||
40 | Route::methods([Method::GET, Method::POST], '/contact', [ContactController::class, 'contact']) |
||
41 | ->name('site/contact'), |
||
42 | Route::methods([Method::GET, Method::POST], '/login', [AuthController::class, 'login']) |
||
43 | ->name('site/login'), |
||
44 | Route::get('/logout', [AuthController::class, 'logout']) |
||
45 | ->name('site/logout'), |
||
46 | Route::methods([Method::GET, Method::POST], '/signup', [SignupController::class, 'signup']) |
||
47 | ->name('site/signup'), |
||
48 | |||
49 | // User |
||
50 | Group::create('/user', [ |
||
51 | // Index |
||
52 | Route::get('[/page-{page:\d+}]', [UserController::class, 'index']) |
||
53 | ->name('user/index'), |
||
54 | // Profile page |
||
55 | Route::get('/{login}', [UserController::class, 'profile']) |
||
56 | ->name('user/profile'), |
||
57 | ]), |
||
58 | |||
59 | // User |
||
60 | Group::create('/api', [ |
||
61 | Route::get('/info/v1', function (DataResponseFactoryInterface $responseFactory) { |
||
62 | return $responseFactory->createResponse(['version' => '1.0', 'author' => 'yiisoft']); |
||
63 | })->name('api/info/v1'), |
||
64 | Route::get('/info/v2', ApiInfo::class) |
||
65 | ->addMiddleware(FormatDataResponseAsJson::class) |
||
66 | ->name('api/info/v2'), |
||
67 | Route::get('/user', [ApiUserController::class, 'index']) |
||
68 | ->name('api/user/index'), |
||
69 | Route::get('/user/{login}', [ApiUserController::class, 'profile']) |
||
70 | ->addMiddleware(FormatDataResponseAsJson::class) |
||
71 | ->name('api/user/profile'), |
||
72 | ])->addMiddleware(ApiDataWrapper::class)->addMiddleware(FormatDataResponseAsXml::class), |
||
73 | |||
74 | // Blog routes |
||
75 | Group::create('/blog', [ |
||
76 | // Index |
||
77 | Route::get('[/page{page:\d+}]', [BlogController::class, 'index']) |
||
78 | ->name('blog/index'), |
||
79 | // Add Post page |
||
80 | Route::methods([Method::GET, Method::POST], '/page/add', [PostController::class, 'add']) |
||
81 | ->name('blog/add')->addMiddleware(Authentication::class), |
||
82 | // Edit Post page |
||
83 | Route::methods([Method::GET, Method::POST], '/page/edit/{slug}', [PostController::class, 'edit']) |
||
84 | ->name('blog/edit') |
||
85 | ->addMiddleware(Authentication::class) |
||
86 | ->addMiddleware(fn (AccessChecker $checker) => $checker->withPermission('editPost')), |
||
87 | // Post page |
||
88 | Route::get('/page/{slug}', [PostController::class, 'index']) |
||
89 | ->name('blog/post'), |
||
90 | // Tag page |
||
91 | Route::get('/tag/{label}[/page{page:\d+}]', [TagController::class, 'index']) |
||
92 | ->name('blog/tag'), |
||
93 | // Archive |
||
94 | Group::create('/archive', [ |
||
95 | // Index page |
||
96 | Route::get('', [ArchiveController::class, 'index']) |
||
97 | ->name('blog/archive/index'), |
||
98 | // Yearly page |
||
99 | Route::get('/{year:\d+}', [ArchiveController::class, 'yearlyArchive']) |
||
100 | ->name('blog/archive/year'), |
||
101 | // Monthly page |
||
102 | Route::get('/{year:\d+}-{month:\d+}[/page{page:\d+}]', [ArchiveController::class, 'monthlyArchive']) |
||
103 | ->name('blog/archive/month') |
||
104 | ]), |
||
105 | // comments |
||
106 | Route::get('/comments/[next/{next}]', [CommentController::class, 'index']) |
||
107 | ->name('blog/comment/index'), |
||
108 | ]), |
||
109 | ]; |
||
110 | |||
111 | $collector = $container->get(RouteCollectorInterface::class); |
||
112 | $collector->addGroup( |
||
113 | Group::create(null, $routes) |
||
114 | ->addMiddleware(FormatDataResponse::class) |
||
115 | ); |
||
116 | |||
117 | return new UrlMatcher(new RouteCollection($collector)); |
||
118 | } |
||
120 |