Completed
Pull Request — master (#250)
by Enrico
08:17
created

MissingBody::pass()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0729

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 24
ccs 12
cts 14
cp 0.8571
crap 5.0729
rs 8.5125
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Statement;
4
5
use PhpParser\Node\Stmt;
6
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
7
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
8
use PHPSA\Context;
9
10
class MissingBody implements AnalyzerPassInterface
11
{
12
    use DefaultMetadataPassTrait;
13
14
    const DESCRIPTION = 'Checks that statements that define a block of statements are not empty.';
15
    
16
    /**
17
     * @param Stmt $stmt
18
     * @param Context $context
19
     * @return bool
20
     */
21 13
    public function pass(Stmt $stmt, Context $context)
22
    {
23 13
        if ($stmt instanceof Stmt\ClassMethod && $stmt->isAbstract()) { // abstract classes are ok
24 1
            return false;
25
        }
26
27 13
        if ($stmt instanceof Stmt\Switch_) {
28
            $counting = $stmt->cases;
29
        } else {
30 13
            $counting = $stmt->stmts;
0 ignored issues
show
Bug introduced by
The property stmts does not seem to exist in PhpParser\Node\Stmt.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
31
        }
32
33 13
        if (count($counting) === 0) {
34 9
            $context->notice(
35 9
                'missing_body',
36 9
                'Missing Body',
37
                $stmt
38 9
            );
39
40 9
            return true;
41
        }
42
        
43 7
        return false;
44
    }
45
46
    /**
47
     * @return array
48
     */
49 1
    public function getRegister()
50
    {
51
        return [
52 1
            Stmt\Function_::class,
53 1
            Stmt\ClassMethod::class,
54 1
            Stmt\For_::class,
55 1
            Stmt\Foreach_::class,
56 1
            Stmt\While_::class,
57 1
            Stmt\Do_::class,
58 1
            Stmt\If_::class,
59 1
            Stmt\ElseIf_::class,
60 1
            Stmt\Else_::class,
61 1
            Stmt\Switch_::class,
62 1
            Stmt\TryCatch::class,
63 1
            Stmt\Catch_::class,
64 1
        ];
65
    }
66
}
67