Completed
Pull Request — master (#2)
by Adrien
02:16
created

DocBlockReader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 6
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
class DocBlockReader
14
{
15
    /**
16
     * @var string
17
     */
18
    private $comment;
19
20
    public function __construct(ReflectionMethod $method)
21
    {
22
        $this->comment = $method->getDocComment() ?: '';
23
    }
24
25
    /**
26
     * Get the description of a method from the doc block
27
     * @param ReflectionMethod $method
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
28
     * @return string|null
29
     */
30
    public function getMethodDescription(): ?string
31
    {
32
        // Remove the comment markers
33
        $description = preg_replace('~\*/$~', '', $this->comment);
34
        $description = preg_replace('~^\s*(/\*\*|\* ?|\*/)~m', '', $description);
35
36
        // Keep everything before the first annotation
37
        $description = trim(explode('@', $description)[0]);
38
39
        // Drop common "Get" or "Return" in front of comment
40
        $description = ucfirst(preg_replace('~^(set|get|return)s? ~i', '', $description));
41
42
        return $description ?: null;
43
    }
44
45
    /**
46
     * Get the parameter description
47
     * @param ReflectionParameter $param
48
     * @return string|null
49
     */
50
    public function getParameterDescription(ReflectionParameter $param): ?string
51
    {
52
        $name = preg_quote($param->getName());
53
54
        if (preg_match('~@param\h+\H+\h+\$' . $name . '\h+(.*)~', $this->comment, $m)) {
55
            return ucfirst(trim($m[1]));
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * Get the parameter type
63
     * @param ReflectionParameter $param
64
     * @return string|null
65
     */
66
    public function getParameterType(ReflectionParameter $param): ?string
67
    {
68
        $name = preg_quote($param->getName());
69
70
        if (preg_match('~@param\h+(\H+)\h+\$' . $name . '(\h|\n)~', $this->comment, $m)) {
71
            return trim($m[1]);
72
        }
73
74
        return null;
75
    }
76
77
    /**
78
     * Get the return type
79
     * @return string|null
80
     */
81
    public function getReturnType(): ?string
82
    {
83
        if (preg_match('~@return\h+(\H+)(\h|\n)~', $this->comment, $m)) {
84
            return trim($m[1]);
85
        }
86
87
        return null;
88
    }
89
}
90