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