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