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

Passed
Pull Request — master (#3950)
by Cristian
13:58
created

skipConvertingEmptyStringsToNull()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 10
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
    /**
224
     * Add a rule to ConvertEmptyStringsToNull to not run itself for a specific route.
225
     * Used to be used inside CreateOperation and UpdateOperation, but can be used
226
     * by any other operation too, if they need it.
227
     *
228
     * @param  \Illuminate\Routing\Route  $route
229
     * @return void
230
     */
231
    public function skipConvertingEmptyStringsToNull($route)
232
    {
233
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::skipWhen(
234
            function ($request) use ($route) {
235
                // Unfortunately inside this closure we don't have access to the current route,
236
                // both request()->route() and Route::getCurrent() will return null,
237
                // so we have to rely on manual matching
238
                $routeUriMatches = $request->is(str_replace('{id}', '*', $route->uri));
239
                $routeMethodMatches = in_array($request->method(), $route->methods);
240
241
                return ($routeUriMatches && $routeMethodMatches) ? true : false;
242
            }
243
        );
244
    }
245
246
    public function loadViewsWithFallbacks()
247
    {
248
        $customBaseFolder = resource_path('views/vendor/backpack/base');
249
        $customCrudFolder = resource_path('views/vendor/backpack/crud');
250
251
        // - first the published/overwritten views (in case they have any changes)
252
        if (file_exists($customBaseFolder)) {
253
            $this->loadViewsFrom($customBaseFolder, 'backpack');
254
        }
255
        if (file_exists($customCrudFolder)) {
256
            $this->loadViewsFrom($customCrudFolder, 'crud');
257
        }
258
        // - then the stock views that come with the package, in case a published view might be missing
259
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views/base'), 'backpack');
260
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views/crud'), 'crud');
261
    }
262
263
    public function loadConfigs()
264
    {
265
        // use the vendor configuration file as fallback
266
        $this->mergeConfigFrom(__DIR__.'/config/backpack/crud.php', 'backpack.crud');
267
        $this->mergeConfigFrom(__DIR__.'/config/backpack/base.php', 'backpack.base');
268
269
        // add the root disk to filesystem configuration
270
        app()->config['filesystems.disks.'.config('backpack.base.root_disk_name')] = [
271
            'driver' => 'local',
272
            'root'   => base_path(),
273
        ];
274
275
        /*
276
         * Backpack login differs from the standard Laravel login.
277
         * As such, Backpack uses its own authentication provider, password broker and guard.
278
         *
279
         * THe process below adds those configuration values on top of whatever is in config/auth.php.
280
         * Developers can overwrite the backpack provider, password broker or guard by adding a
281
         * provider/broker/guard with the "backpack" name inside their config/auth.php file.
282
         * Or they can use another provider/broker/guard entirely, by changing the corresponding
283
         * value inside config/backpack/base.php
284
         */
285
286
        // add the backpack_users authentication provider to the configuration
287
        app()->config['auth.providers'] = app()->config['auth.providers'] +
288
        [
289
            'backpack' => [
290
                'driver'  => 'eloquent',
291
                'model'   => config('backpack.base.user_model_fqn'),
292
            ],
293
        ];
294
295
        // add the backpack_users password broker to the configuration
296
        app()->config['auth.passwords'] = app()->config['auth.passwords'] +
297
        [
298
            'backpack' => [
299
                'provider'  => 'backpack',
300
                'table'     => 'password_resets',
301
                'expire'   => 60,
302
                'throttle' => config('backpack.base.password_recovery_throttle_notifications'),
303
            ],
304
        ];
305
306
        // add the backpack_users guard to the configuration
307
        app()->config['auth.guards'] = app()->config['auth.guards'] +
308
        [
309
            'backpack' => [
310
                'driver'   => 'session',
311
                'provider' => 'backpack',
312
            ],
313
        ];
314
    }
315
316
    /**
317
     * Load the Backpack helper methods, for convenience.
318
     */
319
    public function loadHelpers()
320
    {
321
        require_once __DIR__.'/helpers.php';
322
    }
323
324
    /**
325
     * Get the services provided by the provider.
326
     *
327
     * @return array
328
     */
329
    public function provides()
330
    {
331
        return ['crud', 'widgets'];
332
    }
333
}
334