Completed
Push — master ( 1f691f...f49b53 )
by Enrico
04:10
created

TestAnnotation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 73.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
ccs 17
cts 23
cp 0.7391
rs 10
wmc 7
lcom 0
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
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 PhpParser\Node\Stmt\ClassMethod;
6
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
7
use PHPSA\Analyzer\Pass\ConfigurablePassInterface;
8
use PHPSA\Context;
9
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
10
use phpDocumentor\Reflection\DocBlock;
11
12
class TestAnnotation implements ConfigurablePassInterface, AnalyzerPassInterface
13
{
14
    /**
15
     * @param ClassMethod $methodStmt
16
     * @param Context $context
17
     * @return bool
18
     */
19 22
    public function pass(ClassMethod $methodStmt, Context $context)
20
    {
21 22
        $functionName = $methodStmt->name;
22 22
        if (!$functionName) {
23
            return false;
24
        }
25
26 22
        if (substr($functionName, 0, 4) !== 'test') {
27 9
            return false;
28
        }
29
30 15
        if ($methodStmt->getDocComment()) {
31 11
            $phpdoc = new DocBlock($methodStmt->getDocComment()->getText());
32
33 11
            if ($phpdoc->hasTag('test')) {
34 1
                $context->notice(
35 1
                    'test.annotation',
36 1
                    'Annotation @test is not needed when the method is prefixed with test.',
37
                    $methodStmt
38 1
                );
39 1
                return true;
40
            }
41 11
        }
42 15
        return false;
43
    }
44
45
    /**
46
     * @return TreeBuilder
47
     */
48
    public function getConfiguration()
49
    {
50
        $treeBuilder = new TreeBuilder();
51
        $treeBuilder->root('test_annotation')
52
            ->canBeDisabled()
53
        ;
54
55
        return $treeBuilder;
56
    }
57
58
    /**
59
     * @return array
60
     */
61 1
    public function getRegister()
62
    {
63
        return [
64
            ClassMethod::class
65 1
        ];
66
    }
67
}
68