Completed
Push — master ( e4a352...0b5c23 )
by Freek
02:00
created

NullCoalesceReplacer::leaveNode()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 6
eloc 16
nc 6
nop 1
1
<?php
2
3
namespace Spatie\Php7to5\NodeVisitors;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Expr\BinaryOp\Coalesce;
7
use PhpParser\NodeVisitorAbstract;
8
9
class NullCoalesceReplacer extends NodeVisitorAbstract
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function leaveNode(Node $node)
15
    {
16
        if (!$node instanceof Coalesce) {
17
            return;
18
        }
19
        switch(true)
20
        {
21
            case $node->left instanceof Node\Expr\FuncCall:
22
            case $node->left instanceof Node\Expr\MethodCall:
23
            case $node->left instanceof Node\Expr\StaticCall:
24
                $notEmptyCall = new Node\Expr\BooleanNot(new Node\Expr\FuncCall(new Node\Name('empty'), [$node->left]));
0 ignored issues
show
Documentation introduced by
array($node->left) is of type array<integer,object<Php...e\\Expr\\StaticCall>"}>, but the function expects a array<integer,object<PhpParser\Node\Arg>>.

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...
25
                return new Node\Expr\Ternary($notEmptyCall, $node->left, $node->right);
26
            case $node->left instanceof Node\Expr\BinaryOp:
27
                $issetCall = new Node\Expr\FuncCall(new Node\Name('isset'), [$node->left->right]);
0 ignored issues
show
Documentation introduced by
array($node->left->right) is of type array<integer,object<Php...pParser\\Node\\Expr>"}>, but the function expects a array<integer,object<PhpParser\Node\Arg>>.

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...
28
                $node->left->right = new Node\Expr\Ternary($issetCall, $node->left->right, $node->right);
29
                return $node->left;
30
            default:
31
                $issetCall = new Node\Expr\FuncCall(new Node\Name('isset'), [$node->left]);
0 ignored issues
show
Documentation introduced by
array($node->left) is of type array<integer,object<Php...pParser\\Node\\Expr>"}>, but the function expects a array<integer,object<PhpParser\Node\Arg>>.

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...
32
                return new Node\Expr\Ternary($issetCall, $node->left, $node->right);
33
        }
34
    }
35
}