Completed
Push — master ( 8f0d0f...15736a )
by Дмитрий
06:53
created

TestAnnotation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Statement;
4
5
use phpDocumentor\Reflection\DocBlockFactory;
6
use PhpParser\Node\Stmt\ClassMethod;
7
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
8
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
9
use PHPSA\Context;
10
11
class TestAnnotation implements AnalyzerPassInterface
12
{
13
    use DefaultMetadataPassTrait;
14
15
    const DESCRIPTION = 'Checks for use of `@test` when methods name begins with test, since it is unnecessary.';
16
17
    /** @var DocBlockFactory */
18
    protected $docBlockFactory;
19
20
    /**
21
     * Creates a DocBlockFactory
22
     */
23 2
    public function __construct()
24
    {
25 2
        $this->docBlockFactory = DocBlockFactory::createInstance();
26 2
    }
27
28
    /**
29
     * @param ClassMethod $methodStmt
30
     * @param Context $context
31
     * @return bool
32
     */
33 5
    public function pass(ClassMethod $methodStmt, Context $context)
34
    {
35 5
        $functionName = $methodStmt->name;
36 5
        if (!$functionName) {
37
            return false;
38
        }
39
40 5
        if (substr($functionName, 0, 4) !== 'test') {
41 2
            return false;
42
        }
43
44 4
        if ($methodStmt->getDocComment()) {
45 1
            $phpdoc = $this->docBlockFactory->create($methodStmt->getDocComment()->getText());
46
47 1
            if ($phpdoc->hasTag('test')) {
48 1
                $context->notice(
49 1
                    'test.annotation',
50 1
                    'Annotation @test is not needed when the method is prefixed with test.',
51 1
                    $methodStmt
52
                );
53 1
                return true;
54
            }
55
        }
56
57 4
        return false;
58
    }
59
60
    /**
61
     * @return array
62
     */
63 2
    public function getRegister()
64
    {
65
        return [
66 2
            ClassMethod::class
67
        ];
68
    }
69
}
70