1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Yaco\Definition; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Represents a call to a method. |
7
|
|
|
*/ |
8
|
|
|
class MethodCall implements ActionInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name of the method. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $methodName; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A list of arguments passed to the constructor. |
19
|
|
|
* |
20
|
|
|
* @var array Array of scalars or ReferenceInterface, or array mixing scalars, arrays, and ReferenceInterface |
21
|
|
|
*/ |
22
|
|
|
private $arguments = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* MethodCall constructor. |
26
|
|
|
* |
27
|
|
|
* @param string $methodName |
28
|
|
|
* @param array $arguments |
29
|
|
|
*/ |
30
|
|
|
public function __construct($methodName, array $arguments = array()) |
31
|
|
|
{ |
32
|
|
|
$this->methodName = $methodName; |
33
|
|
|
$this->arguments = $arguments; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Adds an argument to the list of arguments to be passed to the method. |
38
|
|
|
* |
39
|
|
|
* @param mixed $argument |
40
|
|
|
* |
41
|
|
|
* @return self |
42
|
|
|
*/ |
43
|
|
|
public function addArgument($argument) |
44
|
|
|
{ |
45
|
|
|
$this->arguments[] = $argument; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Generates PHP code for the line. |
52
|
|
|
* |
53
|
|
|
* @param string $variableName |
54
|
|
|
* @param string $containerVariable |
55
|
|
|
* @param array $usedVariables |
56
|
|
|
* |
57
|
|
|
* @return InlineEntryInterface |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function toPhpCode($variableName, $containerVariable, array $usedVariables) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$dumpedArguments = ValueUtils::dumpArguments($this->arguments, $containerVariable, $usedVariables); |
62
|
|
|
$codeLine = sprintf('%s->%s(%s);', $variableName, $this->methodName, $dumpedArguments->getExpression()); |
63
|
|
|
|
64
|
|
|
return new InlineEntry('', $dumpedArguments->getStatements().$codeLine, $dumpedArguments->getUsedVariables()); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
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.