FriendsOfBehatSymfonyExtensionExtension   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 111
Duplicated Lines 27.03 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 30
loc 111
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A registerBehatContainer() 0 8 1
A load() 0 8 1
A process() 0 11 2
A registerDriverBehatContainer() 0 20 1
A provideBrowserKitIntegration() 0 13 3
A provideMinkIntegration() 0 10 2
A registerMink() 10 10 1
A registerMinkDefaultSession() 10 10 1
A registerMinkParameters() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace FriendsOfBehat\SymfonyExtension\Bundle\DependencyInjection;
6
7
use Behat\Behat\Context\Context;
8
use Behat\Mink\Mink;
9
use Behat\Mink\Session;
10
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
11
use FriendsOfBehat\SymfonyExtension\ServiceContainer\SymfonyExtension;
12
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
13
use Symfony\Component\BrowserKit\Client;
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
use Symfony\Component\HttpKernel\HttpKernelBrowser;
21
use Symfony\Component\HttpKernel\KernelInterface;
22
23
final class FriendsOfBehatSymfonyExtensionExtension extends Extension implements CompilerPassInterface
24
{
25
    public function load(array $configs, ContainerBuilder $container): void
26
    {
27
        $this->provideMinkIntegration($container);
28
        $this->registerBehatContainer($container);
29
        $this->registerDriverBehatContainer($container);
30
31
        $container->registerForAutoconfiguration(Context::class)->addTag('fob.context');
32
    }
33
34
    public function process(ContainerBuilder $container): void
35
    {
36
        $this->provideBrowserKitIntegration($container);
37
38
        foreach ($container->findTaggedServiceIds('fob.context') as $serviceId => $attributes) {
39
            $serviceDefinition = $container->findDefinition($serviceId);
40
41
            $serviceDefinition->setPublic(true);
42
            $serviceDefinition->clearTag('fob.context');
43
        }
44
    }
45
46
    private function registerBehatContainer(ContainerBuilder $container): void
47
    {
48
        $behatServiceContainerDefinition = new Definition(ContainerInterface::class);
49
        $behatServiceContainerDefinition->setPublic(true);
50
        $behatServiceContainerDefinition->setSynthetic(true);
51
52
        $container->setDefinition('behat.service_container', $behatServiceContainerDefinition);
53
    }
54
55
    private function registerDriverBehatContainer(ContainerBuilder $container): void
56
    {
57
        $driverKernelDefinition = new Definition(KernelInterface::class, [SymfonyExtension::DRIVER_KERNEL_ID]);
58
        $driverKernelDefinition->setFactory([new Reference('behat.service_container'), 'get']);
59
        $driverKernelDefinition->setPublic(true);
60
        $driverKernelDefinition->setLazy(true);
61
62
        $driverServiceContainerDefinition = new Definition(ContainerInterface::class);
63
        $driverServiceContainerDefinition->setFactory([$driverKernelDefinition, 'getContainer']);
64
        $driverServiceContainerDefinition->setPublic(true);
65
        $driverServiceContainerDefinition->setLazy(true);
66
67
        $driverTestServiceContainerDefinition = new Definition(ContainerInterface::class, ['test.service_container']);
68
        $driverTestServiceContainerDefinition->setFactory([$driverServiceContainerDefinition, 'get']);
69
        $driverTestServiceContainerDefinition->setPublic(true);
70
        $driverTestServiceContainerDefinition->setLazy(true);
71
72
        $container->setDefinition('behat.driver.service_container', $driverTestServiceContainerDefinition);
73
        $container->registerAliasForArgument('behat.driver.service_container', ContainerInterface::class, 'driver container');
74
    }
75
76
    private function provideBrowserKitIntegration(ContainerBuilder $container): void
77
    {
78
        if (!$container->has('test.client')) {
79
            return;
80
        }
81
82
        if (class_exists(Client::class)) {
83
            $container->setAlias(Client::class, 'test.client');
84
        }
85
86
        $container->setAlias(KernelBrowser::class, 'test.client');
87
        $container->setAlias(HttpKernelBrowser::class, 'test.client');
88
    }
89
90
    private function provideMinkIntegration(ContainerBuilder $container): void
91
    {
92
        if (!class_exists(Mink::class)) {
93
            return;
94
        }
95
96
        $this->registerMink($container);
97
        $this->registerMinkDefaultSession($container);
98
        $this->registerMinkParameters($container);
99
    }
100
101 View Code Duplication
    private function registerMink(ContainerBuilder $container): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        $minkDefinition = new Definition(Mink::class, ['fob_symfony.mink']);
104
        $minkDefinition->setPublic(true);
105
        $minkDefinition->setLazy(true);
106
        $minkDefinition->setFactory([new Reference('behat.service_container'), 'get']);
107
108
        $container->setDefinition('behat.mink', $minkDefinition);
109
        $container->setAlias(Mink::class, 'behat.mink');
110
    }
111
112 View Code Duplication
    private function registerMinkDefaultSession(ContainerBuilder $container): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        $minkDefaultSessionDefinition = new Definition(Session::class);
115
        $minkDefaultSessionDefinition->setPublic(true);
116
        $minkDefaultSessionDefinition->setLazy(true);
117
        $minkDefaultSessionDefinition->setFactory([new Reference('behat.mink'), 'getSession']);
118
119
        $container->setDefinition('behat.mink.default_session', $minkDefaultSessionDefinition);
120
        $container->setAlias(Session::class, 'behat.mink.default_session');
121
    }
122
123 View Code Duplication
    private function registerMinkParameters(ContainerBuilder $container): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $minkParametersDefinition = new Definition(MinkParameters::class, ['fob_symfony.mink.parameters']);
126
        $minkParametersDefinition->setPublic(true);
127
        $minkParametersDefinition->setLazy(true);
128
        $minkParametersDefinition->setFactory([new Reference('behat.service_container'), 'get']);
129
130
        $container->setDefinition('behat.mink.parameters', $minkParametersDefinition);
131
        $container->setAlias(MinkParameters::class, 'behat.mink.parameters');
132
    }
133
}
134