Completed
Pull Request — master (#150)
by Kevin
06:26
created

ClassConfigurationReader::configure()   C

Complexity

Conditions 12
Paths 35

Size

Total Lines 58
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 6.5331
c 0
b 0
f 0
cc 12
eloc 38
nc 35
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Magium\Util\Configuration;
4
5
class ClassConfigurationReader
6
{
7
8
    protected $configurationDir;
9
    protected $object;
10
11
    public function __construct(
12
        $configurationDir = null
13
    )
14
    {
15
        $this->configurationDir = $configurationDir;
16
    }
17
18
    /**
19
     * @param null $configurationDir
20
     */
21
    public function setConfigurationDir($configurationDir)
22
    {
23
        $this->configurationDir = $configurationDir;
24
    }
25
26
27
    public function __set($name, $value)
28
    {
29
        $this->object->$name = $value;
30
    }
31
32
    public function __call($method, $args)
33
    {
34
        return call_user_func_array([$this->object, $method], $args);
35
    }
36
37
38
    public function configure(ConfigurableObjectInterface $config)
39
    {
40
        $this->object = $config;
41
        $configurationDir = $this->configurationDir;
42
        if ($configurationDir === null) {
43
            $testParts = explode(DIRECTORY_SEPARATOR, realpath(__DIR__.'/../../..'));
44
            $isVendor = array_pop($testParts) == 'vendor';
45
46
            if ($isVendor) {
47
                $path = realpath(__DIR__ . '/../../../../..'); // Get out of the Magium directories
48
            } else {
49
                $path = realpath(__DIR__ . '/../../..');
50
            }
51
52
            $count = 0;
53
            while ($count++ < 10) {
54
                $filename = "{$path}/configuration";
55
                $realpath = realpath($filename);
56
                if ($realpath !== false && is_dir($realpath)) {
57
                    // The equality check is due to case-insensitive file systems *ahem* Windows and OS X HFS+
58
                    $directories = glob(realpath($filename.'/../').'/*', GLOB_ONLYDIR);
59
                    foreach ($directories as $directory) {
60
                        $directory = realpath($directory);
61
                        $parts = explode(DIRECTORY_SEPARATOR, $directory);
62
                        $lastPart = array_pop($parts);
63
                        if ($lastPart == 'configuration') {
64
                            $configurationDir = $realpath;
65
                            break 2;
66
                        }
67
                    }
68
                }
69
                $path .= '/../';
70
                $path = realpath($path); // More for debugging clarity.
71
            }
72
        }
73
74
        if (!is_dir($configurationDir)) return;
75
76
        $reflectionClass = new \ReflectionClass($config);
77
        $classes = [
78
            $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...
79
        ];
80
        while (($class = $reflectionClass->getParentClass()) !== false) {
81
            $classes[] = $class->getName();
82
            $reflectionClass = $class;
83
        }
84
85
        $classes = array_reverse($classes);
86
87
        foreach ($classes as $class) {
88
            $configurationFile = $class . '.php';
89
            $configurationFile = $configurationDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $configurationFile);
90
91
            if (file_exists($configurationFile)) {
92
                include $configurationFile;
93
            }
94
        }
95
    }
96
97
}
98