Completed
Push — master ( 9e11ce...2484f0 )
by Adrien
01:55
created

DocBlockReader::getParameterDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
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
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...
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