|
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
|
|
|
private $nullableReturnType = false; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ArgumentNode[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $arguments = array(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var TypeHintReference |
|
38
|
|
|
*/ |
|
39
|
|
|
private $typeHintReference; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $name |
|
43
|
|
|
* @param string $code |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct($name, $code = null, TypeHintReference $typeHintReference = null) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->name = $name; |
|
48
|
|
|
$this->code = $code; |
|
49
|
|
|
$this->typeHintReference = $typeHintReference ?: new TypeHintReference(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getVisibility() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->visibility; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $visibility |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function setVisibility($visibility) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$visibility = strtolower($visibility); |
|
63
|
|
|
|
|
64
|
|
|
if (!in_array($visibility, array('public', 'private', 'protected'))) { |
|
65
|
|
|
throw new InvalidArgumentException(sprintf( |
|
66
|
|
|
'`%s` method visibility is not supported.', $visibility |
|
67
|
|
|
)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->visibility = $visibility; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function isStatic() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->static; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function setStatic($static = true) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->static = (bool) $static; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function returnsReference() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->returnsReference; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function setReturnsReference() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->returnsReference = true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function getName() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->name; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function addArgument(ArgumentNode $argument) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->arguments[] = $argument; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return ArgumentNode[] |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getArguments() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->arguments; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function hasReturnType() |
|
112
|
|
|
{ |
|
113
|
|
|
return null !== $this->returnType; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $type |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setReturnType($type = null) |
|
120
|
|
|
{ |
|
121
|
|
|
switch ($type) { |
|
122
|
|
|
case '': |
|
123
|
|
|
$this->returnType = null; |
|
124
|
|
|
break; |
|
125
|
|
|
|
|
126
|
|
|
case 'double': |
|
127
|
|
|
case 'real': |
|
128
|
|
|
$type = 'float'; |
|
|
|
|
|
|
129
|
|
|
// intentional fall through |
|
130
|
|
|
|
|
131
|
|
|
case 'boolean': |
|
132
|
|
|
$type = 'bool'; |
|
|
|
|
|
|
133
|
|
|
// intentional fall through |
|
134
|
|
|
|
|
135
|
|
|
case 'integer': |
|
136
|
|
|
$type = 'int'; |
|
137
|
|
|
// intentional fall through |
|
138
|
|
|
|
|
139
|
|
|
case 'string': |
|
140
|
|
|
case 'float': |
|
141
|
|
|
case 'int': |
|
142
|
|
|
case 'bool': |
|
143
|
|
|
case 'array': |
|
144
|
|
|
case 'object': |
|
145
|
|
|
case 'callable': |
|
146
|
|
|
case 'iterable': |
|
147
|
|
|
case 'void': |
|
148
|
|
|
if ($this->typeHintReference->isBuiltInTypeHint($type)) { |
|
149
|
|
|
$this->returnType = $type; |
|
150
|
|
|
break; |
|
151
|
|
|
} |
|
152
|
|
|
// intentional fall-through |
|
153
|
|
|
|
|
154
|
|
|
default: |
|
155
|
|
|
$this->returnType = '\\' . ltrim($type, '\\'); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function getReturnType() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->returnType; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param bool $bool |
|
166
|
|
|
*/ |
|
167
|
|
|
public function setNullableReturnType($bool = true) |
|
168
|
|
|
{ |
|
169
|
|
|
$this->nullableReturnType = (bool) $bool; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
|
|
public function hasNullableReturnType() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->nullableReturnType; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param string $code |
|
182
|
|
|
*/ |
|
183
|
|
|
public function setCode($code) |
|
184
|
|
|
{ |
|
185
|
|
|
$this->code = $code; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function getCode() |
|
189
|
|
|
{ |
|
190
|
|
|
if ($this->returnsReference) |
|
191
|
|
|
{ |
|
192
|
|
|
return "throw new \Prophecy\Exception\Doubler\ReturnByReferenceException('Returning by reference not supported', get_class(\$this), '{$this->name}');"; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return (string) $this->code; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function useParentCode() |
|
199
|
|
|
{ |
|
200
|
|
|
$this->code = sprintf( |
|
201
|
|
|
'return parent::%s(%s);', $this->getName(), implode(', ', |
|
202
|
|
|
array_map(array($this, 'generateArgument'), $this->arguments) |
|
203
|
|
|
) |
|
204
|
|
|
); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
private function generateArgument(ArgumentNode $arg) |
|
208
|
|
|
{ |
|
209
|
|
|
$argument = '$'.$arg->getName(); |
|
210
|
|
|
|
|
211
|
|
|
if ($arg->isVariadic()) { |
|
212
|
|
|
$argument = '...'.$argument; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return $argument; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
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.