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

TestAnnotation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRegister() 0 6 1
B pass() 0 26 5
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