Failed Conditions
Pull Request — master (#62)
by Adrien
06:30 queued 04:20
created

DocBlockReader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 19
c 1
b 0
f 0
dl 0
loc 65
ccs 23
cts 23
cp 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine;
6
7
use ReflectionMethod;
8
use ReflectionParameter;
9
10
/**
11
 * A basic doc block reader to extract.
12
 */
13
final class DocBlockReader
14
{
15
    private readonly string $comment;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 15 at column 21
Loading history...
16
17 39
    public function __construct(ReflectionMethod $method)
18
    {
19 39
        $this->comment = $method->getDocComment() ?: '';
20
    }
21
22
    /**
23
     * Get the description of a method from the doc block.
24
     */
25 24
    public function getMethodDescription(): ?string
26
    {
27
        // Remove the comment markers
28 24
        $description = preg_replace('~\*/$~', '', $this->comment);
29 24
        $description = preg_replace('~^\s*(/\*\*|\* ?|\*/)~m', '', $description);
30
31
        // Keep everything before the first annotation
32 24
        $description = trim(explode('@', $description ?? '')[0]);
33
34
        // Drop common "Get" or "Return" in front of comment
35 24
        $description = ucfirst(preg_replace('~^(set|get|return)s? ~i', '', $description) ?? '');
36
37 24
        return $description ?: null;
38
    }
39
40
    /**
41
     * Get the parameter description.
42
     */
43 15
    public function getParameterDescription(ReflectionParameter $param): ?string
44
    {
45 15
        $name = preg_quote($param->getName());
46
47 15
        if (preg_match('~@param\h+\H+\h+\$' . $name . '\h+(.*)~', $this->comment, $m)) {
48 3
            return ucfirst(trim($m[1]));
49
        }
50
51 14
        return null;
52
    }
53
54
    /**
55
     * Get the parameter type.
56
     */
57 22
    public function getParameterType(ReflectionParameter $param): ?string
58
    {
59 22
        $name = preg_quote($param->getName());
60
61 22
        if (preg_match('~@param\h+(\H+)\h+\$' . $name . '(\h|\n)~', $this->comment, $m)) {
62 11
            return trim($m[1]);
63
        }
64
65 17
        return null;
66
    }
67
68
    /**
69
     * Get the return type.
70
     */
71 13
    public function getReturnType(): ?string
72
    {
73 13
        if (preg_match('~@return\h+(\H+)(\h|\n)~', $this->comment, $m)) {
74 7
            return trim($m[1]);
75
        }
76
77 11
        return null;
78
    }
79
}
80