Completed
Pull Request — master (#237)
by
unknown
03:20
created

ReturnAndYieldInOneMethod   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 50
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 0
wmc 6
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 9 3
A returnStatementExists() 0 4 1
A yieldStatementExists() 0 4 1
A getRegister() 0 6 1
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Statement;
4
5
use PhpParser\Node\Expr\Yield_;
6
use PhpParser\Node\Stmt;
7
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
8
use PHPSA\Analyzer\Helper\ResolveExpressionTrait;
9
use PHPSA\Analyzer\Pass;
10
use PHPSA\Context;
11
12
class ReturnAndYieldInOneMethod implements Pass\AnalyzerPassInterface
13
{
14
    use DefaultMetadataPassTrait,
15
        ResolveExpressionTrait;
16
17
    /**
18
     * @param Stmt $stmt
19
     * @param Context $context
20
     * @return bool
21
     */
22 40
    public function pass(Stmt $stmt, Context $context)
23
    {
24 40
        if ($this->returnStatementExists($stmt) && $this->yieldStatementExists($stmt)) {
25 1
            $context->notice('return_and_yield_in_one_method', 'Do not use return and yield in a one method', $stmt);
26 1
            return true;
27
        }
28
29 40
        return false;
30
    }
31
32
    /**
33
     * @param Stmt $node
34
     *
35
     * @return bool
36
     */
37 40
    private function returnStatementExists(Stmt $node)
38
    {
39 40
        return (bool)$this->findReturnStatement([$node])->current();
40
    }
41
42
    /**
43
     * @param Stmt $node
44
     *
45
     * @return bool
46
     */
47 31
    private function yieldStatementExists(Stmt $node)
48
    {
49 31
        return (bool)$this->findStatement([$node], Yield_::class)->current();
50
    }
51
52
    /**
53
     * @return array
54
     */
55 1
    public function getRegister()
56
    {
57
        return [
58 1
            Stmt\ClassMethod::class,
59 1
        ];
60
    }
61
}
62