|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpParser\Builder; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser; |
|
6
|
|
|
use PhpParser\Node; |
|
7
|
|
|
use PhpParser\Node\Stmt; |
|
8
|
|
|
|
|
9
|
|
|
class Method extends FunctionLike |
|
10
|
|
|
{ |
|
11
|
|
|
protected $name; |
|
12
|
|
|
protected $type = 0; |
|
13
|
|
|
protected $stmts = array(); |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Creates a method builder. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $name Name of the method |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($name) { |
|
21
|
|
|
$this->name = $name; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Makes the method public. |
|
26
|
|
|
* |
|
27
|
|
|
* @return $this The builder instance (for fluid interface) |
|
28
|
|
|
*/ |
|
29
|
|
|
public function makePublic() { |
|
30
|
|
|
$this->setModifier(Stmt\Class_::MODIFIER_PUBLIC); |
|
31
|
|
|
|
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Makes the method protected. |
|
37
|
|
|
* |
|
38
|
|
|
* @return $this The builder instance (for fluid interface) |
|
39
|
|
|
*/ |
|
40
|
|
|
public function makeProtected() { |
|
41
|
|
|
$this->setModifier(Stmt\Class_::MODIFIER_PROTECTED); |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Makes the method private. |
|
48
|
|
|
* |
|
49
|
|
|
* @return $this The builder instance (for fluid interface) |
|
50
|
|
|
*/ |
|
51
|
|
|
public function makePrivate() { |
|
52
|
|
|
$this->setModifier(Stmt\Class_::MODIFIER_PRIVATE); |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Makes the method static. |
|
59
|
|
|
* |
|
60
|
|
|
* @return $this The builder instance (for fluid interface) |
|
61
|
|
|
*/ |
|
62
|
|
|
public function makeStatic() { |
|
63
|
|
|
$this->setModifier(Stmt\Class_::MODIFIER_STATIC); |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Makes the method abstract. |
|
70
|
|
|
* |
|
71
|
|
|
* @return $this The builder instance (for fluid interface) |
|
72
|
|
|
*/ |
|
73
|
|
|
public function makeAbstract() { |
|
74
|
|
|
if (!empty($this->stmts)) { |
|
75
|
|
|
throw new \LogicException('Cannot make method with statements abstract'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT); |
|
79
|
|
|
$this->stmts = null; // abstract methods don't have statements |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Makes the method final. |
|
86
|
|
|
* |
|
87
|
|
|
* @return $this The builder instance (for fluid interface) |
|
88
|
|
|
*/ |
|
89
|
|
|
public function makeFinal() { |
|
90
|
|
|
$this->setModifier(Stmt\Class_::MODIFIER_FINAL); |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Adds a statement. |
|
97
|
|
|
* |
|
98
|
|
|
* @param Node|PhpParser\Builder $stmt The statement to add |
|
99
|
|
|
* |
|
100
|
|
|
* @return $this The builder instance (for fluid interface) |
|
101
|
|
|
*/ |
|
102
|
|
|
public function addStmt($stmt) { |
|
103
|
|
|
if (null === $this->stmts) { |
|
104
|
|
|
throw new \LogicException('Cannot add statements to an abstract method'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->stmts[] = $this->normalizeNode($stmt); |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns the built method node. |
|
114
|
|
|
* |
|
115
|
|
|
* @return Stmt\ClassMethod The built method node |
|
116
|
|
|
*/ |
|
117
|
|
View Code Duplication |
public function getNode() { |
|
|
|
|
|
|
118
|
|
|
return new Stmt\ClassMethod($this->name, array( |
|
119
|
|
|
'type' => $this->type, |
|
120
|
|
|
'byRef' => $this->returnByRef, |
|
121
|
|
|
'params' => $this->params, |
|
122
|
|
|
'stmts' => $this->stmts, |
|
123
|
|
|
), $this->attributes); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..