Completed
Push — master ( 7c6364...af049f )
by Kevin
08:48 queued 06:07
created

ClassConfigurationReader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 63
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 4 1
A __call() 0 4 1
A __construct() 0 6 1
A setConfigurationDir() 0 4 1
B configure() 0 28 6
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
        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
            $path = realpath(__DIR__ . '/../');
44
            $count = 0;
45
            while ($count++ < 10) {
46
                $filename = "{$path}/configuration";
47
                if (is_dir($filename)) {
48
                    $configurationDir = realpath($filename);
49
                    break;
50
                }
51
                $path .= '/../';
52
                $path = realpath($path); // More for debugging clarity.
53
            }
54
        }
55
56
        if (!$configurationDir) return;
0 ignored issues
show
Bug Best Practice introduced by
The expression $configurationDir of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
57
58
        $configurationFile = get_class($config) . '.php';
59
        $configurationFile = $configurationDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $configurationFile);
60
61
        if (file_exists($configurationFile)) {
62
            include $configurationFile;
63
        }
64
65
    }
66
67
}