Completed
Push — master ( 6f2f7b...0abd1b )
by Kamil
16s
created

TestAppKernel::cleanupContainer()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 10
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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
require_once __DIR__.'/AppKernel.php';
13
14
use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
/**
18
 * @author Kamil Kokot <[email protected]>
19
 */
20
class TestAppKernel extends AppKernel
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function shutdown()
26
    {
27
        if (false === $this->booted) {
28
            return;
29
        }
30
31
        if (!in_array($this->environment, ['test', 'test_cached'], true)) {
32
            parent::shutdown();
33
34
            return;
35
        }
36
37
        $container = $this->container;
38
        parent::shutdown();
39
        $this->cleanupContainer($container);
0 ignored issues
show
Documentation introduced by
$container is of type null|object, but the function expects a object<Symfony\Component...ion\ContainerInterface>.

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...
40
    }
41
42
    /**
43
     * Remove all container references from all loaded services
44
     *
45
     * @param ContainerInterface $container
46
     */
47
    protected function cleanupContainer(ContainerInterface $container)
48
    {
49
        $containerReflection = new \ReflectionObject($container);
50
        $containerServicesPropertyReflection = $containerReflection->getProperty('services');
51
        $containerServicesPropertyReflection->setAccessible(true);
52
53
        $services = $containerServicesPropertyReflection->getValue($container) ?: [];
54
        foreach ($services as $serviceId => $service) {
55
            if ('kernel' === $serviceId || 'http_kernel' === $serviceId) {
56
                continue;
57
            }
58
59
            $serviceReflection = new \ReflectionObject($service);
60
61
            $servicePropertiesReflections = $serviceReflection->getProperties();
62
            $servicePropertiesDefaultValues = $serviceReflection->getDefaultProperties();
63
            foreach ($servicePropertiesReflections as $servicePropertyReflection) {
64
                $defaultPropertyValue = null;
65
                if (isset($servicePropertiesDefaultValues[$servicePropertyReflection->getName()])) {
66
                    $defaultPropertyValue = $servicePropertiesDefaultValues[$servicePropertyReflection->getName()];
67
                }
68
69
                $servicePropertyReflection->setAccessible(true);
70
                $servicePropertyReflection->setValue($service, $defaultPropertyValue);
71
            }
72
        }
73
74
        $containerServicesPropertyReflection->setValue($container, null);
75
    }
76
77
    protected function getContainerBaseClass()
78
    {
79
        return MockerContainer::class;
80
    }
81
}
82