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:
| 1 | <?php |
||
| 13 | class PermissionServiceProvider extends ServiceProvider |
||
| 14 | { |
||
| 15 | public function boot(PermissionRegistrar $permissionLoader, Filesystem $filesystem) |
||
| 16 | { |
||
| 17 | if (function_exists('config_path')) { // function not available and 'publish' not relevant in Lumen |
||
| 18 | $this->publishes([ |
||
| 19 | __DIR__.'/../config/permission.php' => config_path('permission.php'), |
||
| 20 | ], 'config'); |
||
| 21 | |||
| 22 | $this->publishes([ |
||
| 23 | __DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->getMigrationFileName($filesystem), |
||
| 24 | ], 'migrations'); |
||
| 25 | } |
||
| 26 | |||
| 27 | $this->registerMacroHelpers(); |
||
| 28 | |||
| 29 | $this->commands([ |
||
| 30 | Commands\CacheReset::class, |
||
| 31 | Commands\CreateRole::class, |
||
| 32 | Commands\CreatePermission::class, |
||
| 33 | Commands\Show::class, |
||
| 34 | ]); |
||
| 35 | |||
| 36 | $this->registerModelBindings(); |
||
| 37 | |||
| 38 | $permissionLoader->clearClassPermissions(); |
||
| 39 | $permissionLoader->registerPermissions(); |
||
| 40 | |||
| 41 | $this->app->singleton(PermissionRegistrar::class, function ($app) use ($permissionLoader) { |
||
|
|
|||
| 42 | return $permissionLoader; |
||
| 43 | }); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function register() |
||
| 47 | { |
||
| 48 | $this->mergeConfigFrom( |
||
| 49 | __DIR__.'/../config/permission.php', |
||
| 50 | 'permission' |
||
| 51 | ); |
||
| 52 | |||
| 53 | $this->registerBladeExtensions(); |
||
| 54 | } |
||
| 55 | |||
| 56 | protected function registerModelBindings() |
||
| 57 | { |
||
| 58 | $config = $this->app->config['permission.models']; |
||
| 59 | |||
| 60 | if (! $config) { |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->app->bind(PermissionContract::class, $config['permission']); |
||
| 65 | $this->app->bind(RoleContract::class, $config['role']); |
||
| 66 | } |
||
| 67 | |||
| 68 | protected function registerBladeExtensions() |
||
| 122 | |||
| 123 | protected function registerMacroHelpers() |
||
| 124 | { |
||
| 125 | if (! method_exists(Route::class, 'macro')) { // Lumen |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns existing migration file if found, else uses the current timestamp. |
||
| 156 | * |
||
| 157 | * @param Filesystem $filesystem |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | protected function getMigrationFileName(Filesystem $filesystem): string |
||
| 170 | } |
||
| 171 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.