AbstractConfigurationReader::addClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 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
        $this->classes = [];
13
        $originalReflectionClass = $reflectionClass = new \ReflectionClass($object);
14
        $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...
15
        while (($class = $reflectionClass->getParentClass()) !== false) {
16
            $this->addClass($class->getName());
17
            foreach ($class->getInterfaceNames() as $interface) {
18
                $this->addClass($interface);
19
                $reflectionInterface = new \ReflectionClass($interface);
20
                $interfaces = $reflectionInterface->getInterfaceNames();
21
                foreach ($interfaces as $interface) {
22
                    $this->addClass($interface);
23
                }
24
            }
25
            $reflectionClass = $class;
26
        }
27
28
        foreach ($originalReflectionClass->getInterfaceNames() as $name){
29
            $this->addClass($name);
30
            $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...
31
            $reflectionInterface = new \ReflectionClass($name);
32
            $theseInterfaces = $reflectionInterface->getInterfaceNames();
33
            $theseInterfaces = array_reverse($theseInterfaces);
34
            foreach ($theseInterfaces as $interface) {
35
                $this->addClass($interface);
36
            }
37
        }
38
39
        $this->classes = array_reverse($this->classes);
40
        return $this->classes;
41
    }
42
43
    protected function addClass($name)
44
    {
45
        if (!in_array($name, $this->classes)) {
46
            $this->classes[] = $name;
47
        }
48
    }
49
50
    protected function recursivelyIntrospectInterface($interfaceName)
51
    {
52
        $allInterfaces = [];
53
        $reflectionInterface = new \ReflectionClass($interfaceName);
54
        $interfaces = $reflectionInterface->getInterfaces();
55
        foreach ($interfaces as $interface) {
56
            $interfaceList = $this->recursivelyIntrospectInterface($interface);
57
            $allInterfaces = array_merge($allInterfaces, $interfaceList);
58
        }
59
60
        while (($parentInterface = $reflectionInterface->getParentClass()) != false) {
61
            $allInterfaces[] = $parentInterface->getName();
62
            $interfaceList = $this->recursivelyIntrospectInterface($parentInterface->getName());
63
            $allInterfaces = array_merge($allInterfaces, $interfaceList);
64
            $reflectionInterface = $parentInterface;
65
        }
66
67
        return $allInterfaces;
68
    }
69
70
    abstract public function configure(ConfigurableObjectInterface $object);
71
72
}
73