Completed
Pull Request — master (#263)
by Дмитрий
09:04
created

ReturnAndYieldInOneMethod   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 15 4
A getRegister() 0 6 1
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Statement;
4
5
use PhpParser\Node\Stmt;
6
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
7
use PHPSA\Analyzer\Helper\ResolveExpressionTrait;
8
use PHPSA\Analyzer\Pass;
9
use PhpParser\Node\Expr;
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 5
    public function pass(Stmt $stmt, Context $context)
23
    {
24 5
        $yieldExists = $this->findYieldExpression([$stmt])->current();
25 5
        if (!$yieldExists) {
26
            // YieldFrom is another expression
27 5
            $yieldExists = $this->findNode([$stmt], Expr\YieldFrom::class)->current();
28 5
        }
29
30 5
        if ($yieldExists && $this->findReturnStatement([$stmt])->current()) {
31 1
            $context->notice('return_and_yield_in_one_method', 'Do not use return and yield in a one method', $stmt);
32 1
            return true;
33
        }
34
35 5
        return false;
36
    }
37
38
    /**
39
     * @return array
40
     */
41 1
    public function getRegister()
42
    {
43
        return [
44 1
            Stmt\ClassMethod::class,
45 1
        ];
46
    }
47
}
48