Completed
Push — master ( fc51f9...b3b654 )
by Freek
03:59
created

SpaceshipOperatorReplacer::leaveNode()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 26
rs 8.8571
c 1
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
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
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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