InheritingConfigurationContainer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 6
c 6
b 2
f 0
lcom 1
cbo 1
dl 0
loc 53
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getValueOrDefault() 0 8 2
A getValueOrFail() 0 12 3
1
<?php
2
3
namespace Onigoetz\Deployer\Configuration\Containers;
4
5
use Onigoetz\Deployer\Configuration\ConfigurationManager;
6
7
abstract class InheritingConfigurationContainer extends ConfigurationContainer
8
{
9
    /**
10
     * @var InheritingConfigurationContainer
11
     */
12
    protected $parent;
13
14 207
    public function __construct($name, array $data, ConfigurationManager $manager, self $parent = null)
15
    {
16 207
        parent::__construct($name, $data, $manager);
17
18 207
        $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent can also be of type object<self>. However, the property $parent is declared as type object<Onigoetz\Deployer...ConfigurationContainer>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
19 207
    }
20
21
    /**
22
     * Get the value or throw an exception
23
     * Will check the parent before the exception is thrown
24
     *
25
     * @param $key
26
     * @param $errorMessage
27
     * @throws \LogicException
28
     * @return mixed
29
     */
30 174
    protected function getValueOrFail($key, $errorMessage)
31
    {
32 174
        if (array_key_exists($key, $this->data)) {
33 126
            return $this->data[$key];
34
        }
35
36 99
        if ($this->parent) {
37 39
            return $this->parent->getValueOrFail($key, $errorMessage);
38
        }
39
40 75
        throw new \LogicException($errorMessage);
41
    }
42
43
    /**
44
     * Get the value or return the default
45
     * Will check the parent before the default is returned
46
     *
47
     * @param $key
48
     * @param $default
49
     * @return mixed
50
     */
51 102
    protected function getValueOrDefault($key, $default)
52
    {
53
        try {
54 102
            return $this->getValueOrFail($key, '');
55 51
        } catch (\LogicException $e) {
56 51
            return $default;
57
        }
58
    }
59
}
60