Completed
Pull Request — master (#139)
by Kévin
03:18
created

MethodCannotReturn   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 94.44%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 47
ccs 17
cts 18
cp 0.9444
rs 10
c 3
b 0
f 1
wmc 7
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B pass() 0 26 6
A getRegister() 0 6 1
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\ConfigurablePassTrait;
10
use PHPSA\Analyzer\Helper\ResolveExpressionTrait;
11
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
12
use PHPSA\Analyzer\Pass\ConfigurablePassInterface;
13
use PHPSA\Context;
14
15
class MethodCannotReturn implements ConfigurablePassInterface, AnalyzerPassInterface
16
{
17
    use ConfigurablePassTrait;
18
    use ResolveExpressionTrait;
19
20
    /**
21
     * @param ClassMethod $methodStmt
22
     * @param Context $context
23
     * @return bool
24
     */
25 19
    public function pass(ClassMethod $methodStmt, Context $context)
26
    {
27 19
        if (count($methodStmt->stmts) == 0) {
28 1
            return false;
29
        }
30
31 19
        $result = false;
32
33 19
        if ($methodStmt->name == '__construct' || $methodStmt->name == '__destruct') {
34 2
            foreach ($this->findReturnStatement($methodStmt->stmts) as $returnStmt) {
35 1
                if (!$returnStmt->expr) {
36
                    continue;
37
                }
38
39 1
                $context->notice(
40 1
                    'return.construct',
41 1
                    sprintf('Method %s cannot return a value.', $methodStmt->name),
42
                    $returnStmt
43 1
                );
44
45 1
                $result = true;
46 2
            }
47 2
        }
48
49 19
        return $result;
50
    }
51
52
    /**
53
     * @return array
54
     */
55 1
    public function getRegister()
56
    {
57
        return [
58
            \PhpParser\Node\Stmt\ClassMethod::class
59 1
        ];
60
    }
61
}
62