annotate vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php @ 0:c75dbcec494b

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