ChainDriver::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Core\Metadata\Driver;
12
13
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMapInterface;
14
use Cubiche\Core\Collections\ArrayCollection\ArrayList;
15
16
/**
17
 * ChainDriver class.
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class ChainDriver implements DriverInterface
22
{
23
    /**
24
     * @var ArrayHashMapInterface
25
     */
26
    protected $drivers;
27
28
    /**
29
     * The default driver.
30
     *
31
     * @var DriverInterface
32
     */
33
    protected $defaultDriver;
34
35
    /**
36
     * ChainDriver constructor.
37
     *
38
     * @param array                $drivers
39
     * @param DriverInterface|null $defaultDriver
40
     */
41
    public function __construct(array $drivers = array(), DriverInterface $defaultDriver = null)
42
    {
43
        $this->drivers = new ArrayList();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...yCollection\ArrayList() of type object<Cubiche\Core\Coll...ayCollection\ArrayList> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $drivers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
        $this->defaultDriver = $defaultDriver;
45
46
        foreach ($drivers as $driver) {
47
            $this->addDriver($driver);
48
        }
49
    }
50
51
    /**
52
     * @param DriverInterface $driver
53
     */
54
    public function addDriver(DriverInterface $driver)
55
    {
56
        $this->drivers->add($driver);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function loadMetadataForClass($className)
63
    {
64
        /** @var $driver DriverInterface */
65
        foreach ($this->drivers->toArray() as $driver) {
66
            if (null !== $metadata = $driver->loadMetadataForClass($className)) {
67
                return $metadata;
68
            }
69
        }
70
71
        if ($this->defaultDriver !== null) {
72
            return $this->defaultDriver->loadMetadataForClass($className);
73
        }
74
75
        return;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getAllClassNames()
82
    {
83
        $classNames = [];
84
        foreach ($this->drivers->toArray() as $driver) {
85
            $classNames = array_merge($classNames, $driver->getAllClassNames());
86
        }
87
88
        if ($this->defaultDriver !== null) {
89
            $classNames = array_merge($classNames, $this->defaultDriver->getAllClassNames());
90
        }
91
92
        return $classNames;
93
    }
94
}
95