Completed
Pull Request — master (#287)
by algo13
03:45
created

ReturnAndYieldInOneMethod::pass()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 4
b 0
f 0
nc 5
nop 2
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
crap 5.0144
rs 8.8571
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Statement;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Expr;
7
use PhpParser\Node\Stmt;
8
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
9
use PHPSA\Analyzer\Helper\ResolveExpressionTrait;
10
use PHPSA\Analyzer\Pass;
11
use PHPSA\Context;
12
13
class ReturnAndYieldInOneMethod implements Pass\AnalyzerPassInterface
14
{
15
    const DESCRIPTION = 'Checks for using return and yield statements in a one method and discourages it.';
16
17
    use DefaultMetadataPassTrait;
18
    use ResolveExpressionTrait;
19
20
    /**
21
     * @param Node\FunctionLike $func
22
     * @param Context $context
23
     * @return bool
24
     */
25 8
    public function pass(Node\FunctionLike $func, Context $context)
26
    {
27 8
        $stmts = $func->getStmts();
28 8
        if ($stmts === null) {
29
            return false;
30
        }
31 8
        $yieldExists = \PHPSA\generatorHasValue($this->findYieldExpression($stmts));
32 8
        if (!$yieldExists) {
33
            // YieldFrom is another expression
34 8
            $yieldExists = \PHPSA\generatorHasValue($this->findNode($stmts, Expr\YieldFrom::class));
35 8
        }
36
37 8
        if ($yieldExists && \PHPSA\generatorHasValue($this->findReturnStatement($stmts))) {
38 1
            $context->notice('return_and_yield_in_one_method', 'Do not use return and yield in a one method', $func);
0 ignored issues
show
Documentation introduced by
$func is of type object<PhpParser\Node\FunctionLike>, but the function expects a object<PhpParser\NodeAbstract>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39 1
            return true;
40
        }
41
42 8
        return false;
43
    }
44
45
    /**
46
     * @return array
47
     */
48 2
    public function getRegister()
49
    {
50
        return [
51 2
            Stmt\ClassMethod::class,
52 2
        ];
53
    }
54
}
55