SpaceshipOperatorFeature::fix()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
crap 2
1
<?php
2
3
4
namespace JanPiet\PhpTranspiler\Feature;
5
6
use JanPiet\PhpTranspiler\NodeSearch;
7
use PhpParser\Node;
8
9
class SpaceshipOperatorFeature implements FeatureInterface
10
{
11
12
    /**
13
     * @param NodeSearch $nodeSearch
14
     * @return bool
15
     */
16 2
    public function fix(NodeSearch $nodeSearch): bool
17
    {
18 2
        $found = false;
19
20
        /** @var Node\Expr\BinaryOp\Spaceship $node */
21 2
        foreach ($nodeSearch->eachType(Node\Expr\BinaryOp\Spaceship::class) as $node) {
22 2
            $found = true;
23
24 2
            $newNode = new Node\Expr\Ternary(
25 2
                new Node\Expr\BinaryOp\Smaller($node->left, $node->right),
26 2
                new Node\Scalar\LNumber(-1),
27 2
                new Node\Expr\Ternary(
28 2
                    new Node\Expr\BinaryOp\Equal($node->left, $node->right),
29 2
                new Node\Scalar\LNumber(0),
30 2
                new Node\Scalar\LNumber(1)
31
                )
32
            );
33
34 2
            $nodeSearch->replaceNode($node, $newNode);
35
        }
36
37 2
        return $found;
38
    }
39
}
40