comparison vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 * @deprecated
42 */
43 public function setLine($line) {
44 $this->setAttribute('startLine', (int) $line);
45 }
46
47 /**
48 * Gets the doc comment of the node.
49 *
50 * The doc comment has to be the last comment associated with the node.
51 *
52 * @return null|Comment\Doc Doc comment object or null
53 */
54 public function getDocComment() {
55 $comments = $this->getAttribute('comments');
56 if (!$comments) {
57 return null;
58 }
59
60 $lastComment = $comments[count($comments) - 1];
61 if (!$lastComment instanceof Comment\Doc) {
62 return null;
63 }
64
65 return $lastComment;
66 }
67
68 /**
69 * Sets the doc comment of the node.
70 *
71 * This will either replace an existing doc comment or add it to the comments array.
72 *
73 * @param Comment\Doc $docComment Doc comment to set
74 */
75 public function setDocComment(Comment\Doc $docComment) {
76 $comments = $this->getAttribute('comments', []);
77
78 $numComments = count($comments);
79 if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
80 // Replace existing doc comment
81 $comments[$numComments - 1] = $docComment;
82 } else {
83 // Append new comment
84 $comments[] = $docComment;
85 }
86
87 $this->setAttribute('comments', $comments);
88 }
89
90 public function setAttribute($key, $value) {
91 $this->attributes[$key] = $value;
92 }
93
94 public function hasAttribute($key) {
95 return array_key_exists($key, $this->attributes);
96 }
97
98 public function &getAttribute($key, $default = null) {
99 if (!array_key_exists($key, $this->attributes)) {
100 return $default;
101 } else {
102 return $this->attributes[$key];
103 }
104 }
105
106 public function getAttributes() {
107 return $this->attributes;
108 }
109
110 public function jsonSerialize() {
111 return ['nodeType' => $this->getType()] + get_object_vars($this);
112 }
113 }