Configuration   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 22
lcom 2
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
D __construct() 0 26 9
A getPaths() 0 4 1
A setPaths() 0 5 1
A setEnvironment() 0 10 3
A removeEnvironment() 0 10 3
A getEnvironment() 0 4 1
A getDotenv() 0 4 1
A setDotenv() 0 6 1
A loadDotenv() 0 10 2
1
<?php
2
3
namespace Sinergi\Config;
4
5
use Dotenv\Dotenv;
6
use Sinergi\Config\Path\PathCollection;
7
8
class Configuration
9
{
10
    /**
11
     * @var array
12
     */
13
    public static $env;
14
15
    /**
16
     * @var PathCollection
17
     */
18
    protected $paths;
19
20
    /**
21
     * @var Dotenv
22
     */
23
    protected $dotenv;
24
25
    /**
26
     * @var string
27
     */
28
    protected $environment;
29
30
    /**
31
     * @param array|Configuration $config
32
     */
33
    public function __construct($config)
34
    {
35
        $this->paths = new PathCollection();
36
        if (is_array($config)) {
37
            if (isset($config['path'])) {
38
                $this->paths->add($config['path']);
39
            }
40
            if (isset($config['paths'])) {
41
                foreach ($config['paths'] as $path) {
42
                    $this->paths->add($path);
43
                }
44
            }
45
            if (isset($config['environment'])) {
46
                $this->setEnvironment($config['environment']);
47
            }
48
            if (isset($config['dotenv'])) {
49
                $this->setDotenv(new Dotenv($config['dotenv']));
50
            }
51
        } elseif ($config instanceof Configuration) {
52
            $this->setPaths($config->getPaths());
53
            $this->setEnvironment($config->getEnvironment());
54
            if ($config->getDotenv()) {
55
                $this->setDotenv($config->getDotenv());
56
            }
57
        }
58
    }
59
60
    /**
61
     * @return PathCollection
62
     */
63
    public function getPaths()
64
    {
65
        return $this->paths;
66
    }
67
68
    /**
69
     * @param PathCollection $pathCollection
70
     * @return $this
71
     */
72
    public function setPaths(PathCollection $pathCollection)
73
    {
74
        $this->paths = $pathCollection;
75
        return $this;
76
    }
77
78
    /**
79
     * @param null|string $environment
80
     * @return $this
81
     */
82
    public function setEnvironment($environment)
83
    {
84
        if ($this->environment !== $environment) {
85
            if (method_exists($this, 'reset')) {
86
                $this->reset();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Sinergi\Config\Configuration as the method reset() does only exist in the following sub-classes of Sinergi\Config\Configuration: Sinergi\Config\Collection. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
87
            }
88
        }
89
        $this->environment = $environment;
90
        return $this;
91
    }
92
93
    /**
94
     * @return $this
95
     */
96
    public function removeEnvironment()
97
    {
98
        if ($this->environment !== null) {
99
            if (method_exists($this, 'reset')) {
100
                $this->reset();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Sinergi\Config\Configuration as the method reset() does only exist in the following sub-classes of Sinergi\Config\Configuration: Sinergi\Config\Collection. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
101
            }
102
        }
103
        $this->environment = null;
104
        return $this;
105
    }
106
107
    /**
108
     * @return null|string
109
     */
110
    public function getEnvironment()
111
    {
112
        return $this->environment;
113
    }
114
115
    /**
116
     * @return Dotenv
117
     */
118
    public function getDotenv()
119
    {
120
        return $this->dotenv;
121
    }
122
123
    /**
124
     * @param Dotenv $dotenv
125
     * @return $this
126
     */
127
    public function setDotenv(Dotenv $dotenv)
128
    {
129
        $this->dotenv = $dotenv;
130
        $this->loadDotenv();
131
        return $this;
132
    }
133
134
    private function loadDotenv()
135
    {
136
        $retval = [];
137
        $values = $this->dotenv->load();
138
        foreach ($values as $value) {
139
            $parts = explode("=", $value, 2);
140
            $retval[$parts[0]] = getenv($parts[0]);
141
        }
142
        return self::$env = $retval;
143
    }
144
}
145