CspServiceProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A boot() 0 18 3
1
<?php
2
3
namespace Spatie\Csp;
4
5
use Illuminate\Support\ServiceProvider;
6
use Spatie\Csp\Nonce\NonceGenerator;
7
8
class CspServiceProvider extends ServiceProvider
9
{
10
    public function boot()
11
    {
12
        if ($this->app->runningInConsole() && function_exists('config_path')) {
13
            $this->publishes([
14
                __DIR__.'/../config/csp.php' => config_path('csp.php'),
15
            ], 'config');
16
        }
17
18
        $this->app->singleton(NonceGenerator::class, config('csp.nonce_generator'));
19
20
        $this->app->singleton('csp-nonce', function () {
21
            return app(NonceGenerator::class)->generate();
22
        });
23
24
        $this->app->view->getEngineResolver()->resolve('blade')->getCompiler()->directive('nonce', function () {
0 ignored issues
show
Bug introduced by
Accessing view on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
25
            return '<?php echo "nonce=\"" . csp_nonce() . "\""; ?>';
26
        });
27
    }
28
29
    public function register()
30
    {
31
        $this->mergeConfigFrom(__DIR__.'/../config/csp.php', 'csp');
32
    }
33
}
34