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