1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 07.08.16 at 15:46 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\container\configurator; |
7
|
|
|
|
8
|
|
|
use samsonframework\container\metadata\MethodMetadata; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Method argument injection configurator. |
12
|
|
|
* |
13
|
|
|
* @author Vitaly Egorov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class InjectableArgumentConfigurator extends InjectableAbstractConfigurator implements MethodConfiguratorInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
* |
20
|
|
|
* @throws \InvalidArgumentException |
21
|
|
|
*/ |
22
|
|
|
public function toMethodMetadata(MethodMetadata $methodMetadata) |
23
|
|
|
{ |
24
|
|
|
// Check for argument name input and validity |
25
|
|
View Code Duplication |
if (!$this->checkArgumentExists($this->argumentName, $methodMetadata)) { |
|
|
|
|
26
|
|
|
throw new \InvalidArgumentException( |
27
|
|
|
'@InjectArgument argument "' |
28
|
|
|
. $methodMetadata->classMetadata->className . '::' |
29
|
|
|
. $methodMetadata->name . ' ' |
30
|
|
|
. $this->argumentName . '" does not exists' |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Check for type input |
35
|
|
View Code Duplication |
if ($this->argumentType === null) { |
|
|
|
|
36
|
|
|
throw new \InvalidArgumentException( |
37
|
|
|
'@InjectArgument argument "' |
38
|
|
|
. $methodMetadata->classMetadata->className . '::' |
39
|
|
|
. $methodMetadata->name . ' ' |
40
|
|
|
. $this->argumentName . '" type not specified' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Store dependency with fully qualified type name |
45
|
|
|
$methodMetadata->dependencies[$this->argumentName] = $this->buildFullClassName( |
46
|
|
|
$this->argumentType, |
47
|
|
|
$methodMetadata->classMetadata->nameSpace |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check method argument existance. |
53
|
|
|
* |
54
|
|
|
* @param string $argument |
55
|
|
|
* @param MethodMetadata $methodMetadata |
56
|
|
|
* |
57
|
|
|
* @return bool True if @InjectArgument argument name is valid |
58
|
|
|
*/ |
59
|
|
|
protected function checkArgumentExists(string $argument, MethodMetadata $methodMetadata) : bool |
60
|
|
|
{ |
61
|
|
|
return $argument !== null && array_key_exists($argument, $methodMetadata->parametersMetadata); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
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.