SpaceshipOperatorReplacer   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 7
dl 0
loc 32
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A leaveNode() 0 26 2
1
<?php
2
3
namespace Spatie\Php7to5\NodeVisitors;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Expr\BinaryOp\Equal;
7
use PhpParser\Node\Expr\BinaryOp\Smaller;
8
use PhpParser\Node\Expr\BinaryOp\Spaceship;
9
use PhpParser\Node\Expr\Ternary;
10
use PhpParser\Node\Expr\UnaryMinus;
11
use PhpParser\Node\Scalar\LNumber;
12
use PhpParser\NodeVisitorAbstract;
13
14
class SpaceshipOperatorReplacer extends NodeVisitorAbstract
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function leaveNode(Node $node)
20
    {
21
        if (!$node instanceof Spaceship) {
22
            return;
23
        }
24
25
        /*
26
         * Replacing
27
         * $a <=> $b
28
         * with
29
         * $a < $b ? -1 : ($a == $b ? 0 : 1)
30
         */
31
32
        $attributes = $node->getAttributes();
33
34
        $smaller = new UnaryMinus(new LNumber(1, $attributes), $attributes);
35
        $equal = new LNumber(0, $attributes);
36
        $larger = new LNumber(1, $attributes);
37
38
        $isEqual = new Equal($node->left, $node->right, $attributes);
39
        $isSmaller = new Smaller($node->left, $node->right, $attributes);
40
41
        $else = new Ternary($isEqual, $equal, $larger, $attributes);
42
43
        return new Ternary($isSmaller, $smaller, $else, $attributes);
44
    }
45
}
46