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; |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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 theid
property of an instance of theAccount
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.