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

ReturnAndYieldInOneMethod   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 93.33%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 43
ccs 14
cts 15
cp 0.9333
rs 10
c 3
b 0
f 1
wmc 6
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B 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 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