1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Doctrine; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\Reader; |
8
|
|
|
use ReflectionMethod; |
9
|
|
|
use ReflectionParameter; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A basic doc block reader to extract |
13
|
|
|
*/ |
14
|
|
|
class DocBlockReader |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $comment; |
20
|
|
|
|
21
|
9 |
|
public function __construct(ReflectionMethod $method) |
22
|
|
|
{ |
23
|
9 |
|
$this->comment = $method->getDocComment() ?: ''; |
24
|
9 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the description of a method from the doc block |
28
|
|
|
* @param ReflectionMethod $method |
|
|
|
|
29
|
|
|
* @return string|null |
30
|
|
|
*/ |
31
|
9 |
|
public function getMethodDescription(): ?string |
32
|
|
|
{ |
33
|
|
|
// Remove the comment markers |
34
|
9 |
|
$description = preg_replace('~^\s*(/\*\*|\* ?|\*/)~m', '', $this->comment); |
35
|
|
|
|
36
|
|
|
// Keep everything before the first annotation |
37
|
9 |
|
$description = trim(explode('@', $description)[0]); |
38
|
|
|
|
39
|
|
|
// Drop common "Get" or "Return" in front of comment |
40
|
9 |
|
$description = ucfirst(preg_replace('~^(get|return)s? ~i', '', $description)); |
41
|
|
|
|
42
|
9 |
|
return $description ?: null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the parameter description |
47
|
|
|
* @param ReflectionParameter $param |
48
|
|
|
* @return string|null |
49
|
|
|
*/ |
50
|
6 |
|
public function getParameterDescription(ReflectionParameter $param): ?string |
51
|
|
|
{ |
52
|
6 |
|
$name = preg_quote($param->getName()); |
53
|
|
|
|
54
|
6 |
|
if (preg_match('~@param\h+\H+\h+\$' . $name . '\h+(.*)~', $this->comment, $m)) { |
55
|
1 |
|
return ucfirst(trim($m[1])); |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the parameter description |
63
|
|
|
* @param ReflectionParameter $param |
64
|
|
|
* @return string|null |
65
|
|
|
*/ |
66
|
6 |
|
public function getParameterType(ReflectionParameter $param): ?string |
67
|
|
|
{ |
68
|
6 |
|
$name = preg_quote($param->getName()); |
69
|
|
|
|
70
|
6 |
|
if (preg_match('~@param\h+(\H+)\h+\$' . $name . '(\h|\n)~', $this->comment, $m)) { |
71
|
3 |
|
return trim($m[1]); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
public function getReturnType(): ?string |
78
|
|
|
{ |
79
|
8 |
|
if (preg_match('~@return\h+(\H+)(\h|\n)~', $this->comment, $m)) { |
80
|
3 |
|
return trim($m[1]); |
81
|
|
|
} |
82
|
|
|
|
83
|
5 |
|
return null; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.