Completed
Push — master ( 02285f...915210 )
by personal
05:27
created

TokenCollectionCompatibility   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 42
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C decorate() 0 34 12
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Token;
11
12
/**
13
 * Representation of Collection of tokens
14
 *
15
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
16
 */
17
class TokenCollectionCompatibility {
18
19
20
    /**
21
     * @param array $tokens
22
     * @return array
23
     */
24
    public function decorate(array $tokens)
25
    {
26
        if(-1 !== version_compare(PHP_VERSION, '7.0.0')) {
27
            return $tokens;
28
        }
29
30
        $previous = null;
31
        foreach($tokens as $index => &$token) {
32
33
            // coalesce operator
34
            if(T_STRING == $token->getType()&&'?' == $token->getValue()
35
                && $index > 0
36
                && T_STRING == $previous->getType()&&'?' == $previous->getValue()
37
            ) {
38
                unset($tokens[$index]);
39
                $tokens[$index - 1] = new Token(array(T_COALESCE, T_COALESCE));
40
                continue;
41
            }
42
43
44
            // spaceship operator
45
            if($index > 2
46
                && T_STRING == $tokens[$index]->getType() && '>' === $tokens[$index]->getValue()
47
                && T_IS_SMALLER_OR_EQUAL == $previous->getType()
48
            ) {
49
                unset($tokens[$index]);
50
                $tokens[$index - 1] = new Token(array(T_SPACESHIP, T_SPACESHIP));
51
                continue;
52
            }
53
54
            $previous = $token;
55
        }
56
        return array_values($tokens);
57
    }
58
}
59