DriverLocator::getInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Bex\Behat\ExtensionDriverLocator;
4
5
use Bex\Behat\ExtensionDriverLocator\DriverClassNameResolver;
6
use Bex\Behat\ExtensionDriverLocator\DriverClassValidator;
7
use Bex\Behat\ExtensionDriverLocator\DriverInterface;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\NodeInterface;
10
use Symfony\Component\Config\Definition\Processor;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
13
class DriverLocator
14
{
15
    /**
16
     * @var DriverClassNameResolver
17
     */
18
    private $classNameResolver;
19
20
    /**
21
     * @var DriverInterface[]
22
     */
23
    private $drivers = [];
24
25
    /**
26
     * @param DriverClassNameResolver $classNameResolver
27
     */
28
    public function __construct(DriverClassNameResolver $classNameResolver)
29
    {
30
        $this->classNameResolver = $classNameResolver;
31
    }
32
33
    /**
34
     * @param  string $namespace
35
     * @param  string $parent
36
     *
37
     * @return Locator
38
     */
39
    public static function getInstance($namespace, $parent = '')
40
    {
41
        return new self(new DriverClassNameResolver($namespace, new DriverClassValidator($parent)));
42
    }
43
44
    /**
45
     * @param  ContainerBuilder $container
46
     * @param  array            $activeDrivers
47
     * @param  array            $driverConfigs
48
     *
49
     * @return DriverInterface[]
50
     */
51
    public function findDrivers(ContainerBuilder $container, array $activeDrivers, array $driverConfigs)
52
    {
53
        $driverConfigs = $this->removeUnusedDrivers($activeDrivers, $driverConfigs);
54
        $this->createDrivers($activeDrivers);
55
        $configTree = $this->configureDrivers();
56
        $driverConfigs = $this->processDriverConfiguration($configTree, $driverConfigs);
57
        $this->loadDrivers($container, $driverConfigs);
58
59
        return $this->drivers;
60
    }
61
62
    /**
63
     * @return DriverInterface[]
64
     */
65
    public function getDrivers()
66
    {
67
        return $this->drivers;
68
    }
69
70
    /**
71
     * @param  array $activeDrivers
72
     * @param  array $driverConfigs
73
     *
74
     * @return array
75
     */
76
    private function removeUnusedDrivers($activeDrivers, $driverConfigs)
77
    {
78
        foreach ($driverConfigs as $driverKey => $driverConfig) {
79
            if (!in_array($driverKey, $activeDrivers)) {
80
                unset($driverConfigs[$driverKey]);
81
            }
82
        }
83
84
        return $driverConfigs;
85
    }
86
87
    /**
88
     * @param array $driverKeys 
89
     *
90
     * @return DriverInterface[]
91
     */
92
    private function createDrivers($driverKeys)
93
    {
94
        $this->drivers = [];
95
96
        foreach ($driverKeys as $driverKey) {
97
            $driverClass = $this->classNameResolver->getClassNameByDriverKey($driverKey);
98
            $this->drivers[$driverKey] = new $driverClass();
99
        }
100
101
        return $this->drivers;
102
    }
103
104
    /**
105
     * @return NodeInterface
106
     */
107
    private function configureDrivers()
108
    {
109
        $tree = new TreeBuilder('drivers');
110
111
        foreach ($this->drivers as $driverKey => $driver) {
112
            $driver->configure($tree->getRootNode()->children()->arrayNode($driverKey));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
113
        }
114
115
        return $tree->buildTree();
116
    }
117
118
    /**
119
     * @param  NodeInterface $configTree
120
     * @param  array         $configs
121
     *
122
     * @return array The processed configuration
123
     */
124
    private function processDriverConfiguration(NodeInterface $configTree, array $configs)
125
    {
126
        $configProcessor = new Processor();
127
128
        foreach ($this->drivers as $driverKey => $driver) {
129
            $configs[$driverKey] = isset($configs[$driverKey]) ? $configs[$driverKey] : [];
130
        }
131
132
        return $configProcessor->process($configTree, ['drivers' => $configs]);
133
    }
134
135
    /**
136
     * @param  ContainerBuilder $container
137
     * @param  array            $driverConfigs
138
     *
139
     * @return DriverInterface[]
140
     */
141
    private function loadDrivers(ContainerBuilder $container, array $driverConfigs)
142
    {
143
        foreach ($this->drivers as $driverKey => $driver) {
144
            $driver->load($container, $driverConfigs[$driverKey]);
145
        }
146
147
        return $this->drivers;
148
    }
149
}