InheritingConfigurationContainer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 4
crap 1
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