| Conditions | 18 |
| Paths | 1 |
| Total Lines | 182 |
| Code Lines | 121 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 211 | protected function registerMacros() |
||
| 212 | { |
||
| 213 | Route::macro('moduleShowWithPreview', function ( |
||
| 214 | $moduleName, |
||
| 215 | $routePrefix = null, |
||
| 216 | $controllerName = null |
||
| 217 | ) { |
||
| 218 | if ($routePrefix === null) { |
||
| 219 | $routePrefix = $moduleName; |
||
| 220 | } |
||
| 221 | |||
| 222 | if ($controllerName === null) { |
||
| 223 | $controllerName = ucfirst(Str::plural($moduleName)); |
||
| 224 | } |
||
| 225 | |||
| 226 | $routePrefix = empty($routePrefix) |
||
| 227 | ? '/' |
||
| 228 | : (Str::startsWith($routePrefix, '/') |
||
| 229 | ? $routePrefix |
||
| 230 | : '/' . $routePrefix); |
||
| 231 | $routePrefix = Str::endsWith($routePrefix, '/') |
||
| 232 | ? $routePrefix |
||
| 233 | : $routePrefix . '/'; |
||
| 234 | |||
| 235 | Route::name($moduleName . '.show')->get( |
||
| 236 | $routePrefix . '{slug}', |
||
| 237 | $controllerName . 'Controller@show' |
||
| 238 | ); |
||
| 239 | Route::name($moduleName . '.preview') |
||
| 240 | ->get( |
||
| 241 | '/admin-preview' . $routePrefix . '{slug}', |
||
| 242 | $controllerName . 'Controller@show' |
||
| 243 | ) |
||
| 244 | ->middleware(['web', 'twill_auth:twill_users', 'can:list']); |
||
| 245 | }); |
||
| 246 | |||
| 247 | Route::macro('module', function ( |
||
| 248 | $slug, |
||
| 249 | $options = [], |
||
| 250 | $resource_options = [], |
||
| 251 | $resource = true |
||
| 252 | ) { |
||
| 253 | $slugs = explode('.', $slug); |
||
| 254 | $prefixSlug = str_replace('.', '/', $slug); |
||
| 255 | $_slug = Arr::last($slugs); |
||
| 256 | $className = implode( |
||
| 257 | '', |
||
| 258 | array_map(function ($s) { |
||
| 259 | return ucfirst(Str::singular($s)); |
||
| 260 | }, $slugs) |
||
| 261 | ); |
||
| 262 | |||
| 263 | $customRoutes = $defaults = [ |
||
| 264 | 'reorder', |
||
| 265 | 'publish', |
||
| 266 | 'bulkPublish', |
||
| 267 | 'browser', |
||
| 268 | 'feature', |
||
| 269 | 'bulkFeature', |
||
| 270 | 'tags', |
||
| 271 | 'preview', |
||
| 272 | 'restore', |
||
| 273 | 'bulkRestore', |
||
| 274 | 'forceDelete', |
||
| 275 | 'bulkForceDelete', |
||
| 276 | 'bulkDelete', |
||
| 277 | 'restoreRevision', |
||
| 278 | 'duplicate', |
||
| 279 | ]; |
||
| 280 | |||
| 281 | if (isset($options['only'])) { |
||
| 282 | $customRoutes = array_intersect( |
||
| 283 | $defaults, |
||
| 284 | (array) $options['only'] |
||
| 285 | ); |
||
| 286 | } elseif (isset($options['except'])) { |
||
| 287 | $customRoutes = array_diff( |
||
| 288 | $defaults, |
||
| 289 | (array) $options['except'] |
||
| 290 | ); |
||
| 291 | } |
||
| 292 | |||
| 293 | $lastRouteGroupName = RouteServiceProvider::getLastRouteGroupName(); |
||
| 294 | |||
| 295 | $groupPrefix = RouteServiceProvider::getGroupPrefix(); |
||
| 296 | |||
| 297 | // Check if name will be a duplicate, and prevent if needed/allowed |
||
| 298 | if (RouteServiceProvider::shouldPrefixRouteName($groupPrefix, $lastRouteGroupName)) { |
||
| 299 | $customRoutePrefix = "{$groupPrefix}.{$slug}"; |
||
| 300 | $resourceCustomGroupPrefix = "{$groupPrefix}."; |
||
| 301 | } else { |
||
| 302 | $customRoutePrefix = $slug; |
||
| 303 | |||
| 304 | // Prevent Laravel from generating route names with duplication |
||
| 305 | $resourceCustomGroupPrefix = ''; |
||
| 306 | } |
||
| 307 | |||
| 308 | foreach ($customRoutes as $route) { |
||
| 309 | $routeSlug = "{$prefixSlug}/{$route}"; |
||
| 310 | $mapping = [ |
||
| 311 | 'as' => $customRoutePrefix . ".{$route}", |
||
| 312 | 'uses' => "{$className}Controller@{$route}", |
||
| 313 | ]; |
||
| 314 | |||
| 315 | if (in_array($route, ['browser', 'tags'])) { |
||
| 316 | Route::get($routeSlug, $mapping); |
||
| 317 | } |
||
| 318 | |||
| 319 | if (in_array($route, ['restoreRevision'])) { |
||
| 320 | Route::get($routeSlug . '/{id}', $mapping); |
||
| 321 | } |
||
| 322 | |||
| 323 | if ( |
||
| 324 | in_array($route, [ |
||
| 325 | 'publish', |
||
| 326 | 'feature', |
||
| 327 | 'restore', |
||
| 328 | 'forceDelete', |
||
| 329 | ]) |
||
| 330 | ) { |
||
| 331 | Route::put($routeSlug, $mapping); |
||
| 332 | } |
||
| 333 | |||
| 334 | if (in_array($route, ['duplicate'])) { |
||
| 335 | Route::put($routeSlug . '/{id}', $mapping); |
||
| 336 | } |
||
| 337 | |||
| 338 | if (in_array($route, ['preview'])) { |
||
| 339 | Route::put($routeSlug . '/{id}', $mapping); |
||
| 340 | } |
||
| 341 | |||
| 342 | if ( |
||
| 343 | in_array($route, [ |
||
| 344 | 'reorder', |
||
| 345 | 'bulkPublish', |
||
| 346 | 'bulkFeature', |
||
| 347 | 'bulkDelete', |
||
| 348 | 'bulkRestore', |
||
| 349 | 'bulkForceDelete', |
||
| 350 | ]) |
||
| 351 | ) { |
||
| 352 | Route::post($routeSlug, $mapping); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | if ($resource) { |
||
| 357 | Route::group( |
||
| 358 | ['as' => $resourceCustomGroupPrefix], |
||
| 359 | function () use ($slug, $className, $resource_options) { |
||
| 360 | Route::resource( |
||
| 361 | $slug, |
||
| 362 | "{$className}Controller", |
||
| 363 | $resource_options |
||
| 364 | ); |
||
| 365 | } |
||
| 366 | ); |
||
| 367 | } |
||
| 368 | }); |
||
| 369 | |||
| 370 | Route::macro('singleton', function ( |
||
| 371 | $slug, |
||
| 372 | $options = [], |
||
| 373 | $resource_options = [], |
||
| 374 | $resource = true |
||
| 375 | ) { |
||
| 376 | $pluralSlug = Str::plural($slug); |
||
| 377 | $modelName = Str::studly($slug); |
||
| 378 | |||
| 379 | Route::module($pluralSlug, $options, $resource_options, $resource); |
||
| 380 | |||
| 381 | $lastRouteGroupName = RouteServiceProvider::getLastRouteGroupName(); |
||
| 382 | |||
| 383 | $groupPrefix = RouteServiceProvider::getGroupPrefix(); |
||
| 384 | |||
| 385 | // Check if name will be a duplicate, and prevent if needed/allowed |
||
| 386 | if (RouteServiceProvider::shouldPrefixRouteName($groupPrefix, $lastRouteGroupName)) { |
||
| 387 | $singletonRouteName = "{$groupPrefix}.{$slug}"; |
||
| 388 | } else { |
||
| 389 | $singletonRouteName = $slug; |
||
| 390 | } |
||
| 391 | |||
| 392 | Route::get($slug, $modelName . 'Controller@editSingleton')->name($singletonRouteName); |
||
| 393 | }); |
||
| 433 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.