|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Spiral\Reactor\ClassDeclaration; |
|
9
|
|
|
|
|
10
|
|
|
use Spiral\Reactor\Body\Source; |
|
11
|
|
|
use Spiral\Reactor\ClassDeclaration\Aggregators\ParameterAggregator; |
|
12
|
|
|
use Spiral\Reactor\Prototypes\NamedDeclaration; |
|
13
|
|
|
use Spiral\Reactor\ReplaceableInterface; |
|
14
|
|
|
use Spiral\Reactor\Traits\AccessTrait; |
|
15
|
|
|
use Spiral\Reactor\Traits\CommentTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Represent class method. |
|
19
|
|
|
*/ |
|
20
|
|
|
class MethodDeclaration extends NamedDeclaration implements ReplaceableInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use CommentTrait, AccessTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
private $static = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ParameterAggregator |
|
31
|
|
|
*/ |
|
32
|
|
|
private $parameters = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Source |
|
36
|
|
|
*/ |
|
37
|
|
|
private $source = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $name |
|
41
|
|
|
* @param string|array $source |
|
42
|
|
|
* @param string $comment |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(string $name, $source = '', string $comment = '') |
|
45
|
|
|
{ |
|
46
|
|
|
parent::__construct($name); |
|
47
|
|
|
|
|
48
|
|
|
$this->parameters = new ParameterAggregator([]); |
|
49
|
|
|
|
|
50
|
|
|
$this->initSource($source); |
|
51
|
|
|
$this->initComment($comment); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param bool $static |
|
56
|
|
|
* |
|
57
|
|
|
* @return self |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setStatic(bool $static = true): MethodDeclaration |
|
60
|
|
|
{ |
|
61
|
|
|
$this->static = (bool)$static; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public function isStatic(): bool |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->static; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Rename to getSource()? |
|
76
|
|
|
* |
|
77
|
|
|
* @return Source |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getSource(): Source |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->source; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set method source. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string|array $source |
|
88
|
|
|
* |
|
89
|
|
|
* @return self |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setSource($source): MethodDeclaration |
|
92
|
|
|
{ |
|
93
|
|
|
if (!empty($source)) { |
|
94
|
|
|
if (is_array($source)) { |
|
95
|
|
|
$this->source->setLines($source); |
|
96
|
|
|
} elseif (is_string($source)) { |
|
97
|
|
|
$this->source->setString($source); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return ParameterAggregator|ParameterDeclaration[] |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getParameters(): ParameterAggregator |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->parameters; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $name |
|
114
|
|
|
* |
|
115
|
|
|
* @return ParameterDeclaration |
|
116
|
|
|
*/ |
|
117
|
|
|
public function parameter(string $name): ParameterDeclaration |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->parameters->get($name); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
* |
|
125
|
|
|
* @return $this |
|
126
|
|
|
*/ |
|
127
|
|
|
public function replace($search, $replace): MethodDeclaration |
|
128
|
|
|
{ |
|
129
|
|
|
$this->docComment->replace($search, $replace); |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param int $indentLevel |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
public function render(int $indentLevel = 0): string |
|
140
|
|
|
{ |
|
141
|
|
|
$result = ''; |
|
142
|
|
|
if (!$this->docComment->isEmpty()) { |
|
143
|
|
|
$result .= $this->docComment->render($indentLevel) . "\n"; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$method = "{$this->getAccess()} function {$this->getName()}"; |
|
147
|
|
|
if (!$this->parameters->isEmpty()) { |
|
148
|
|
|
$method .= "({$this->parameters->render()})"; |
|
149
|
|
|
} else { |
|
150
|
|
|
$method .= "()"; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$result .= $this->addIndent($method, $indentLevel) . "\n"; |
|
154
|
|
|
$result .= $this->addIndent('{', $indentLevel) . "\n"; |
|
155
|
|
|
|
|
156
|
|
|
if (!$this->source->isEmpty()) { |
|
157
|
|
|
$result .= $this->source->render($indentLevel + 1) . "\n"; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$result .= $this->addIndent("}", $indentLevel); |
|
161
|
|
|
|
|
162
|
|
|
return $result; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Init source value. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string|array $source |
|
169
|
|
|
*/ |
|
170
|
|
|
private function initSource($source) |
|
171
|
|
|
{ |
|
172
|
|
|
if (empty($this->source)) { |
|
173
|
|
|
$this->source = new Source(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if (!empty($source)) { |
|
177
|
|
|
if (is_array($source)) { |
|
178
|
|
|
$this->source->setLines($source); |
|
179
|
|
|
} elseif (is_string($source)) { |
|
180
|
|
|
$this->source->setString($source); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |