Completed
Pull Request — master (#6)
by Tomáš
05:31
created

AbstractKernel   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 13

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 13
dl 0
loc 117
ccs 0
cts 69
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getKernelParameters() 0 10 2
A registerBundles() 0 19 2
A registerContainerConfiguration() 0 9 2
A boot() 0 10 2
A buildContainer() 0 17 3
A initializeContainer() 0 6 1
getAdditionalSculpinBundles() 0 1 ?
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Bundle\SculpinBundle\HttpKernel;
13
14
use Symplify\PHP7_Sculpin\Bundle\ContentTypesBundle\SculpinContentTypesBundle;
15
use Symplify\PHP7_Sculpin\Bundle\MarkdownBundle\SculpinMarkdownBundle;
16
use Symplify\PHP7_Sculpin\Bundle\MarkdownTwigCompatBundle\SculpinMarkdownTwigCompatBundle;
17
use Symplify\PHP7_Sculpin\Bundle\PaginationBundle\SculpinPaginationBundle;
18
use Symplify\PHP7_Sculpin\Bundle\SculpinBundle\SculpinBundle;
19
use Symplify\PHP7_Sculpin\Bundle\StandaloneBundle\SculpinStandaloneBundle;
20
use Symplify\PHP7_Sculpin\Bundle\TwigBundle\SculpinTwigBundle;
21
use Symfony\Component\Config\FileLocator;
22
use Symfony\Component\Config\Loader\LoaderInterface;
23
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
24
use Symfony\Component\HttpKernel\Kernel;
25
use Symplify\DefaultAutowire\SymplifyDefaultAutowireBundle;
26
27
abstract class AbstractKernel extends Kernel
28
{
29
    /**
30
     * @var string
31
     */
32
    private $projectDir;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function __construct(string $environment, string $debug, string $projectDir = null)
38
    {
39
        if ($projectDir) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $projectDir of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
40
            $this->projectDir = $projectDir;
41
            $this->rootDir = $projectDir.'/app';
42
        }
43
44
        parent::__construct($environment, $debug);
0 ignored issues
show
Documentation introduced by
$debug is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function getKernelParameters()
51
    {
52
        if (null === $this->projectDir) {
53
            $this->projectDir = dirname($this->rootDir);
54
        }
55
56
        return array_merge(parent::getKernelParameters(), [
57
            'sculpin.project_dir' => $this->projectDir,
58
        ]);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function registerBundles()
65
    {
66
        $bundles = [
67
            new SymplifyDefaultAutowireBundle(),
68
            new SculpinStandaloneBundle(),
69
            new SculpinMarkdownBundle(),
70
            new SculpinMarkdownTwigCompatBundle(),
71
            new SculpinPaginationBundle(),
72
            new SculpinBundle(),
73
            new SculpinTwigBundle(),
74
            new SculpinContentTypesBundle(),
75
        ];
76
77
        foreach ($this->getAdditionalSculpinBundles() as $class) {
78
            $bundles[] = new $class();
79
        }
80
81
        return $bundles;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function registerContainerConfiguration(LoaderInterface $loader)
88
    {
89
        // Load defaults.
90
        $loader->load(__DIR__.'/../Resources/config/kernel.yml');
91
92
        if (file_exists($file = $this->rootDir.'/config/sculpin_kernel.yml')) {
93
            $loader->load($file);
94
        }
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function boot()
101
    {
102
        if (true === $this->booted) {
103
            return;
104
        }
105
106
        parent::boot();
107
108
        $this->container->compile();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    protected function buildContainer()
115
    {
116
        $container = $this->getContainerBuilder();
117
        $container->addObjectResource($this);
118
        $this->prepareContainer($container);
119
120
        if (file_exists($this->rootDir.'/config/sculpin_services.yml')) {
121
            $loader = new YamlFileLoader($container, new FileLocator($this->rootDir.'/config'));
122
            $loader->load('sculpin_services.yml');
123
        }
124
125
        if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $cont is correct as $this->registerContainer...inerLoader($container)) (which targets Symplify\PHP7_Sculpin\Bu...ontainerConfiguration()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
126
            $container->merge($cont);
0 ignored issues
show
Documentation introduced by
$cont is of type null, but the function expects a object<Symfony\Component...ction\ContainerBuilder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
        }
128
129
        return $container;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    protected function initializeContainer()
136
    {
137
        $container = $this->buildContainer();
138
        $container->set('kernel', $this);
139
        $this->container = $container;
140
    }
141
142
    abstract protected function getAdditionalSculpinBundles() : array;
143
}
144