Completed
Pull Request — master (#263)
by Дмитрий
20:51 queued 06:41
created

ReturnAndYieldInOneMethod   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 38
ccs 12
cts 12
cp 1
rs 10
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
    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 Stmt $stmt
21
     * @param Context $context
22
     * @return bool
23
     */
24 5
    public function pass(Stmt $stmt, Context $context)
25
    {
26 5
        $yieldExists = $this->findYieldExpression([$stmt])->current();
27 5
        if (!$yieldExists) {
28
            // YieldFrom is another expression
29 5
            $yieldExists = $this->findNode([$stmt], Expr\YieldFrom::class)->current();
30 5
        }
31
32 5
        if ($yieldExists && $this->findReturnStatement([$stmt])->current()) {
33 1
            $context->notice('return_and_yield_in_one_method', 'Do not use return and yield in a one method', $stmt);
34 1
            return true;
35
        }
36
37 5
        return false;
38
    }
39
40
    /**
41
     * @return array
42
     */
43 1
    public function getRegister()
44
    {
45
        return [
46 1
            Stmt\ClassMethod::class,
47 1
        ];
48
    }
49
}
50