1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Reflection; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class CommentParser |
8
|
|
|
{ |
9
|
|
|
private $comment; |
10
|
|
|
|
11
|
|
|
public function __construct(string $docBlock) |
12
|
|
|
{ |
13
|
|
|
$lines = $this->getAllLinesWithoutStars($docBlock); |
14
|
|
|
|
15
|
|
|
// First, let's go to the first Annotation. |
16
|
|
|
// Anything before is the pure comment. |
17
|
|
|
|
18
|
|
|
$oneAnnotationFound = false; |
19
|
|
|
// Is the line an annotation? Let's test this with a regexp. |
20
|
|
|
foreach ($lines as $line) { |
21
|
|
|
if ($oneAnnotationFound === false && preg_match("/^[@][a-zA-Z]/", $line) === 0) { |
22
|
|
|
$this->comment .= $line."\n"; |
23
|
|
|
} else { |
24
|
|
|
//$this->parseAnnotation($line); |
|
|
|
|
25
|
|
|
$oneAnnotationFound = true; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$this->comment = rtrim($this->comment, "\n"); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $docComment |
34
|
|
|
* @return string[] |
35
|
|
|
*/ |
36
|
|
|
private function getAllLinesWithoutStars(string $docComment): array |
37
|
|
|
{ |
38
|
|
|
if (strpos($docComment, '/**') === 0) { |
39
|
|
|
$docComment = substr($docComment, 3); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Let's remove all the \r... |
43
|
|
|
$docComment = str_replace("\r", '', $docComment); |
44
|
|
|
|
45
|
|
|
$commentLines = explode("\n", $docComment); |
46
|
|
|
$commentLinesWithoutStars = array_map(function(string $line) { |
47
|
|
|
return ltrim($line, " \t*"); |
48
|
|
|
}, $commentLines); |
49
|
|
|
|
50
|
|
|
// Let's remove the trailing /: |
51
|
|
|
$lastComment = $commentLinesWithoutStars[count($commentLinesWithoutStars)-1]; |
52
|
|
|
$commentLinesWithoutStars[count($commentLinesWithoutStars)-1] = substr($lastComment, 0, strlen($lastComment)-1); |
53
|
|
|
|
54
|
|
|
if ($commentLinesWithoutStars[count($commentLinesWithoutStars)-1] == "") { |
55
|
|
|
array_pop($commentLinesWithoutStars); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (isset($commentLinesWithoutStars[0]) && $commentLinesWithoutStars[0] === '') { |
59
|
|
|
$commentLinesWithoutStars = array_slice($commentLinesWithoutStars, 1); |
60
|
|
|
} |
61
|
|
|
return $commentLinesWithoutStars; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the comment out of a Docblock, ignoring all annotations |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getComment(): string |
70
|
|
|
{ |
71
|
|
|
return $this->comment; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.