Completed
Push — master ( 1813ef...334261 )
by Дмитрий
05:51
created

ReturnAndYieldInOneMethod::pass()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

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