Completed
Push — master ( 6f1a19...e34473 )
by Vitaly
02:18
created

InjectArgument::buildFullClassName()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 3
eloc 4
nc 2
nop 2
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 07.08.16 at 15:46
5
 */
6
namespace samsonframework\container\annotation;
7
8
use samsonframework\container\metadata\MethodMetadata;
9
10
/**
11
 * Method argument injection annotation.
12
 *
13
 * @Annotation
14
 */
15
class InjectArgument extends CollectionValue implements MethodInterface
16
{
17
    /** @var string Method argument name */
18
    protected $argumentName;
19
20
    /** @var string Method argument type */
21
    protected $argumentType;
22
23
    /**
24
     * InjectArgument constructor.
25
     *
26
     * @param array $valueOrValues
27
     */
28
    public function __construct(array $valueOrValues)
29
    {
30
        parent::__construct($valueOrValues);
31
32
        // Set data
33
        foreach ($valueOrValues as $argumentName => $argumentType) {
34
            $this->argumentName = $argumentName;
0 ignored issues
show
Documentation Bug introduced by
It seems like $argumentName can also be of type integer. However, the property $argumentName is declared as type string. 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...
35
            $this->argumentType = $argumentType;
36
        }
37
    }
38
39
    /** {@inheritdoc} */
40
    public function toMethodMetadata(MethodMetadata $methodMetadata)
41
    {
42
        // Inject only @Inject with value
43
        if ($this->argumentName !== null && $this->argumentType !== null) {
44
            $this->argumentName = $this->argumentName;
45
            $this->argumentType = $this->buildFullClassName($this->argumentType, $methodMetadata->classMetadata->nameSpace);
46
47
            $methodMetadata->dependencies[$this->argumentName] = $this->argumentType;
48
        }
49
    }
50
51
    /**
52
     * Build full class name.
53
     *
54
     * @param string $className Full or short class name
55
     * @param string $namespace Name space
56
     *
57
     * @return string Full class name
58
     */
59 View Code Duplication
    protected function buildFullClassName($className, $namespace)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        // Check if we need to append namespace to dependency
62
        if ($className !== null && strpos($className, '\\') === false) {
63
            return $namespace . '\\' . $className;
64
        }
65
66
        return $className;
67
    }
68
}
69