1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Prophecy. |
5
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
6
|
|
|
* Marcello Duarte <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Prophecy\Doubler\Generator\Node; |
13
|
|
|
|
14
|
|
|
use Prophecy\Exception\InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Method node. |
18
|
|
|
* |
19
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class MethodNode |
22
|
|
|
{ |
23
|
|
|
private $name; |
24
|
|
|
private $code; |
25
|
|
|
private $visibility = 'public'; |
26
|
|
|
private $static = false; |
27
|
|
|
private $returnsReference = false; |
28
|
|
|
private $returnType; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ArgumentNode[] |
32
|
|
|
*/ |
33
|
|
|
private $arguments = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $name |
37
|
|
|
* @param string $code |
38
|
|
|
*/ |
39
|
|
|
public function __construct($name, $code = null) |
40
|
|
|
{ |
41
|
|
|
$this->name = $name; |
42
|
|
|
$this->code = $code; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getVisibility() |
46
|
|
|
{ |
47
|
|
|
return $this->visibility; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $visibility |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function setVisibility($visibility) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$visibility = strtolower($visibility); |
56
|
|
|
|
57
|
|
|
if (!in_array($visibility, array('public', 'private', 'protected'))) { |
58
|
|
|
throw new InvalidArgumentException(sprintf( |
59
|
|
|
'`%s` method visibility is not supported.', $visibility |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->visibility = $visibility; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function isStatic() |
67
|
|
|
{ |
68
|
|
|
return $this->static; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setStatic($static = true) |
72
|
|
|
{ |
73
|
|
|
$this->static = (bool) $static; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function returnsReference() |
77
|
|
|
{ |
78
|
|
|
return $this->returnsReference; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setReturnsReference() |
82
|
|
|
{ |
83
|
|
|
$this->returnsReference = true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getName() |
87
|
|
|
{ |
88
|
|
|
return $this->name; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function addArgument(ArgumentNode $argument) |
92
|
|
|
{ |
93
|
|
|
$this->arguments[] = $argument; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return ArgumentNode[] |
98
|
|
|
*/ |
99
|
|
|
public function getArguments() |
100
|
|
|
{ |
101
|
|
|
return $this->arguments; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function hasReturnType() |
105
|
|
|
{ |
106
|
|
|
return null !== $this->returnType; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $type |
111
|
|
|
*/ |
112
|
|
|
public function setReturnType($type = null) |
113
|
|
|
{ |
114
|
|
|
switch ($type) { |
115
|
|
|
case '': |
116
|
|
|
$this->returnType = null; |
117
|
|
|
break; |
118
|
|
|
|
119
|
|
|
case 'string'; |
120
|
|
|
case 'float': |
121
|
|
|
case 'int': |
122
|
|
|
case 'bool': |
123
|
|
|
case 'array': |
124
|
|
|
case 'callable': |
125
|
|
|
$this->returnType = $type; |
126
|
|
|
break; |
127
|
|
|
|
128
|
|
|
case 'double': |
129
|
|
|
case 'real': |
130
|
|
|
$this->returnType = 'float'; |
131
|
|
|
break; |
132
|
|
|
|
133
|
|
|
case 'boolean': |
134
|
|
|
$this->returnType = 'bool'; |
135
|
|
|
break; |
136
|
|
|
|
137
|
|
|
case 'integer': |
138
|
|
|
$this->returnType = 'int'; |
139
|
|
|
break; |
140
|
|
|
|
141
|
|
|
default: |
142
|
|
|
$this->returnType = '\\' . ltrim($type, '\\'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getReturnType() |
147
|
|
|
{ |
148
|
|
|
return $this->returnType; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $code |
153
|
|
|
*/ |
154
|
|
|
public function setCode($code) |
155
|
|
|
{ |
156
|
|
|
$this->code = $code; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getCode() |
160
|
|
|
{ |
161
|
|
|
if ($this->returnsReference) |
162
|
|
|
{ |
163
|
|
|
return "throw new \Prophecy\Exception\Doubler\ReturnByReferenceException('Returning by reference not supported', get_class(\$this), '{$this->name}');"; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return (string) $this->code; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function useParentCode() |
170
|
|
|
{ |
171
|
|
|
$this->code = sprintf( |
172
|
|
|
'return parent::%s(%s);', $this->getName(), implode(', ', |
173
|
|
|
array_map(array($this, 'generateArgument'), $this->arguments) |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
private function generateArgument(ArgumentNode $arg) |
179
|
|
|
{ |
180
|
|
|
$argument = '$'.$arg->getName(); |
181
|
|
|
|
182
|
|
|
if ($arg->isVariadic()) { |
183
|
|
|
$argument = '...'.$argument; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $argument; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
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.