1: <?php
2: namespace Pharborist;
3:
4: use Pharborist\Namespaces\NameNode;
5: use Pharborist\Namespaces\NamespaceNode;
6: use phpDocumentor\Reflection\DocBlock;
7:
8: 9: 10:
11: class DocCommentNode extends CommentNode {
12: 13: 14: 15: 16:
17: private $docBlock;
18:
19: 20: 21: 22: 23: 24: 25: 26:
27: public static function create($comment) {
28: $comment = trim($comment);
29: $lines = array_map('trim', explode("\n", $comment));
30: $text = "/**\n";
31: foreach ($lines as $i => $line) {
32: $text .= ' * ' . $line . "\n";
33: }
34: $text .= ' */';
35: return new DocCommentNode(T_DOC_COMMENT, $text);
36: }
37:
38: 39: 40: 41: 42: 43: 44:
45: public function setIndent($indent) {
46: $lines = explode("\n", $this->text);
47: if (count($lines) === 1) {
48: return $this;
49: }
50: $comment = '';
51: $last_index = count($lines) - 1;
52: foreach ($lines as $i => $line) {
53: if ($i === 0) {
54: $comment .= trim($line) . "\n";
55: }
56: elseif ($i === $last_index) {
57: $comment .= $indent . ' ' . trim($line);
58: }
59: else {
60: $comment .= $indent . ' ' . trim($line) . "\n";
61: }
62: }
63: $this->setText($comment);
64: return $this;
65: }
66:
67: public function setText($text) {
68: parent::setText($text);
69: $this->docBlock = NULL;
70: }
71:
72: 73: 74: 75: 76: 77:
78: public function getDocBlock() {
79: if ($this->docBlock === NULL) {
80: $namespace = '\\';
81: $aliases = array();
82:
83: $namespace_node = $this->closest(Filter::isInstanceOf('\Pharborist\Namespaces\NamespaceNode'));
84: if ($namespace_node !== NULL) {
85: $namespace = $namespace_node->getName()->getAbsolutePath();
86: $aliases = $namespace_node->getClassAliases();
87: } else {
88:
89: $root_node = $this->closest(Filter::isInstanceOf('\Pharborist\RootNode'));
90: if ($root_node !== NULL) {
91: $aliases = $root_node->getClassAliases();
92: }
93: }
94: $context = new DocBlock\Context($namespace, $aliases);
95: $this->docBlock = new DocBlock($this->text, $context);
96: }
97: return $this->docBlock;
98: }
99:
100: 101: 102: 103: 104: 105:
106: public function getShortDescription() {
107: return $this->getDocBlock()->getShortDescription();
108: }
109:
110: 111: 112: 113: 114: 115:
116: public function getLongDescription() {
117: return (string) $this->getDocBlock()->getLongDescription();
118: }
119:
120: 121: 122: 123: 124: 125:
126: public function getReturn() {
127: $return_tags = $this->getDocBlock()->getTagsByName('return');
128: return end($return_tags);
129: }
130:
131: 132: 133: 134: 135: 136:
137: public function getParameters() {
138: return $this->getDocBlock()->getTagsByName('param');
139: }
140:
141: 142: 143: 144: 145: 146:
147: public function getParametersByName() {
148: $param_tags = $this->getDocBlock()->getTagsByName('param');
149: $parameters = array();
150:
151: foreach ($param_tags as $param_tag) {
152: $name = $param_tag->getVariableName();
153: $parameters[$name] = $param_tag;
154: }
155: return $parameters;
156: }
157:
158: 159: 160: 161: 162: 163: 164: 165: 166:
167: public function getParameter($parameterName) {
168: $param_tags = $this->getDocBlock()->getTagsByName('param');
169:
170: foreach ($param_tags as $param_tag) {
171: if ($param_tag->getVariableName() === $parameterName) {
172: return $param_tag;
173: }
174: }
175: return NULL;
176: }
177: }
178: