annotate vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
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 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@0 14 public function __construct(array $attributes = []) {
Chris@0 15 $this->attributes = $attributes;
Chris@0 16 }
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Gets line the node started in (alias of getStartLine).
Chris@0 20 *
Chris@0 21 * @return int Start line (or -1 if not available)
Chris@0 22 */
Chris@0 23 public function getLine() : int {
Chris@0 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@0 30 * Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
Chris@0 31 *
Chris@0 32 * @return int Start line (or -1 if not available)
Chris@0 33 */
Chris@0 34 public function getStartLine() : int {
Chris@0 35 return $this->attributes['startLine'] ?? -1;
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Gets the line the node ended in.
Chris@0 40 *
Chris@0 41 * Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
Chris@0 42 *
Chris@0 43 * @return int End line (or -1 if not available)
Chris@0 44 */
Chris@0 45 public function getEndLine() : int {
Chris@0 46 return $this->attributes['endLine'] ?? -1;
Chris@0 47 }
Chris@0 48
Chris@0 49 /**
Chris@0 50 * Gets the token offset of the first token that is part of this node.
Chris@0 51 *
Chris@0 52 * The offset is an index into the array returned by Lexer::getTokens().
Chris@0 53 *
Chris@0 54 * Requires the 'startTokenPos' attribute to be enabled in the lexer (DISABLED by default).
Chris@0 55 *
Chris@0 56 * @return int Token start position (or -1 if not available)
Chris@0 57 */
Chris@0 58 public function getStartTokenPos() : int {
Chris@0 59 return $this->attributes['startTokenPos'] ?? -1;
Chris@0 60 }
Chris@0 61
Chris@0 62 /**
Chris@0 63 * Gets the token offset of the last token that is part of this node.
Chris@0 64 *
Chris@0 65 * The offset is an index into the array returned by Lexer::getTokens().
Chris@0 66 *
Chris@0 67 * Requires the 'endTokenPos' attribute to be enabled in the lexer (DISABLED by default).
Chris@0 68 *
Chris@0 69 * @return int Token end position (or -1 if not available)
Chris@0 70 */
Chris@0 71 public function getEndTokenPos() : int {
Chris@0 72 return $this->attributes['endTokenPos'] ?? -1;
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * Gets the file offset of the first character that is part of this node.
Chris@0 77 *
Chris@0 78 * Requires the 'startFilePos' attribute to be enabled in the lexer (DISABLED by default).
Chris@0 79 *
Chris@0 80 * @return int File start position (or -1 if not available)
Chris@0 81 */
Chris@0 82 public function getStartFilePos() : int {
Chris@0 83 return $this->attributes['startFilePos'] ?? -1;
Chris@0 84 }
Chris@0 85
Chris@0 86 /**
Chris@0 87 * Gets the file offset of the last character that is part of this node.
Chris@0 88 *
Chris@0 89 * Requires the 'endFilePos' attribute to be enabled in the lexer (DISABLED by default).
Chris@0 90 *
Chris@0 91 * @return int File end position (or -1 if not available)
Chris@0 92 */
Chris@0 93 public function getEndFilePos() : int {
Chris@0 94 return $this->attributes['endFilePos'] ?? -1;
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Gets all comments directly preceding this node.
Chris@0 99 *
Chris@0 100 * The comments are also available through the "comments" attribute.
Chris@0 101 *
Chris@0 102 * @return Comment[]
Chris@0 103 */
Chris@0 104 public function getComments() : array {
Chris@0 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@0 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@0 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@0 151 public function setAttribute(string $key, $value) {
Chris@0 152 $this->attributes[$key] = $value;
Chris@0 153 }
Chris@0 154
Chris@0 155 public function hasAttribute(string $key) : bool {
Chris@0 156 return array_key_exists($key, $this->attributes);
Chris@0 157 }
Chris@0 158
Chris@0 159 public function getAttribute(string $key, $default = null) {
Chris@4 160 if (array_key_exists($key, $this->attributes)) {
Chris@0 161 return $this->attributes[$key];
Chris@0 162 }
Chris@4 163
Chris@4 164 return $default;
Chris@0 165 }
Chris@0 166
Chris@0 167 public function getAttributes() : array {
Chris@0 168 return $this->attributes;
Chris@0 169 }
Chris@0 170
Chris@0 171 public function setAttributes(array $attributes) {
Chris@0 172 $this->attributes = $attributes;
Chris@0 173 }
Chris@0 174
Chris@0 175 /**
Chris@0 176 * @return array
Chris@0 177 */
Chris@0 178 public function jsonSerialize() : array {
Chris@0 179 return ['nodeType' => $this->getType()] + get_object_vars($this);
Chris@0 180 }
Chris@0 181 }