Completed
Pull Request — master (#165)
by Kevin
03:00
created

AbstractConfigurationReader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B introspectClass() 0 29 4
A recursivelyIntrospectInterface() 0 19 3
configure() 0 1 ?
1
<?php
2
3
namespace Magium\Util\Configuration;
4
5
abstract class AbstractConfigurationReader
6
{
7
8
    protected function introspectClass(ConfigurableObjectInterface $object)
9
    {
10
        $originalReflectionClass = $reflectionClass = new \ReflectionClass($object);
11
        $classes = [
12
            $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...
13
        ];
14
        while (($class = $reflectionClass->getParentClass()) !== false) {
15
            $classes[] = $class->getName();
16
            foreach ($class->getInterfaceNames() as $interface) {
17
                $reflectionInterface = new \ReflectionClass($interface);
18
                $interfaces = $reflectionInterface->getInterfaceNames();
19
                $classes = array_merge($interfaces, $classes);
20
            }
21
            $reflectionClass = $class;
22
        }
23
24
        foreach ($originalReflectionClass->getInterfaceNames() as $name){
25
            $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...
26
            $reflectionInterface = new \ReflectionClass($name);
27
            $theseInterfaces = $reflectionInterface->getInterfaceNames();
28
            $theseInterfaces = array_reverse($theseInterfaces);
29
30
            $interfaces = array_merge($theseInterfaces, $interfaces);
31
            $classes = array_merge($classes, $interfaces);
32
        }
33
        $classes = array_unique($classes);
34
        $classes = array_reverse($classes);
35
        return $classes;
36
    }
37
38
    protected function recursivelyIntrospectInterface($interfaceName)
39
    {
40
        $allInterfaces = [];
41
        $reflectionInterface = new \ReflectionClass($interfaceName);
42
        $interfaces = $reflectionInterface->getInterfaces();
43
        foreach ($interfaces as $interface) {
44
            $interfaceList = $this->recursivelyIntrospectInterface($interface);
45
            $allInterfaces = array_merge($allInterfaces, $interfaceList);
46
        }
47
48
        while (($parentInterface = $reflectionInterface->getParentClass()) != false) {
49
            $allInterfaces[] = $parentInterface->getName();
50
            $interfaceList = $this->recursivelyIntrospectInterface($parentInterface->getName());
51
            $allInterfaces = array_merge($allInterfaces, $interfaceList);
52
            $reflectionInterface = $parentInterface;
53
        }
54
55
        return $allInterfaces;
56
    }
57
58
    abstract public function configure(ConfigurableObjectInterface $object);
59
60
}
61