| 1 |
<?php /** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */ |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Peast package |
| 5 |
* |
| 6 |
* (c) Marco Marchiò <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information refer to the LICENSE file |
| 9 |
* distributed with this source code |
| 10 |
*/ |
| 11 |
namespace Peast\Syntax\Node; |
| 12 |
|
| 13 |
use Peast\Syntax\SourceLocation; |
| 14 |
use Peast\Syntax\Position; |
| 15 |
|
| 16 |
/** |
| 17 |
* Base class for all the nodes generated by Peast. |
| 18 |
* |
| 19 |
* @author Marco Marchiò <[email protected]> |
| 20 |
* |
| 21 |
* @abstract |
| 22 |
*/ |
| 23 |
abstract class Node implements \JSONSerializable |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Map of node properties |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
protected $propertiesMap = array( |
| 31 |
"type" => false, |
| 32 |
"location" => false, |
| 33 |
"leadingComments" => false, |
| 34 |
"trailingComments" => false |
| 35 |
); |
| 36 |
|
| 37 |
/** |
| 38 |
* Node location in the source code |
| 39 |
* |
| 40 |
* @var SourceLocation |
| 41 |
*/ |
| 42 |
public $location; |
| 43 |
|
| 44 |
/** |
| 45 |
* Leading comments array |
| 46 |
* |
| 47 |
* @var Comment[] |
| 48 |
*/ |
| 49 |
protected $leadingComments = array(); |
| 50 |
|
| 51 |
/** |
| 52 |
* Trailing comments array |
| 53 |
* |
| 54 |
* @var Comment[] |
| 55 |
*/ |
| 56 |
protected $trailingComments = array(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Class constructor |
| 60 |
*/ |
| 61 |
public function __construct() |
| 62 |
{ |
| 63 |
$this->location = new SourceLocation; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns node type |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public function getType() |
| 72 |
{ |
| 73 |
$class = explode("\\", get_class($this)); |
| 74 |
return array_pop($class); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Sets leading comments array |
| 79 |
* |
| 80 |
* @param Comment[] $comments Comments array |
| 81 |
* |
| 82 |
* @return $this |
| 83 |
*/ |
| 84 |
public function setLeadingComments($comments) |
| 85 |
{ |
| 86 |
$this->assertArrayOf($comments, "Comment"); |
| 87 |
$this->leadingComments = $comments; |
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns leading comments array |
| 93 |
* |
| 94 |
* @return Comment[] |
| 95 |
*/ |
| 96 |
public function getLeadingComments() |
| 97 |
{ |
| 98 |
return $this->leadingComments; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Sets trailing comments array |
| 103 |
* |
| 104 |
* @param Comment[] $comments Comments array |
| 105 |
* |
| 106 |
* @return $this |
| 107 |
*/ |
| 108 |
public function setTrailingComments($comments) |
| 109 |
{ |
| 110 |
$this->assertArrayOf($comments, "Comment"); |
| 111 |
$this->trailingComments = $comments; |
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns trailing comments array |
| 117 |
* |
| 118 |
* @return Comment[] |
| 119 |
*/ |
| 120 |
public function getTrailingComments() |
| 121 |
{ |
| 122 |
return $this->trailingComments; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns node location in the source code |
| 127 |
* |
| 128 |
* @return SourceLocation |
| 129 |
*/ |
| 130 |
public function getLocation() |
| 131 |
{ |
| 132 |
return $this->location; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Sets the start position of the node in the source code |
| 137 |
* |
| 138 |
* @param Position $position Start position |
| 139 |
* |
| 140 |
* @return $this |
| 141 |
*/ |
| 142 |
public function setStartPosition(Position $position) |
| 143 |
{ |
| 144 |
$this->location->start = $position; |
| 145 |
return $this; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Sets the end position of the node in the source code |
| 150 |
* |
| 151 |
* @param Position $position Start position |
| 152 |
* |
| 153 |
* @return $this |
| 154 |
*/ |
| 155 |
public function setEndPosition(Position $position) |
| 156 |
{ |
| 157 |
$this->location->end = $position; |
| 158 |
return $this; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Traverses the current node and all its child nodes using the given |
| 163 |
* function |
| 164 |
* |
| 165 |
* @param callable $fn Function that will be called on each node |
| 166 |
* @param array $options Options array. See Traverser class |
| 167 |
* documentation for available options |
| 168 |
* |
| 169 |
* @return $this |
| 170 |
*/ |
| 171 |
public function traverse(callable $fn, $options = array()) |
| 172 |
{ |
| 173 |
$traverser = new \Peast\Traverser($options); |
| 174 |
$traverser->addFunction($fn)->traverse($this); |
| 175 |
return $this; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns a serializable version of the node |
| 180 |
* |
| 181 |
* @return array |
| 182 |
*/ |
| 183 |
#[\ReturnTypeWillChange] |
| 184 |
public function jsonSerialize() |
| 185 |
{ |
| 186 |
$ret = array(); |
| 187 |
$props = \Peast\Syntax\Utils::getNodeProperties($this); |
| 188 |
foreach ($props as $prop) { |
| 189 |
$ret[$prop["name"]] = $this->{$prop["getter"]}(); |
| 190 |
} |
| 191 |
return $ret; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Renders the current node |
| 196 |
* |
| 197 |
* @param \Peast\Formatter\Base $formatter Formatter to use for the |
| 198 |
* rendering |
| 199 |
* |
| 200 |
* @return string |
| 201 |
*/ |
| 202 |
public function render(\Peast\Formatter\Base $formatter) |
| 203 |
{ |
| 204 |
$renderer = new \Peast\Renderer(); |
| 205 |
return $renderer->setFormatter($formatter)->render($this); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Asserts that the given value is an array of defined type |
| 210 |
* |
| 211 |
* @param mixed $params Value to check |
| 212 |
* @param string|array $classes Class or array of classes to check against |
| 213 |
* @param bool $allowNull If true, null values are allowed |
| 214 |
* |
| 215 |
* @return void |
| 216 |
* |
| 217 |
* @codeCoverageIgnore |
| 218 |
*/ |
| 219 |
protected function assertArrayOf($params, $classes, $allowNull = false) |
| 220 |
{ |
| 221 |
if (!is_array($classes)) { |
| 222 |
$classes = array($classes); |
| 223 |
} |
| 224 |
if (!is_array($params)) { |
| 225 |
$this->typeError($params, $classes, $allowNull, true); |
| 226 |
} else { |
| 227 |
foreach ($params as $param) { |
| 228 |
foreach ($classes as $class) { |
| 229 |
if ($param === null && $allowNull) { |
| 230 |
continue 2; |
| 231 |
} else { |
| 232 |
$c = "Peast\\Syntax\\Node\\$class"; |
| 233 |
if ($param instanceof $c) { |
| 234 |
continue 2; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
$this->typeError($param, $classes, $allowNull, true, true); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Asserts that the given value respects the defined type |
| 245 |
* |
| 246 |
* @param mixed $param Value to check |
| 247 |
* @param string|array $classes Class or array of classes to check against |
| 248 |
* @param bool $allowNull If true, null values are allowed |
| 249 |
* |
| 250 |
* @return void |
| 251 |
* |
| 252 |
* @codeCoverageIgnore |
| 253 |
*/ |
| 254 |
protected function assertType($param, $classes, $allowNull = false) |
| 255 |
{ |
| 256 |
if (!is_array($classes)) { |
| 257 |
$classes = array($classes); |
| 258 |
} |
| 259 |
if ($param === null) { |
| 260 |
if (!$allowNull) { |
| 261 |
$this->typeError($param, $classes, $allowNull); |
| 262 |
} |
| 263 |
} else { |
| 264 |
foreach ($classes as $class) { |
| 265 |
$c = "Peast\\Syntax\\Node\\$class"; |
| 266 |
if ($param instanceof $c) { |
| 267 |
return; |
| 268 |
} |
| 269 |
} |
| 270 |
$this->typeError($param, $classes, $allowNull); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Throws an error if the defined type is not supported b |
| 276 |
* |
| 277 |
* @param mixed $param The value to check |
| 278 |
* @param mixed $allowedTypes Class or array of classes to check against |
| 279 |
* @param bool $allowNull If true, null values are allowed |
| 280 |
* @param bool $array If true, the value must be an array |
| 281 |
* @param bool $inArray If true, the value is an array but the content |
| 282 |
* does not respects the type |
| 283 |
* |
| 284 |
* @return void |
| 285 |
* |
| 286 |
* @throws \TypeError |
| 287 |
* |
| 288 |
* @codeCoverageIgnore |
| 289 |
*/ |
| 290 |
protected function typeError( |
| 291 |
$param, $allowedTypes, $allowNull = false, $array = false, |
| 292 |
$inArray = false |
| 293 |
) { |
| 294 |
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
| 295 |
$method = $backtrace[2]["class"] . "::" . $backtrace[2]["function"]; |
| 296 |
$msg = "Argument 0 passed to $method must be "; |
| 297 |
if ($array) { |
| 298 |
$msg .= "an array of "; |
| 299 |
} |
| 300 |
$msg .= implode(" or ", $allowedTypes); |
| 301 |
if ($allowNull) { |
| 302 |
$msg .= " or null"; |
| 303 |
} |
| 304 |
if (is_object($param)) { |
| 305 |
$parts = explode("\\", get_class($param)); |
| 306 |
$type = array_pop($parts); |
| 307 |
} else { |
| 308 |
$type = gettype($param); |
| 309 |
} |
| 310 |
if ($inArray) { |
| 311 |
$type = "array of $type"; |
| 312 |
} |
| 313 |
$msg .= ", $type given"; |
| 314 |
if (version_compare(phpversion(), '7', '>=')) { |
| 315 |
throw new \TypeError($msg); |
| 316 |
} else { |
| 317 |
trigger_error($msg, E_USER_ERROR); |
| 318 |
} |
| 319 |
} |
| 320 |
} |