1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
6
|
|
|
* on 07.08.16 at 15:46 |
7
|
|
|
*/ |
8
|
|
|
namespace samsonframework\container\annotation; |
9
|
|
|
|
10
|
|
|
use samsonframework\container\metadata\MethodMetadata; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Method argument injection annotation. |
14
|
|
|
* |
15
|
|
|
* @Annotation |
16
|
|
|
*/ |
17
|
|
|
class InjectArgument extends CollectionValue implements MethodInterface |
18
|
|
|
{ |
19
|
|
|
/** @var string Method argument name */ |
20
|
|
|
protected $argumentName; |
21
|
|
|
|
22
|
|
|
/** @var string Method argument type */ |
23
|
|
|
protected $argumentType; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* InjectArgument constructor. |
27
|
|
|
* |
28
|
|
|
* @param array $valueOrValues |
29
|
|
|
* |
30
|
|
|
* @throws \InvalidArgumentException |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct(array $valueOrValues) |
33
|
|
|
{ |
34
|
4 |
|
parent::__construct($valueOrValues); |
35
|
|
|
|
36
|
4 |
|
if (count($valueOrValues)) { |
37
|
4 |
|
list($this->argumentName, $this->argumentType) = each($valueOrValues); |
38
|
|
|
} |
39
|
4 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
* |
44
|
|
|
* @throws \InvalidArgumentException |
45
|
|
|
*/ |
46
|
4 |
|
public function toMethodMetadata(MethodMetadata $methodMetadata) |
47
|
|
|
{ |
48
|
|
|
// Check for argument name input and validity |
49
|
4 |
View Code Duplication |
if (!$this->checkArgumentExists($this->argumentName, $methodMetadata)) { |
|
|
|
|
50
|
1 |
|
throw new \InvalidArgumentException( |
51
|
|
|
'@InjectArgument argument "' |
52
|
1 |
|
. $methodMetadata->classMetadata->className . '::' |
53
|
1 |
|
. $methodMetadata->name . ' ' |
54
|
1 |
|
. $this->argumentName . '" does not exists' |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Check for type input |
59
|
3 |
View Code Duplication |
if ($this->argumentType === null) { |
|
|
|
|
60
|
1 |
|
throw new \InvalidArgumentException( |
61
|
|
|
'@InjectArgument argument "' |
62
|
1 |
|
. $methodMetadata->classMetadata->className . '::' |
63
|
1 |
|
. $methodMetadata->name . ' ' |
64
|
1 |
|
. $this->argumentName . '" type not specified' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Store dependency with fully qualified type name |
69
|
2 |
|
$methodMetadata->dependencies[$this->argumentName] = $this->buildFullClassName( |
70
|
2 |
|
$this->argumentType, |
71
|
2 |
|
$methodMetadata->classMetadata->nameSpace |
72
|
|
|
); |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check method argument existance. |
77
|
|
|
* |
78
|
|
|
* @param string $argument |
79
|
|
|
* @param MethodMetadata $methodMetadata |
80
|
|
|
* |
81
|
|
|
* @return bool True if @InjectArgument argument name is valid |
82
|
|
|
*/ |
83
|
4 |
|
protected function checkArgumentExists(string $argument, MethodMetadata $methodMetadata) : bool |
84
|
|
|
{ |
85
|
4 |
|
return $argument !== null && array_key_exists($argument, $methodMetadata->parametersMetadata); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Build full class name. |
90
|
|
|
* |
91
|
|
|
* @param string $className Full or short class name |
92
|
|
|
* @param string $namespace Name space |
93
|
|
|
* |
94
|
|
|
* @return string Full class name |
95
|
|
|
*/ |
96
|
2 |
View Code Duplication |
protected function buildFullClassName($className, $namespace) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
// Check if we need to append namespace to dependency |
99
|
2 |
|
if ($className !== null && strpos($className, '\\') === false) { |
100
|
1 |
|
return $namespace . '\\' . $className; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return $className; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.