Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Test Setup Failed
Pull Request — master (#2940)
by Cristian
14:26
created

mergeConfigFromOperationsDirectory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD;
4
5
use Backpack\CRUD\app\Http\Middleware\ThrottlePasswordRecovery;
6
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel;
7
use Illuminate\Routing\Router;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Facades\App;
10
use Illuminate\Support\Facades\Route;
11
use Illuminate\Support\ServiceProvider;
12
13
class BackpackServiceProvider extends ServiceProvider
14
{
15
    use Stats, LicenseCheck;
16
17
    protected $commands = [
18
        \Backpack\CRUD\app\Console\Commands\Install::class,
19
        \Backpack\CRUD\app\Console\Commands\AddSidebarContent::class,
20
        \Backpack\CRUD\app\Console\Commands\AddCustomRouteContent::class,
21
        \Backpack\CRUD\app\Console\Commands\Version::class,
22
        \Backpack\CRUD\app\Console\Commands\CreateUser::class,
23
        \Backpack\CRUD\app\Console\Commands\PublishBackpackMiddleware::class,
24
        \Backpack\CRUD\app\Console\Commands\PublishView::class,
25
        \Backpack\CRUD\app\Console\Commands\RequireDevTools::class,
26
    ];
27
28
    // Indicates if loading of the provider is deferred.
29
    protected $defer = false;
30
    // Where the route file lives, both inside the package and in the app (if overwritten).
31
    public $routeFilePath = '/routes/backpack/base.php';
32
    // Where custom routes can be written, and will be registered by Backpack.
33
    public $customRoutesFilePath = '/routes/backpack/custom.php';
34
35
    /**
36
     * Perform post-registration booting of services.
37
     *
38
     * @return void
39
     */
40
    public function boot(\Illuminate\Routing\Router $router)
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
    public function boot(/** @scrutinizer ignore-unused */ \Illuminate\Routing\Router $router)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        $this->loadViewsWithFallbacks();
43
        $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack');
44
        $this->loadConfigs();
45
        $this->registerMiddlewareGroup($this->app->router);
0 ignored issues
show
Bug introduced by
Accessing router on the interface Illuminate\Contracts\Fou...ion\CachesConfiguration suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing router on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
        $this->setupRoutes($this->app->router);
47
        $this->setupCustomRoutes($this->app->router);
0 ignored issues
show
Bug introduced by
Accessing router on the interface Illuminate\Contracts\Foundation\CachesRoutes suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
48
        $this->publishFiles();
49
        $this->checkLicenseCodeExists();
50
        $this->sendUsageStats();
51
    }
52
53
    /**
54
     * Register any package services.
55
     *
56
     * @return void
57
     */
58
    public function register()
59
    {
60
        // Bind the CrudPanel object to Laravel's service container
61
        $this->app->singleton('crud', function ($app) {
62
            return new CrudPanel($app);
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...rudPanel::__construct() has too many arguments starting with $app. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            return /** @scrutinizer ignore-call */ new CrudPanel($app);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
63
        });
64
65
        // Bind the widgets collection object to Laravel's service container
66
        $this->app->singleton('widgets', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

66
        $this->app->singleton('widgets', function (/** @scrutinizer ignore-unused */ $app) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
            return new Collection();
68
        });
69
70
        // load a macro for Route,
71
        // helps developers load all routes for a CRUD resource in one line
72
        if (! Route::hasMacro('crud')) {
73
            $this->addRouteMacro();
74
        }
75
76
        // register the helper functions
77
        $this->loadHelpers();
78
79
        // register the artisan commands
80
        $this->commands($this->commands);
81
    }
82
83
    public function registerMiddlewareGroup(Router $router)
84
    {
85
        $middleware_key = config('backpack.base.middleware_key');
86
        $middleware_class = config('backpack.base.middleware_class');
87
88
        if (! is_array($middleware_class)) {
89
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
90
91
            return;
92
        }
93
94
        foreach ($middleware_class as $middleware_class) {
95
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
96
        }
97
98
        // register internal backpack middleware for throttling the password recovery functionality
99
        // but only if functionality is enabled by developer in config
100
        if (config('backpack.base.setup_password_recovery_routes')) {
101
            $router->aliasMiddleware('backpack.throttle.password.recovery', ThrottlePasswordRecovery::class);
102
        }
103
    }
104
105
    public function publishFiles()
106
    {
107
        $error_views = [__DIR__.'/resources/error_views' => resource_path('views/errors')];
108
        $backpack_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack')];
109
        $backpack_public_assets = [__DIR__.'/public' => public_path()];
110
        $backpack_lang_files = [__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')];
111
        $backpack_config_files = [__DIR__.'/config' => config_path()];
112
113
        // sidebar content views, which are the only views most people need to overwrite
114
        $backpack_menu_contents_view = [
115
            __DIR__.'/resources/views/base/inc/sidebar_content.blade.php'      => resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php'),
116
            __DIR__.'/resources/views/base/inc/topbar_left_content.blade.php'  => resource_path('views/vendor/backpack/base/inc/topbar_left_content.blade.php'),
117
            __DIR__.'/resources/views/base/inc/topbar_right_content.blade.php' => resource_path('views/vendor/backpack/base/inc/topbar_right_content.blade.php'),
118
        ];
119
        $backpack_custom_routes_file = [__DIR__.$this->customRoutesFilePath => base_path($this->customRoutesFilePath)];
120
121
        // calculate the path from current directory to get the vendor path
122
        $vendorPath = dirname(__DIR__, 3);
123
        $gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()];
124
125
        // establish the minimum amount of files that need to be published, for Backpack to work; there are the files that will be published by the install command
126
        $minimum = array_merge(
127
            // $backpack_views,
128
            // $backpack_lang_files,
129
            $error_views,
130
            $backpack_public_assets,
131
            $backpack_config_files,
132
            $backpack_menu_contents_view,
133
            $backpack_custom_routes_file,
134
            $gravatar_assets
135
        );
136
137
        // register all possible publish commands and assign tags to each
138
        $this->publishes($backpack_config_files, 'config');
139
        $this->publishes($backpack_lang_files, 'lang');
140
        $this->publishes($backpack_views, 'views');
141
        $this->publishes($backpack_menu_contents_view, 'menu_contents');
142
        $this->publishes($error_views, 'errors');
143
        $this->publishes($backpack_public_assets, 'public');
144
        $this->publishes($backpack_custom_routes_file, 'custom_routes');
145
        $this->publishes($gravatar_assets, 'gravatar');
146
        $this->publishes($minimum, 'minimum');
147
    }
148
149
    /**
150
     * Define the routes for the application.
151
     *
152
     * @param  \Illuminate\Routing\Router  $router
153
     * @return void
154
     */
155
    public function setupRoutes(Router $router)
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

155
    public function setupRoutes(/** @scrutinizer ignore-unused */ Router $router)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
    {
157
        // by default, use the routes file provided in vendor
158
        $routeFilePathInUse = __DIR__.$this->routeFilePath;
159
160
        // but if there's a file with the same name in routes/backpack, use that one
161
        if (file_exists(base_path().$this->routeFilePath)) {
162
            $routeFilePathInUse = base_path().$this->routeFilePath;
163
        }
164
165
        $this->loadRoutesFrom($routeFilePathInUse);
166
    }
167
168
    /**
169
     * Load custom routes file.
170
     *
171
     * @param  \Illuminate\Routing\Router  $router
172
     * @return void
173
     */
174
    public function setupCustomRoutes(Router $router)
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

174
    public function setupCustomRoutes(/** @scrutinizer ignore-unused */ Router $router)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
    {
176
        // if the custom routes file is published, register its routes
177
        if (file_exists(base_path().$this->customRoutesFilePath)) {
178
            $this->loadRoutesFrom(base_path().$this->customRoutesFilePath);
179
        }
180
    }
181
182
    /**
183
     * The route macro allows developers to generate the routes for a CrudController,
184
     * for all operations, using a simple syntax: Route::crud().
185
     *
186
     * It will go to the given CrudController and get the setupRoutes() method on it.
187
     */
188
    private function addRouteMacro()
189
    {
190
        Route::macro('crud', function ($name, $controller) {
191
            // put together the route name prefix,
192
            // as passed to the Route::group() statements
193
            $routeName = '';
194
            if ($this->hasGroupStack()) {
0 ignored issues
show
Bug introduced by
The method hasGroupStack() does not exist on Backpack\CRUD\BackpackServiceProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

194
            if ($this->/** @scrutinizer ignore-call */ hasGroupStack()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
195
                foreach ($this->getGroupStack() as $key => $groupStack) {
0 ignored issues
show
Bug introduced by
The method getGroupStack() does not exist on Backpack\CRUD\BackpackServiceProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

195
                foreach ($this->/** @scrutinizer ignore-call */ getGroupStack() as $key => $groupStack) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
196
                    if (isset($groupStack['name'])) {
197
                        if (is_array($groupStack['name'])) {
198
                            $routeName = implode('', $groupStack['name']);
199
                        } else {
200
                            $routeName = $groupStack['name'];
201
                        }
202
                    }
203
                }
204
            }
205
            // add the name of the current entity to the route name prefix
206
            // the result will be the current route name (not ending in dot)
207
            $routeName .= $name;
208
209
            // get an instance of the controller
210
            if ($this->hasGroupStack()) {
211
                $groupStack = $this->getGroupStack();
212
                $groupNamespace = $groupStack && isset(end($groupStack)['namespace']) ? end($groupStack)['namespace'].'\\' : '';
213
            } else {
214
                $groupNamespace = '';
215
            }
216
            $namespacedController = $groupNamespace.$controller;
217
            $controllerInstance = App::make($namespacedController);
218
219
            return $controllerInstance->setupRoutes($name, $routeName, $controller);
220
        });
221
    }
222
223
    public function loadViewsWithFallbacks()
224
    {
225
        $customBaseFolder = resource_path('views/vendor/backpack/base');
226
        $customCrudFolder = resource_path('views/vendor/backpack/crud');
227
228
        // - first the published/overwritten views (in case they have any changes)
229
        if (file_exists($customBaseFolder)) {
230
            $this->loadViewsFrom($customBaseFolder, 'backpack');
231
        }
232
        if (file_exists($customCrudFolder)) {
233
            $this->loadViewsFrom($customCrudFolder, 'crud');
234
        }
235
        // - then the stock views that come with the package, in case a published view might be missing
236
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views/base'), 'backpack');
237
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views/crud'), 'crud');
238
    }
239
240
    protected function mergeConfigFromOperationsDirectory()
241
    {
242
        $operationConfigs = scandir(__DIR__.'/config/backpack/operations/');
243
        $operationConfigs = array_diff($operationConfigs, ['.', '..']);
244
245
        if (! count($operationConfigs)) {
246
            return;
247
        }
248
249
        foreach ($operationConfigs as $configFile) {
250
            $this->mergeConfigFrom(
251
                __DIR__.'/config/backpack/operations/'.$configFile,
252
                'backpack.operations.'.substr($configFile, 0, strrpos($configFile, '.'))
253
            );
254
        }
255
    }
256
257
    public function loadConfigs()
258
    {
259
        // use the vendor configuration file as fallback
260
        $this->mergeConfigFrom(__DIR__.'/config/backpack/crud.php', 'backpack.crud');
261
        $this->mergeConfigFrom(__DIR__.'/config/backpack/base.php', 'backpack.base');
262
        $this->mergeConfigFromOperationsDirectory();
263
264
        // add the root disk to filesystem configuration
265
        app()->config['filesystems.disks.'.config('backpack.base.root_disk_name')] = [
266
            'driver' => 'local',
267
            'root'   => base_path(),
268
        ];
269
270
        /*
271
         * Backpack login differs from the standard Laravel login.
272
         * As such, Backpack uses its own authentication provider, password broker and guard.
273
         *
274
         * THe process below adds those configuration values on top of whatever is in config/auth.php.
275
         * Developers can overwrite the backpack provider, password broker or guard by adding a
276
         * provider/broker/guard with the "backpack" name inside their config/auth.php file.
277
         * Or they can use another provider/broker/guard entirely, by changing the corresponding
278
         * value inside config/backpack/base.php
279
         */
280
281
        // add the backpack_users authentication provider to the configuration
282
        app()->config['auth.providers'] = app()->config['auth.providers'] +
283
        [
284
            'backpack' => [
285
                'driver'  => 'eloquent',
286
                'model'   => config('backpack.base.user_model_fqn'),
287
            ],
288
        ];
289
290
        // add the backpack_users password broker to the configuration
291
        app()->config['auth.passwords'] = app()->config['auth.passwords'] +
292
        [
293
            'backpack' => [
294
                'provider'  => 'backpack',
295
                'table'     => 'password_resets',
296
                'expire'   => 60,
297
                'throttle' => config('backpack.base.password_recovery_throttle_notifications'),
298
            ],
299
        ];
300
301
        // add the backpack_users guard to the configuration
302
        app()->config['auth.guards'] = app()->config['auth.guards'] +
303
        [
304
            'backpack' => [
305
                'driver'   => 'session',
306
                'provider' => 'backpack',
307
            ],
308
        ];
309
    }
310
311
    /**
312
     * Load the Backpack helper methods, for convenience.
313
     */
314
    public function loadHelpers()
315
    {
316
        require_once __DIR__.'/helpers.php';
317
    }
318
319
    /**
320
     * Get the services provided by the provider.
321
     *
322
     * @return array
323
     */
324
    public function provides()
325
    {
326
        return ['crud', 'widgets'];
327
    }
328
}
329