Test Setup Failed
Push — master ( 1fc96a...b6ad37 )
by Jan Philipp
03:06
created

SpaceshipOperatorFeature   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5
Metric Value
wmc 2
lcom 0
cbo 5
dl 0
loc 31
rs 10

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
    public function fix(NodeSearch $nodeSearch): bool
17
    {
18
        $found = false;
19
20
        /** @var Node\Expr\BinaryOp\Spaceship $node */
21
        foreach ($nodeSearch->eachType(Node\Expr\BinaryOp\Spaceship::class) as $node) {
22
            $found = true;
23
24
            $newNode = new Node\Expr\Ternary(
25
                new Node\Expr\BinaryOp\Smaller($node->left, $node->right),
26
                new Node\Scalar\LNumber(-1),
27
                new Node\Expr\Ternary(
28
                    new Node\Expr\BinaryOp\Equal($node->left, $node->right),
29
                new Node\Scalar\LNumber(0),
30
                new Node\Scalar\LNumber(1)
31
                )
32
            );
33
34
            $nodeSearch->replaceNode($node, $newNode);
35
        }
36
37
        return $found;
38
    }
39
}
40