Version 1
[yaffs-website] / vendor / nikic / php-parser / lib / PhpParser / NodeAbstract.php
1 <?php
2
3 namespace PhpParser;
4
5 abstract class NodeAbstract implements Node, \JsonSerializable
6 {
7     protected $attributes;
8
9     /**
10      * Creates a Node.
11      *
12      * @param array $attributes Array of attributes
13      */
14     public function __construct(array $attributes = array()) {
15         $this->attributes = $attributes;
16     }
17
18     /**
19      * Gets the type of the node.
20      *
21      * @return string Type of the node
22      */
23     public function getType() {
24         return strtr(substr(rtrim(get_class($this), '_'), 15), '\\', '_');
25     }
26
27     /**
28      * Gets line the node started in.
29      *
30      * @return int Line
31      */
32     public function getLine() {
33         return $this->getAttribute('startLine', -1);
34     }
35
36     /**
37      * Sets line the node started in.
38      *
39      * @param int $line Line
40      */
41     public function setLine($line) {
42         $this->setAttribute('startLine', (int) $line);
43     }
44
45     /**
46      * Gets the doc comment of the node.
47      *
48      * The doc comment has to be the last comment associated with the node.
49      *
50      * @return null|Comment\Doc Doc comment object or null
51      */
52     public function getDocComment() {
53         $comments = $this->getAttribute('comments');
54         if (!$comments) {
55             return null;
56         }
57
58         $lastComment = $comments[count($comments) - 1];
59         if (!$lastComment instanceof Comment\Doc) {
60             return null;
61         }
62
63         return $lastComment;
64     }
65
66     /**
67      * Sets the doc comment of the node.
68      *
69      * This will either replace an existing doc comment or add it to the comments array.
70      *
71      * @param Comment\Doc $docComment Doc comment to set
72      */
73     public function setDocComment(Comment\Doc $docComment) {
74         $comments = $this->getAttribute('comments', []);
75
76         $numComments = count($comments);
77         if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
78             // Replace existing doc comment
79             $comments[$numComments - 1] = $docComment;
80         } else {
81             // Append new comment
82             $comments[] = $docComment;
83         }
84
85         $this->setAttribute('comments', $comments);
86     }
87
88     public function setAttribute($key, $value) {
89         $this->attributes[$key] = $value;
90     }
91
92     public function hasAttribute($key) {
93         return array_key_exists($key, $this->attributes);
94     }
95
96     public function &getAttribute($key, $default = null) {
97         if (!array_key_exists($key, $this->attributes)) {
98             return $default;
99         } else {
100             return $this->attributes[$key];
101         }
102     }
103
104     public function getAttributes() {
105         return $this->attributes;
106     }
107
108     public function jsonSerialize() {
109         return ['nodeType' => $this->getType()] + get_object_vars($this);
110     }
111 }