Completed
Push — master ( d82f0a...49e44e )
by Дмитрий
03:44
created

MethodCannotReturn   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 30.43%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 59
ccs 7
cts 23
cp 0.3043
rs 10
wmc 8
lcom 0
cbo 5

3 Methods

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