PsrContainerExtension::setContainerScope()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Roave\BehatPsrContainer;
5
6
use Behat\Testwork\ServiceContainer\Extension;
7
use Behat\Testwork\ServiceContainer\ExtensionManager;
8
use Psr\Container\ContainerInterface;
9
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
13
final class PsrContainerExtension implements Extension
14
{
15
    public function process(ContainerBuilder $container) : void
16
    {
17
    }
18
19 1
    public function getConfigKey() : string
20
    {
21 1
        return __NAMESPACE__;
22
    }
23
24
    public function initialize(ExtensionManager $extensionManager) : void
25
    {
26
    }
27
28 1
    public function configure(ArrayNodeDefinition $builder) : void
29
    {
30
        /** @noinspection NullPointerExceptionInspection */
31
        $builder
32 1
            ->children()
33 1
                ->scalarNode('container')->defaultValue('config/container.php')->end()
34 1
                ->scalarNode('name')->defaultValue('psr_container')->end()
35 1
            ->end()
36 1
        ->end();
37 1
    }
38
39 1
    public function load(ContainerBuilder $container, array $config) : void
40
    {
41 1
        $container->setParameter('roave.behat.psr.container.included.file', $config['container']);
42 1
        $container->setDefinition($config['name'], $this->createContainerDefinition());
43 1
    }
44
45 1
    private function createContainerDefinition() : Definition
46
    {
47 1
        $definition = new Definition(ContainerInterface::class, ['%roave.behat.psr.container.included.file%']);
48 1
        $definition->setFactory([ContainerFactory::class, 'createContainerFromIncludedFile']);
49 1
        $definition->addTag('helper_container.container');
50 1
        $definition->setPublic(true);
51
52 1
        $this->setContainerScope($definition);
53
54 1
        return $definition;
55
    }
56
57
    /**
58
     * The way to set service scope was improved across Symfony versions:
59
     *  - setShared was introduced in 2.8
60
     *  - setScope (and the constant) were removed in 3.0
61
     *
62
     * @param Definition $definition
63
     */
64 1
    private function setContainerScope(Definition $definition) : void
65
    {
66 1
        if (method_exists($definition, 'setShared')) {
67 1
            $definition->setShared(false);
68
        } else {
69
            $definition->setScope(ContainerBuilder::SCOPE_PROTOTYPE);
0 ignored issues
show
Bug introduced by
The method setScope() does not seem to exist on object<Symfony\Component...cyInjection\Definition>.

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...
70
        }
71 1
    }
72
}
73