-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathInlineComment.php
82 lines (66 loc) · 2.56 KB
/
InlineComment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php declare(strict_types = 1);
namespace TheSeer\phpDox\Collector;
use TheSeer\fDOM\fDOMDocument;
use TheSeer\fDOM\fDOMElement;
class InlineComment {
public const XMLNS = 'http://xml.phpdox.net/src';
private $fragment;
private $startLine;
public function __construct($line, $comment) {
$this->startLine = $line;
$this->fragment = (new fDOMDocument())->createDocumentFragment();
$this->parse(
$this->normalizeSplit($comment)
);
}
public function getCount() {
return $this->fragment->childNodes->length;
}
public function asDom(fDOMDocument $dom) {
return $dom->importNode($this->fragment, true);
}
private function normalizeSplit($comment) {
$comment = \str_replace(["\r\n", "\n"], "\n", $comment);
$res = [];
foreach (\explode("\n", \trim($comment)) as $line) {
$line = \trim($line);
\preg_match('=//(.*)$|/\*{1,}(.*)\*/$|/\*{1,}(.*)$|^(.*)\*/$|#(.*)$|\*{1}(.*)$|^(.*)$=', $line, $matches);
$normalized = \trim(\end($matches));
if ($normalized != '') {
$res[] = $normalized;
}
}
return $res;
}
private function parse(array $comments): void {
foreach ($comments as $pos => $comment) {
\preg_match('=^@{0,1}(todo|var|fixme):{0,1}(.*)=i', $comment, $matches);
if (\count($matches) != 0) {
switch (\mb_strtolower($matches[1])) {
case 'var':
{
// we ignore @var comments as they are IDE support only
continue 2;
}
case 'fixme':
case 'todo':
{
$node = $this->fragment->appendChild(
$this->fragment->ownerDocument->createElementNS(self::XMLNS, \mb_strtolower($matches[1]))
);
$node->setAttribute('value', \trim($matches[2]));
break;
}
}
} else {
$node = $this->fragment->appendChild(
$this->fragment->ownerDocument->createElementNS(self::XMLNS, 'comment')
);
$node->setAttribute('value', \trim($comment));
}
if (isset($node) && $node instanceof fDOMElement) {
$node->setAttribute('line', $this->startLine + $pos);
}
}
}
}