annotate vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 7a779792577d
children 129ea1e6d783
rev   line source
Chris@13 1 <?php declare(strict_types=1);
Chris@0 2
Chris@0 3 namespace PhpParser;
Chris@0 4
Chris@13 5 use PhpParser\Node;
Chris@13 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@13 16 public function __construct(array $attributes = []) {
Chris@0 17 $this->attributes = $attributes;
Chris@0 18 }
Chris@0 19
Chris@0 20 /**
Chris@13 21 * Gets line the node started in (alias of getStartLine).
Chris@0 22 *
Chris@13 23 * @return int Start line (or -1 if not available)
Chris@0 24 */
Chris@13 25 public function getLine() : int {
Chris@13 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@13 32 * Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
Chris@13 33 *
Chris@13 34 * @return int Start line (or -1 if not available)
Chris@0 35 */
Chris@13 36 public function getStartLine() : int {
Chris@13 37 return $this->attributes['startLine'] ?? -1;
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@13 41 * Gets the line the node ended in.
Chris@0 42 *
Chris@13 43 * Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
Chris@0 44 *
Chris@13 45 * @return int End line (or -1 if not available)
Chris@0 46 */
Chris@13 47 public function getEndLine() : int {
Chris@13 48 return $this->attributes['endLine'] ?? -1;
Chris@13 49 }
Chris@13 50
Chris@13 51 /**
Chris@13 52 * Gets the token offset of the first token that is part of this node.
Chris@13 53 *
Chris@13 54 * The offset is an index into the array returned by Lexer::getTokens().
Chris@13 55 *
Chris@13 56 * Requires the 'startTokenPos' attribute to be enabled in the lexer (DISABLED by default).
Chris@13 57 *
Chris@13 58 * @return int Token start position (or -1 if not available)
Chris@13 59 */
Chris@13 60 public function getStartTokenPos() : int {
Chris@13 61 return $this->attributes['startTokenPos'] ?? -1;
Chris@13 62 }
Chris@13 63
Chris@13 64 /**
Chris@13 65 * Gets the token offset of the last token that is part of this node.
Chris@13 66 *
Chris@13 67 * The offset is an index into the array returned by Lexer::getTokens().
Chris@13 68 *
Chris@13 69 * Requires the 'endTokenPos' attribute to be enabled in the lexer (DISABLED by default).
Chris@13 70 *
Chris@13 71 * @return int Token end position (or -1 if not available)
Chris@13 72 */
Chris@13 73 public function getEndTokenPos() : int {
Chris@13 74 return $this->attributes['endTokenPos'] ?? -1;
Chris@13 75 }
Chris@13 76
Chris@13 77 /**
Chris@13 78 * Gets the file offset of the first character that is part of this node.
Chris@13 79 *
Chris@13 80 * Requires the 'startFilePos' attribute to be enabled in the lexer (DISABLED by default).
Chris@13 81 *
Chris@13 82 * @return int File start position (or -1 if not available)
Chris@13 83 */
Chris@13 84 public function getStartFilePos() : int {
Chris@13 85 return $this->attributes['startFilePos'] ?? -1;
Chris@13 86 }
Chris@13 87
Chris@13 88 /**
Chris@13 89 * Gets the file offset of the last character that is part of this node.
Chris@13 90 *
Chris@13 91 * Requires the 'endFilePos' attribute to be enabled in the lexer (DISABLED by default).
Chris@13 92 *
Chris@13 93 * @return int File end position (or -1 if not available)
Chris@13 94 */
Chris@13 95 public function getEndFilePos() : int {
Chris@13 96 return $this->attributes['endFilePos'] ?? -1;
Chris@13 97 }
Chris@13 98
Chris@13 99 /**
Chris@13 100 * Gets all comments directly preceding this node.
Chris@13 101 *
Chris@13 102 * The comments are also available through the "comments" attribute.
Chris@13 103 *
Chris@13 104 * @return Comment[]
Chris@13 105 */
Chris@13 106 public function getComments() : array {
Chris@13 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@13 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@13 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@13 153 public function setAttribute(string $key, $value) {
Chris@0 154 $this->attributes[$key] = $value;
Chris@0 155 }
Chris@0 156
Chris@13 157 public function hasAttribute(string $key) : bool {
Chris@0 158 return array_key_exists($key, $this->attributes);
Chris@0 159 }
Chris@0 160
Chris@13 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@13 169 public function getAttributes() : array {
Chris@0 170 return $this->attributes;
Chris@0 171 }
Chris@0 172
Chris@13 173 public function setAttributes(array $attributes) {
Chris@13 174 $this->attributes = $attributes;
Chris@13 175 }
Chris@13 176
Chris@13 177 /**
Chris@13 178 * @return array
Chris@13 179 */
Chris@13 180 public function jsonSerialize() : array {
Chris@0 181 return ['nodeType' => $this->getType()] + get_object_vars($this);
Chris@0 182 }
Chris@0 183 }