Completed
Pull Request — master (#237)
by
unknown
03:20
created

ReturnAndYieldInOneMethod::yieldStatementExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Statement;
4
5
use PhpParser\Node\Expr\Yield_;
6
use PhpParser\Node\Stmt;
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
    use DefaultMetadataPassTrait,
15
        ResolveExpressionTrait;
16
17
    /**
18
     * @param Stmt $stmt
19
     * @param Context $context
20
     * @return bool
21
     */
22 40
    public function pass(Stmt $stmt, Context $context)
23
    {
24 40
        if ($this->returnStatementExists($stmt) && $this->yieldStatementExists($stmt)) {
25 1
            $context->notice('return_and_yield_in_one_method', 'Do not use return and yield in a one method', $stmt);
26 1
            return true;
27
        }
28
29 40
        return false;
30
    }
31
32
    /**
33
     * @param Stmt $node
34
     *
35
     * @return bool
36
     */
37 40
    private function returnStatementExists(Stmt $node)
38
    {
39 40
        return (bool)$this->findReturnStatement([$node])->current();
40
    }
41
42
    /**
43
     * @param Stmt $node
44
     *
45
     * @return bool
46
     */
47 31
    private function yieldStatementExists(Stmt $node)
48
    {
49 31
        return (bool)$this->findStatement([$node], Yield_::class)->current();
50
    }
51
52
    /**
53
     * @return array
54
     */
55 1
    public function getRegister()
56
    {
57
        return [
58 1
            Stmt\ClassMethod::class,
59 1
        ];
60
    }
61
}
62