Completed
Push — master ( efd13a...5f2691 )
by Kévin
8s
created

TestAnnotation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 65
ccs 20
cts 26
cp 0.7692
rs 10
c 1
b 0
f 0
wmc 8
lcom 1
cbo 7

4 Methods

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