annotate vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@13 1 <?php declare(strict_types=1);
Chris@0 2
Chris@0 3 namespace PhpParser;
Chris@0 4
Chris@0 5 abstract class NodeAbstract implements Node, \JsonSerializable
Chris@0 6 {
Chris@0 7 protected $attributes;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Creates a Node.
Chris@0 11 *
Chris@0 12 * @param array $attributes Array of attributes
Chris@0 13 */
Chris@13 14 public function __construct(array $attributes = []) {
Chris@0 15 $this->attributes = $attributes;
Chris@0 16 }
Chris@0 17
Chris@0 18 /**
Chris@13 19 * Gets line the node started in (alias of getStartLine).
Chris@0 20 *
Chris@13 21 * @return int Start line (or -1 if not available)
Chris@0 22 */
Chris@13 23 public function getLine() : int {
Chris@13 24 return $this->attributes['startLine'] ?? -1;
Chris@0 25 }
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Gets line the node started in.
Chris@0 29 *
Chris@13 30 * Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
Chris@13 31 *
Chris@13 32 * @return int Start line (or -1 if not available)
Chris@0 33 */
Chris@13 34 public function getStartLine() : int {
Chris@13 35 return $this->attributes['startLine'] ?? -1;
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@13 39 * Gets the line the node ended in.
Chris@0 40 *
Chris@13 41 * Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
Chris@0 42 *
Chris@13 43 * @return int End line (or -1 if not available)
Chris@0 44 */
Chris@13 45 public function getEndLine() : int {
Chris@13 46 return $this->attributes['endLine'] ?? -1;
Chris@13 47 }
Chris@13 48
Chris@13 49 /**
Chris@13 50 * Gets the token offset of the first token that is part of this node.
Chris@13 51 *
Chris@13 52 * The offset is an index into the array returned by Lexer::getTokens().
Chris@13 53 *
Chris@13 54 * Requires the 'startTokenPos' attribute to be enabled in the lexer (DISABLED by default).
Chris@13 55 *
Chris@13 56 * @return int Token start position (or -1 if not available)
Chris@13 57 */
Chris@13 58 public function getStartTokenPos() : int {
Chris@13 59 return $this->attributes['startTokenPos'] ?? -1;
Chris@13 60 }
Chris@13 61
Chris@13 62 /**
Chris@13 63 * Gets the token offset of the last token that is part of this node.
Chris@13 64 *
Chris@13 65 * The offset is an index into the array returned by Lexer::getTokens().
Chris@13 66 *
Chris@13 67 * Requires the 'endTokenPos' attribute to be enabled in the lexer (DISABLED by default).
Chris@13 68 *
Chris@13 69 * @return int Token end position (or -1 if not available)
Chris@13 70 */
Chris@13 71 public function getEndTokenPos() : int {
Chris@13 72 return $this->attributes['endTokenPos'] ?? -1;
Chris@13 73 }
Chris@13 74
Chris@13 75 /**
Chris@13 76 * Gets the file offset of the first character that is part of this node.
Chris@13 77 *
Chris@13 78 * Requires the 'startFilePos' attribute to be enabled in the lexer (DISABLED by default).
Chris@13 79 *
Chris@13 80 * @return int File start position (or -1 if not available)
Chris@13 81 */
Chris@13 82 public function getStartFilePos() : int {
Chris@13 83 return $this->attributes['startFilePos'] ?? -1;
Chris@13 84 }
Chris@13 85
Chris@13 86 /**
Chris@13 87 * Gets the file offset of the last character that is part of this node.
Chris@13 88 *
Chris@13 89 * Requires the 'endFilePos' attribute to be enabled in the lexer (DISABLED by default).
Chris@13 90 *
Chris@13 91 * @return int File end position (or -1 if not available)
Chris@13 92 */
Chris@13 93 public function getEndFilePos() : int {
Chris@13 94 return $this->attributes['endFilePos'] ?? -1;
Chris@13 95 }
Chris@13 96
Chris@13 97 /**
Chris@13 98 * Gets all comments directly preceding this node.
Chris@13 99 *
Chris@13 100 * The comments are also available through the "comments" attribute.
Chris@13 101 *
Chris@13 102 * @return Comment[]
Chris@13 103 */
Chris@13 104 public function getComments() : array {
Chris@13 105 return $this->attributes['comments'] ?? [];
Chris@0 106 }
Chris@0 107
Chris@0 108 /**
Chris@0 109 * Gets the doc comment of the node.
Chris@0 110 *
Chris@0 111 * The doc comment has to be the last comment associated with the node.
Chris@0 112 *
Chris@0 113 * @return null|Comment\Doc Doc comment object or null
Chris@0 114 */
Chris@0 115 public function getDocComment() {
Chris@13 116 $comments = $this->getComments();
Chris@0 117 if (!$comments) {
Chris@0 118 return null;
Chris@0 119 }
Chris@0 120
Chris@0 121 $lastComment = $comments[count($comments) - 1];
Chris@0 122 if (!$lastComment instanceof Comment\Doc) {
Chris@0 123 return null;
Chris@0 124 }
Chris@0 125
Chris@0 126 return $lastComment;
Chris@0 127 }
Chris@0 128
Chris@0 129 /**
Chris@0 130 * Sets the doc comment of the node.
Chris@0 131 *
Chris@0 132 * This will either replace an existing doc comment or add it to the comments array.
Chris@0 133 *
Chris@0 134 * @param Comment\Doc $docComment Doc comment to set
Chris@0 135 */
Chris@0 136 public function setDocComment(Comment\Doc $docComment) {
Chris@13 137 $comments = $this->getComments();
Chris@0 138
Chris@0 139 $numComments = count($comments);
Chris@0 140 if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
Chris@0 141 // Replace existing doc comment
Chris@0 142 $comments[$numComments - 1] = $docComment;
Chris@0 143 } else {
Chris@0 144 // Append new comment
Chris@0 145 $comments[] = $docComment;
Chris@0 146 }
Chris@0 147
Chris@0 148 $this->setAttribute('comments', $comments);
Chris@0 149 }
Chris@0 150
Chris@13 151 public function setAttribute(string $key, $value) {
Chris@0 152 $this->attributes[$key] = $value;
Chris@0 153 }
Chris@0 154
Chris@13 155 public function hasAttribute(string $key) : bool {
Chris@0 156 return array_key_exists($key, $this->attributes);
Chris@0 157 }
Chris@0 158
Chris@13 159 public function getAttribute(string $key, $default = null) {
Chris@17 160 if (array_key_exists($key, $this->attributes)) {
Chris@0 161 return $this->attributes[$key];
Chris@0 162 }
Chris@17 163
Chris@17 164 return $default;
Chris@0 165 }
Chris@0 166
Chris@13 167 public function getAttributes() : array {
Chris@0 168 return $this->attributes;
Chris@0 169 }
Chris@0 170
Chris@13 171 public function setAttributes(array $attributes) {
Chris@13 172 $this->attributes = $attributes;
Chris@13 173 }
Chris@13 174
Chris@13 175 /**
Chris@13 176 * @return array
Chris@13 177 */
Chris@13 178 public function jsonSerialize() : array {
Chris@0 179 return ['nodeType' => $this->getType()] + get_object_vars($this);
Chris@0 180 }
Chris@0 181 }