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

TokenCollectionCompatibility::decorate()   C

Complexity

Conditions 12
Paths 5

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 5.1612
cc 12
eloc 19
nc 5
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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