Completed
Pull Request — master (#3)
by Todd
01:29
created

Reference::resolve()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 10
cp 0.7
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 2
crap 4.432
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2016 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Container;
9
10
/**
11
 * A reference to another entry in a {@link Container}.
12
 */
13
class Reference implements ReferenceInterface {
14
    /**
15
     * @var array
16
     */
17
    private $name;
18
19
    /**
20
     * Construct a new instance of the {@link Reference} class.
21
     *
22
     * @param string|array $name The name of the reference.
23
     */
24 6
    public function __construct($name) {
25
        $this->setName($name);
26 6
    }
27
28
    /**
29
     * Get the name of the reference.
30
     *
31
     * @return string|array Returns the name of the reference.
32
     */
33
    public function getName() {
34
        return $this->name;
35
    }
36
37
    /**
38
     * Set the name of the reference.
39
     *
40
     * @param string|array $name The name of the reference.
41
     */
42 5
    public function setName($name) {
43 5
        $this->name = $name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $name can also be of type string. However, the property $name is declared as type array. 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...
44 5
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 5
    public function resolve(Container $container, $_ = null) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $_ is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
50 5
        if (empty($this->name)) {
51 1
            return null;
52
        } elseif (is_string($this->name)) {
53
            return $container->get($this->name);
54
        } else {
55 3
            $result = $container;
56 3
            foreach ($this->name as $name) {
57
                $result = $result->get($name);
58
            }
59 3
            return $result;
60
        }
61 5
    }
62
}
63