Completed
Push — master ( b1b3d7...ed93a7 )
by Дмитрий
03:53
created

MethodCannotReturn::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 2
rs 9.6666
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 PhpParser\Node\Stmt\Return_;
10
use PHPSA\Analyzer\Pass\ConfigurablePassInterface;
11
use PHPSA\Context;
12
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
13
14
class MethodCannotReturn implements ConfigurablePassInterface
15
{
16
    /**
17
     * @param ClassMethod $methodStmt
18
     * @param Context $context
19
     * @return bool
20
     */
21 6
    public function pass(ClassMethod $methodStmt, Context $context)
22
    {
23 6
        if (count($methodStmt->stmts) == 0) {
24
            return false;
25
        }
26
27 6
        $result = false;
28
29 6
        if ($methodStmt->name == '__construct' || $methodStmt->name == '__destruct') {
30
            foreach ($methodStmt->stmts as $stmt) {
31
                if ($stmt instanceof Return_) {
32
                    if (!$stmt->expr) {
33
                        continue;
34
                    }
35
36
                    $context->notice(
37
                        'return.construct',
38
                        sprintf('Method %s cannot return a value.', $methodStmt->name),
39
                        $stmt
40
                    );
41
42
                    $result = true;
43
                }
44
            }
45
        }
46
47 6
        return $result;
48
    }
49
50
    /**
51
     * @return TreeBuilder
52
     */
53
    public function getConfiguration()
54
    {
55
        $treeBuilder = new TreeBuilder();
56
        $treeBuilder->root('method_cannot_return')
57
            ->canBeDisabled()
58
        ;
59
60
        return $treeBuilder;
61
    }
62
}
63