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

ReturnAndYieldInOneMethod::pass()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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