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

MethodCannotReturn   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 21.74%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 49
ccs 5
cts 23
cp 0.2174
rs 10
wmc 8
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
C pass() 0 28 7
A getConfiguration() 0 9 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 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