ReturnAndYieldInOneMethod::pass()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 20
ccs 0
cts 11
cp 0
crap 30
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Statement;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Stmt\ClassMethod;
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
    const DESCRIPTION = 'Checks for using return and yield statements in a one method and discourages it.';
15
16
    use DefaultMetadataPassTrait;
17
    use ResolveExpressionTrait;
18
19
    /**
20
     * @param ClassMethod $func
21
     * @param Context $context
22
     * @return bool
23
     */
24
    public function pass(ClassMethod $func, Context $context)
25
    {
26
        $stmts = $func->getStmts();
27
        if ($stmts === null) {
28
            return false;
29
        }
30
31
        $yieldExists = \PHPSA\generatorHasValue($this->findNode($stmts, Node\Expr\Yield_::class));
32
        if (!$yieldExists) {
33
            // YieldFrom is another expression
34
            $yieldExists = \PHPSA\generatorHasValue($this->findNode($stmts, Node\Expr\YieldFrom::class));
35
        }
36
37
        if ($yieldExists && \PHPSA\generatorHasValue($this->findNode($stmts, Node\Stmt\Return_::class))) {
38
            $context->notice('return_and_yield_in_one_method', 'Do not use return and yield in a one method', $func);
39
            return true;
40
        }
41
42
        return false;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getRegister()
49
    {
50
        return [
51
            ClassMethod::class,
52
        ];
53
    }
54
}
55