1: <?php
2: namespace Pharborist;
3:
4: trait DocCommentTrait {
5: 6: 7:
8: protected $docComment;
9:
10: 11: 12: 13: 14:
15: public function getIndent() {
16:
17: $whitespace_token = $this->previousToken();
18: if ($whitespace_token->getType() !== T_WHITESPACE) {
19: return '';
20: }
21: $nl = FormatterFactory::getDefaultFormatter()->getConfig('nl');
22: $lines = explode($nl, $whitespace_token->getText());
23: $last_line = end($lines);
24: return $last_line;
25: }
26:
27: 28: 29:
30: public function getDocComment() {
31: return $this->docComment;
32: }
33:
34: 35: 36: 37:
38: public function setDocComment(DocCommentNode $comment) {
39: if (isset($this->docComment)) {
40: $this->docComment->replaceWith($comment);
41: }
42: else {
43: $indent = $this->getIndent();
44: $comment->setIndent($indent);
45: $nl = FormatterFactory::getDefaultFormatter()->getConfig('nl');
46:
47: $this->firstChild()->before([
48: $comment,
49: WhitespaceNode::create($nl . $indent),
50: ]);
51: }
52: return $this;
53: }
54: }
55: