Completed
Push — master ( efd13a...5f2691 )
by Kévin
8s
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\Pass\AnalyzerPassInterface;
8
use PHPSA\Analyzer\Pass\ConfigurablePassInterface;
9
use PHPSA\Context;
10
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
11
12
class TestAnnotation implements ConfigurablePassInterface, AnalyzerPassInterface
13
{
14
15
    /** @var DocBlockFactory */
16
    protected $docBlockFactory;
17
18 1
    public function __construct()
19
    {
20 1
        $this->docBlockFactory = DocBlockFactory::createInstance();
21 1
    }
22
23
    /**
24
     * @param ClassMethod $methodStmt
25
     * @param Context $context
26
     * @return bool
27
     */
28 24
    public function pass(ClassMethod $methodStmt, Context $context)
29
    {
30 24
        $functionName = $methodStmt->name;
31 24
        if (!$functionName) {
32
            return false;
33
        }
34
35 24
        if (substr($functionName, 0, 4) !== 'test') {
36 11
            return false;
37
        }
38
39 15
        if ($methodStmt->getDocComment()) {
40 11
            $phpdoc = $this->docBlockFactory->create($methodStmt->getDocComment()->getText());
41
42 11
            if ($phpdoc->hasTag('test')) {
43 1
                $context->notice(
44 1
                    'test.annotation',
45 1
                    'Annotation @test is not needed when the method is prefixed with test.',
46
                    $methodStmt
47 1
                );
48 1
                return true;
49
            }
50 11
        }
51 15
        return false;
52
    }
53
54
    /**
55
     * @return TreeBuilder
56
     */
57
    public function getConfiguration()
58
    {
59
        $treeBuilder = new TreeBuilder();
60
        $treeBuilder->root('test_annotation')
61
            ->canBeDisabled()
62
        ;
63
64
        return $treeBuilder;
65
    }
66
67
    /**
68
     * @return array
69
     */
70 1
    public function getRegister()
71
    {
72
        return [
73
            ClassMethod::class
74 1
        ];
75
    }
76
}
77