Completed
Push — pull-request/8600 ( 367a7c )
by Kamil
56:28 queued 34:06
created

getServicesToIgnoreDuringContainerCleanup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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 23 and the first side effect is on line 14.

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
declare(strict_types=1);
13
14
require_once __DIR__.'/AppKernel.php';
15
16
use ProxyManager\Proxy\VirtualProxyInterface;
17
use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
/**
21
 * @author Kamil Kokot <[email protected]>
22
 */
23
class TestAppKernel extends AppKernel
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function shutdown(): void
29
    {
30
        if (false === $this->booted) {
31
            return;
32
        }
33
34
        if (!in_array($this->getEnvironment(), ['test', 'test_cached'], true)) {
35
            parent::shutdown();
36
37
            return;
38
        }
39
40
        $container = $this->getContainer();
41
        parent::shutdown();
42
        $this->cleanupContainer($container);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->getContainer() on line 40 can be null; however, TestAppKernel::cleanupContainer() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
43
    }
44
45
    /**
46
     * Remove all container references from all loaded services
47
     *
48
     * @param ContainerInterface $container
49
     */
50
    protected function cleanupContainer(ContainerInterface $container): void
51
    {
52
        $containerReflection = new \ReflectionObject($container);
53
        $containerServicesPropertyReflection = $containerReflection->getProperty('services');
54
        $containerServicesPropertyReflection->setAccessible(true);
55
56
        $services = $containerServicesPropertyReflection->getValue($container) ?: [];
57
        foreach ($services as $serviceId => $service) {
58
            if (in_array($serviceId, $this->getServicesToIgnoreDuringContainerCleanup())) {
59
                continue;
60
            }
61
62
            $serviceReflection = new \ReflectionObject($service);
63
64
            if ($serviceReflection->implementsInterface(VirtualProxyInterface::class)) {
65
                continue;
66
            }
67
68
            $servicePropertiesReflections = $serviceReflection->getProperties();
69
            $servicePropertiesDefaultValues = $serviceReflection->getDefaultProperties();
70
            foreach ($servicePropertiesReflections as $servicePropertyReflection) {
71
                $defaultPropertyValue = null;
72
                if (isset($servicePropertiesDefaultValues[$servicePropertyReflection->getName()])) {
73
                    $defaultPropertyValue = $servicePropertiesDefaultValues[$servicePropertyReflection->getName()];
74
                }
75
76
                $servicePropertyReflection->setAccessible(true);
77
                $servicePropertyReflection->setValue($service, $defaultPropertyValue);
78
            }
79
        }
80
81
        $containerServicesPropertyReflection->setValue($container, null);
82
    }
83
84
    protected function getContainerBaseClass(): string
85
    {
86
        return MockerContainer::class;
87
    }
88
89
    protected function getServicesToIgnoreDuringContainerCleanup(): array
90
    {
91
        return [
92
            'kernel',
93
            'http_kernel',
94
            'liip_imagine.mime_type_guesser',
95
            'liip_imagine.extension_guesser',
96
        ];
97
    }
98
}
99