Passed
Push — master ( 3e88a2...447363 )
by Jan Philipp
02:46
created

NullCoalescingOperatorFeature   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fix() 0 17 2
1
<?php
2
3
4
namespace JanPiet\PhpTranspiler\Feature;
5
6
use JanPiet\PhpTranspiler\NodeSearch;
7
use PhpParser\Node;
8
9
class NullCoalescingOperatorFeature implements FeatureInterface
10
{
11
    /**
12
     * @param NodeSearch $nodeSearch
13
     * @return bool
14
     */
15 2
    public function fix(NodeSearch $nodeSearch): bool
16
    {
17 2
        $found = false;
18
        /** @var Node\Expr\BinaryOp\Coalesce $node */
19 2
        foreach ($nodeSearch->eachType(Node\Expr\BinaryOp\Coalesce::class) as $node) {
20 2
            $found = true;
21 2
            $newNode = new Node\Expr\Ternary(
22 2
                new Node\Expr\Isset_([$node->left]),
23 2
                $node->left,
24 2
                $node->right
25
            );
26
27 2
            $nodeSearch->replaceNode($node, $newNode);
28
        }
29
        
30 2
        return $found;
31
    }
32
}
33