SpaceshipOperatorFeature   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 5
dl 0
loc 31
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fix() 0 23 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