Completed
Push — master ( c5f2e3...7cc072 )
by Enrico
9s
created

MethodCannotReturn::pass()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7.0084

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 4
nop 2
dl 0
loc 30
ccs 17
cts 18
cp 0.9444
crap 7.0084
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Statement;
7
8
use PhpParser\Node\Stmt\ClassMethod;
9
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
10
use PHPSA\Analyzer\Helper\ResolveExpressionTrait;
11
use PHPSA\Analyzer\Pass;
12
use PHPSA\Context;
13
14
class MethodCannotReturn implements Pass\AnalyzerPassInterface
15
{
16
    use DefaultMetadataPassTrait;
17
    use ResolveExpressionTrait;
18
19
    const DESCRIPTION = 'Checks for return statements in `__construct` and `__destruct` since they can\'t return anything.';
20
21
    /**
22
     * @param ClassMethod $methodStmt
23
     * @param Context $context
24
     * @return bool
25
     */
26 12
    public function pass(ClassMethod $methodStmt, Context $context)
27
    {
28 12
        if ($methodStmt->stmts === null) {
29 1
            return false;
30
        }
31
        
32 12
        if (count($methodStmt->stmts) == 0) {
33 2
            return false;
34
        }
35
36 11
        $result = false;
37
38 11
        if ($methodStmt->name == '__construct' || $methodStmt->name == '__destruct') {
39 2
            foreach ($this->findReturnStatement($methodStmt->stmts) as $returnStmt) {
40 1
                if (!$returnStmt->expr) {
41
                    continue;
42
                }
43
44 1
                $context->notice(
45 1
                    'return.construct',
46 1
                    sprintf('Method %s cannot return a value.', $methodStmt->name),
47
                    $returnStmt
48 1
                );
49
50 1
                $result = true;
51 2
            }
52 2
        }
53
54 11
        return $result;
55
    }
56
57
    /**
58
     * @return array
59
     */
60 1
    public function getRegister()
61
    {
62
        return [
63
            ClassMethod::class
64 1
        ];
65
    }
66
}
67