1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PDepend. |
4
|
|
|
* |
5
|
|
|
* PHP Version 5 |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2008-2015, Manuel Pichler <[email protected]>. |
8
|
|
|
* All rights reserved. |
9
|
|
|
* |
10
|
|
|
* Redistribution and use in source and binary forms, with or without |
11
|
|
|
* modification, are permitted provided that the following conditions |
12
|
|
|
* are met: |
13
|
|
|
* |
14
|
|
|
* * Redistributions of source code must retain the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer. |
16
|
|
|
* |
17
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
18
|
|
|
* notice, this list of conditions and the following disclaimer in |
19
|
|
|
* the documentation and/or other materials provided with the |
20
|
|
|
* distribution. |
21
|
|
|
* |
22
|
|
|
* * Neither the name of Manuel Pichler nor the names of his |
23
|
|
|
* contributors may be used to endorse or promote products derived |
24
|
|
|
* from this software without specific prior written permission. |
25
|
|
|
* |
26
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
27
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
28
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
29
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
30
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
31
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
32
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
33
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
34
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
35
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
36
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
37
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
38
|
|
|
* |
39
|
|
|
* @copyright 2008-2015 Manuel Pichler. All rights reserved. |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @since 2.3 |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace PDepend\Source\AST; |
45
|
|
|
|
46
|
|
|
use PDepend\Source\ASTVisitor\ASTVisitor; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Represents a php class node. |
50
|
|
|
* |
51
|
|
|
* @copyright 2008-2015 Manuel Pichler. All rights reserved. |
52
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
53
|
|
|
* @since 2.3 |
54
|
|
|
*/ |
55
|
|
|
class ASTAnonymousClass extends ASTClass implements ASTNode |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* The parent node of this node or <b>null</b> when this node is the root |
59
|
|
|
* of a node tree. |
60
|
|
|
* |
61
|
|
|
* @var \PDepend\Source\AST\ASTNode |
62
|
|
|
*/ |
63
|
|
|
protected $parent = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Metadata for this node instance, serialized in a string. This string |
67
|
|
|
* contains the start, end line, and the start, end column and the node |
68
|
|
|
* image in a colon separated string. |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
* @since 0.10.4 |
72
|
|
|
*/ |
73
|
|
|
protected $metadata = ':::'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $image |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function setImage($image) |
80
|
|
|
{ |
81
|
|
|
$this->setName($image); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the source image of this ast node. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getImage() |
90
|
|
|
{ |
91
|
|
|
return $this->getName(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the start line for this ast node. |
96
|
|
|
* |
97
|
|
|
* @return integer |
98
|
|
|
*/ |
99
|
|
|
public function getStartLine() |
100
|
|
|
{ |
101
|
|
|
return $this->getMetadataInteger(0); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns the start column for this ast node. |
106
|
|
|
* |
107
|
|
|
* @return integer |
108
|
|
|
*/ |
109
|
|
|
public function getStartColumn() |
110
|
|
|
{ |
111
|
|
|
return $this->getMetadataInteger(2); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the end line for this ast node. |
116
|
|
|
* |
117
|
|
|
* @return integer |
118
|
|
|
*/ |
119
|
|
|
public function getEndLine() |
120
|
|
|
{ |
121
|
|
|
return $this->getMetadataInteger(1); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns the end column for this ast node. |
126
|
|
|
* |
127
|
|
|
* @return integer |
128
|
|
|
*/ |
129
|
|
|
public function getEndColumn() |
130
|
|
|
{ |
131
|
|
|
return $this->getMetadataInteger(3); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* For better performance we have moved the single setter methods for the |
136
|
|
|
* node columns and lines into this configure method. |
137
|
|
|
* |
138
|
|
|
* @param integer $startLine |
139
|
|
|
* @param integer $endLine |
140
|
|
|
* @param integer $startColumn |
141
|
|
|
* @param integer $endColumn |
142
|
|
|
* @return void |
143
|
|
|
* @since 0.9.10 |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function configureLinesAndColumns($startLine, $endLine, $startColumn, $endColumn) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$this->setMetadataInteger(0, $startLine); |
148
|
|
|
$this->setMetadataInteger(1, $endLine); |
149
|
|
|
$this->setMetadataInteger(2, $startColumn); |
150
|
|
|
$this->setMetadataInteger(3, $endColumn); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns the parent node of this node or <b>null</b> when this node is |
155
|
|
|
* the root of a node tree. |
156
|
|
|
* |
157
|
|
|
* @return \PDepend\Source\AST\ASTNode |
158
|
|
|
*/ |
159
|
|
|
public function getParent() |
160
|
|
|
{ |
161
|
|
|
return $this->parent; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Sets the parent node of this node. |
166
|
|
|
* |
167
|
|
|
* @param \PDepend\Source\AST\ASTNode $node |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function setParent(ASTNode $node) |
171
|
|
|
{ |
172
|
|
|
$this->parent = $node; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Traverses up the node tree and finds all parent nodes that are instances |
177
|
|
|
* of <b>$parentType</b>. |
178
|
|
|
* |
179
|
|
|
* @param string $parentType |
180
|
|
|
* @return \PDepend\Source\AST\ASTNode[] |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
public function getParentsOfType($parentType) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$parents = array(); |
185
|
|
|
|
186
|
|
|
$parentNode = $this->parent; |
187
|
|
|
while (is_object($parentNode)) { |
188
|
|
|
if ($parentNode instanceof $parentType) { |
189
|
|
|
array_unshift($parents, $parentNode); |
190
|
|
|
} |
191
|
|
|
$parentNode = $parentNode->getParent(); |
192
|
|
|
} |
193
|
|
|
return $parents; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* This method adds a new child node at the first position of the children. |
198
|
|
|
* |
199
|
|
|
* @param \PDepend\Source\AST\ASTNode $node |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
public function prependChild(ASTNode $node) |
203
|
|
|
{ |
204
|
|
|
array_unshift($this->nodes, $node); |
205
|
|
|
$node->setParent($this); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Will return <b>true</b> if this class was declared anonymous in an |
210
|
|
|
* allocation expression. |
211
|
|
|
* |
212
|
|
|
* @return boolean |
213
|
|
|
*/ |
214
|
|
|
public function isAnonymous() |
215
|
|
|
{ |
216
|
|
|
return true; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param \PDepend\Source\ASTVisitor\ASTVisitor $visitor |
221
|
|
|
* @param mixed $data |
222
|
|
|
* @return void |
223
|
|
|
*/ |
224
|
|
|
public function accept(ASTVisitor $visitor, $data = null) |
225
|
|
|
{ |
226
|
|
|
return $visitor->visitAnonymousClass($this, $data); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* The magic sleep method will be called by PHP's runtime environment right |
231
|
|
|
* before an instance of this class gets serialized. It should return an |
232
|
|
|
* array with those property names that should be serialized for this class. |
233
|
|
|
* |
234
|
|
|
* @return array |
235
|
|
|
* @since 0.10.0 |
236
|
|
|
*/ |
237
|
|
|
public function __sleep() |
238
|
|
|
{ |
239
|
|
|
return array_merge(array('comment', 'metadata', 'nodes'), parent::__sleep()); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* The magic wakeup method will be called by PHP's runtime environment when |
244
|
|
|
* a serialized instance of this class was unserialized. This implementation |
245
|
|
|
* of the wakeup method will register this object in the the global class |
246
|
|
|
* context. |
247
|
|
|
* |
248
|
|
|
* @return void |
249
|
|
|
*/ |
250
|
|
|
public function __wakeup() |
251
|
|
|
{ |
252
|
|
|
$this->methods = null; |
|
|
|
|
253
|
|
|
|
254
|
|
|
foreach ($this->nodes as $node) { |
255
|
|
|
$node->setParent($this); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
parent::__wakeup(); |
259
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Returns an integer value that was stored under the given index. |
264
|
|
|
* |
265
|
|
|
* @param integer $index |
266
|
|
|
* @return integer |
267
|
|
|
* @since 0.10.4 |
268
|
|
|
*/ |
269
|
|
|
protected function getMetadataInteger($index) |
270
|
|
|
{ |
271
|
|
|
return (int) $this->getMetadata($index); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Stores an integer value under the given index in the internally used data |
276
|
|
|
* string. |
277
|
|
|
* |
278
|
|
|
* @param integer $index |
279
|
|
|
* @param integer $value |
280
|
|
|
* @return void |
281
|
|
|
* @since 0.10.4 |
282
|
|
|
*/ |
283
|
|
|
protected function setMetadataInteger($index, $value) |
284
|
|
|
{ |
285
|
|
|
$this->setMetadata($index, $value); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Returns the value that was stored under the given index. |
290
|
|
|
* |
291
|
|
|
* @param integer $index |
292
|
|
|
* @return mixed |
293
|
|
|
* @since 0.10.4 |
294
|
|
|
*/ |
295
|
|
|
protected function getMetadata($index) |
296
|
|
|
{ |
297
|
|
|
$metadata = explode(':', $this->metadata, $this->getMetadataSize()); |
298
|
|
|
return $metadata[$index]; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Stores the given value under the given index in an internal storage |
303
|
|
|
* container. |
304
|
|
|
* |
305
|
|
|
* @param integer $index |
306
|
|
|
* @param mixed $value |
307
|
|
|
* @return void |
308
|
|
|
* @since 0.10.4 |
309
|
|
|
*/ |
310
|
|
View Code Duplication |
protected function setMetadata($index, $value) |
|
|
|
|
311
|
|
|
{ |
312
|
|
|
$metadata = explode(':', $this->metadata, $this->getMetadataSize()); |
313
|
|
|
$metadata[$index] = $value; |
314
|
|
|
|
315
|
|
|
$this->metadata = join(':', $metadata); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Returns the total number of the used property bag. |
320
|
|
|
* |
321
|
|
|
* @return integer |
322
|
|
|
* @since 0.10.4 |
323
|
|
|
*/ |
324
|
|
|
protected function getMetadataSize() |
325
|
|
|
{ |
326
|
|
|
return 4; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
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.