ReturnAndYieldInOneMethod   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 20 5
A getRegister() 0 6 1
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