Completed
Push — master ( 198175...8777d2 )
by Kevin
04:54 queued 02:32
created

AbstractConfigurationReader::configure()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Magium\Util\Configuration;
4
5
abstract class AbstractConfigurationReader
6
{
7
8
    protected $classes = [];
9
10
    protected function introspectClass(ConfigurableObjectInterface $object)
11
    {
12
        $originalReflectionClass = $reflectionClass = new \ReflectionClass($object);
13
        $this->addClass($reflectionClass->getName());
0 ignored issues
show
Bug introduced by
Consider using $reflectionClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
14
        while (($class = $reflectionClass->getParentClass()) !== false) {
15
            $this->addClass($class->getName());
16
            foreach ($class->getInterfaceNames() as $interface) {
17
                $this->addClass($interface);
18
                $reflectionInterface = new \ReflectionClass($interface);
19
                $interfaces = $reflectionInterface->getInterfaceNames();
20
                foreach ($interfaces as $interface) {
21
                    $this->addClass($interface);
22
                }
23
            }
24
            $reflectionClass = $class;
25
        }
26
27
        foreach ($originalReflectionClass->getInterfaceNames() as $name){
28
            $this->addClass($name);
29
            $interfaces[] = $name;
0 ignored issues
show
Bug introduced by
The variable $interfaces does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
30
            $reflectionInterface = new \ReflectionClass($name);
31
            $theseInterfaces = $reflectionInterface->getInterfaceNames();
32
            $theseInterfaces = array_reverse($theseInterfaces);
33
            foreach ($theseInterfaces as $interface) {
34
                $this->addClass($interface);
35
            }
36
        }
37
38
        $this->classes = array_reverse($this->classes);
39
        return $this->classes;
40
    }
41
42
    protected function addClass($name)
43
    {
44
        if (!in_array($name, $this->classes)) {
45
            $this->classes[] = $name;
46
        }
47
    }
48
49
    protected function recursivelyIntrospectInterface($interfaceName)
50
    {
51
        $allInterfaces = [];
52
        $reflectionInterface = new \ReflectionClass($interfaceName);
53
        $interfaces = $reflectionInterface->getInterfaces();
54
        foreach ($interfaces as $interface) {
55
            $interfaceList = $this->recursivelyIntrospectInterface($interface);
56
            $allInterfaces = array_merge($allInterfaces, $interfaceList);
57
        }
58
59
        while (($parentInterface = $reflectionInterface->getParentClass()) != false) {
60
            $allInterfaces[] = $parentInterface->getName();
61
            $interfaceList = $this->recursivelyIntrospectInterface($parentInterface->getName());
62
            $allInterfaces = array_merge($allInterfaces, $interfaceList);
63
            $reflectionInterface = $parentInterface;
64
        }
65
66
        return $allInterfaces;
67
    }
68
69
    abstract public function configure(ConfigurableObjectInterface $object);
70
71
}
72