Completed
Pull Request — master (#135)
by Juliette
03:37 queued 01:19
created

Sniffs/PHP/NewLanguageConstructsSniff.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewLanguageConstructsSniff.
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 * @copyright 2013 Cu.be Solutions bvba
11
 */
12
13
/**
14
 * PHPCompatibility_Sniffs_PHP_NewLanguageConstructsSniff.
15
 *
16
 * @category  PHP
17
 * @package   PHPCompatibility
18
 * @author    Wim Godden <[email protected]>
19
 * @version   1.0.0
20
 * @copyright 2013 Cu.be Solutions bvba
21
 */
22
class PHPCompatibility_Sniffs_PHP_NewLanguageConstructsSniff extends PHPCompatibility_Sniff
23
{
24
25
    /**
26
     * A list of new language constructs, not present in older versions.
27
     *
28
     * The array lists : version number with false (not present) or true (present).
29
     * If's sufficient to list the first version where the keyword appears.
30
     *
31
     * @var array(string => array(string => int|string|null))
32
     */
33
    protected $newConstructs = array(
34
                                        'T_NS_SEPARATOR' => array(
35
                                            '5.2' => false,
36
                                            '5.3' => true,
37
                                            'description' => 'the \ operator (for namespaces)'
38
                                        ),
39
                                        'T_POW' => array(
40
                                            '5.5' => false,
41
                                            '5.6' => true,
42
                                            'description' => 'power operator (**)'
43
                                        ),
44
                                        'T_POW_EQUAL' => array(
45
                                            '5.5' => false,
46
                                            '5.6' => true,
47
                                            'description' => 'power assignment operator (**=)'
48
                                        ),
49
                                        'T_SPACESHIP' => array(
50
                                            '5.6' => false,
51
                                            '7.0' => true,
52
                                            'description' => 'spaceship operator (<=>)'
53
                                        ),
54
                                        'T_COALESCE' => array(
55
                                            '5.6' => false,
56
                                            '7.0' => true,
57
                                            'description' => 'null coalescing operator (??)'
58
                                        ),
59
                                    );
60
61
62
    /**
63
     * Returns an array of tokens this test wants to listen for.
64
     *
65
     * @return array
66
     */
67 View Code Duplication
    public function register()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $tokens = array();
70
        foreach ($this->newConstructs as $token => $versions) {
71
            if (defined($token)) {
72
                $tokens[] = constant($token);
73
            }
74
        }
75
        return $tokens;
76
77
    }//end register()
78
79
80
    /**
81
     * Processes this test, when one of its tokens is encountered.
82
     *
83
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
84
     * @param int                  $stackPtr  The position of the current token in
85
     *                                        the stack passed in $tokens.
86
     *
87
     * @return void
88
     */
89
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
90
    {
91
        $tokens = $phpcsFile->getTokens();
92
        $this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['type']);
93
    }//end process()
94
95
96
    /**
97
     * Generates the error or warning for this sniff.
98
     *
99
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
100
     * @param int                  $stackPtr  The position of the function
101
     *                                        in the token array.
102
     * @param string               $function  The name of the function.
103
     *
104
     * @return void
105
     */
106 View Code Duplication
    protected function addError($phpcsFile, $stackPtr, $keywordName, $pattern=null)
107
    {
108
        if ($pattern === null) {
109
            $pattern = $keywordName;
110
        }
111
112
        $error = '';
113
114
        $isError = false;
115
        foreach ($this->newConstructs[$pattern] as $version => $present) {
116
            if ($this->supportsBelow($version)) {
117
                if ($present === false) {
118
                    $isError = true;
119
                    $error .= 'not present in PHP version ' . $version . ' or earlier';
120
                }
121
            }
122
        }
123
        if (strlen($error) > 0) {
124
            $error = $this->newConstructs[$keywordName]['description'] . ' is ' . $error;
125
126
            if ($isError === true) {
127
                $phpcsFile->addError($error, $stackPtr);
128
            } else {
129
                $phpcsFile->addWarning($error, $stackPtr);
130
            }
131
        }
132
133
    }//end addError()
134
135
}//end class
136