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

register()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 11
loc 11
rs 9.4285
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewKeywordsSniff.
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_NewClassesSniff.
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_NewKeywordsSniff extends PHPCompatibility_Sniff
23
{
24
25
    /**
26
     * A list of new keywords, 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 $newKeywords = array(
34
                                        'T_HALT_COMPILER' => array(
35
                                            '5.0' => false,
36
                                            '5.1' => true,
37
                                            'description' => '"__halt_compiler" keyword'
38
                                        ),
39
                                        'T_CONST' => array(
40
                                            '5.2' => false,
41
                                            '5.3' => true,
42
                                            'description' => '"const" keyword'
43
                                        ),
44
                                        'T_CALLABLE' => array(
45
                                            '5.3' => false,
46
                                            '5.4' => true,
47
                                            'description' => '"callable" keyword'
48
                                        ),
49
                                        'T_DIR' => array(
50
                                            '5.2' => false,
51
                                            '5.3' => true,
52
                                            'description' => '__DIR__ magic constant'
53
                                        ),
54
                                        'T_GOTO' => array(
55
                                            '5.2' => false,
56
                                            '5.3' => true,
57
                                            'description' => '"goto" keyword'
58
                                        ),
59
                                        'T_INSTEADOF' => array(
60
                                            '5.3' => false,
61
                                            '5.4' => true,
62
                                            'description' => '"insteadof" keyword (for traits)'
63
                                        ),
64
                                        'T_NAMESPACE' => array(
65
                                            '5.2' => false,
66
                                            '5.3' => true,
67
                                            'description' => '"namespace" keyword'
68
                                        ),
69
                                        'T_NS_C' => array(
70
                                            '5.2' => false,
71
                                            '5.3' => true,
72
                                            'description' => '__NAMESPACE__ magic constant'
73
                                        ),
74
                                        'T_USE' => array(
75
                                            '5.2' => false,
76
                                            '5.3' => true,
77
                                            'description' => '"use" keyword (for traits/namespaces/anonymous functions)'
78
                                        ),
79
                                        'T_TRAIT' => array(
80
                                            '5.3' => false,
81
                                            '5.4' => true,
82
                                            'description' => '"trait" keyword'
83
                                        ),
84
                                        'T_TRAIT_C' => array(
85
                                            '5.3' => false,
86
                                            '5.4' => true,
87
                                            'description' => '__TRAIT__ magic constant'
88
                                        ),
89
                                        'T_YIELD' => array(
90
                                            '5.4' => false,
91
                                            '5.5' => true,
92
                                            'description' => '"yield" keyword (for generators)'
93
                                        ),
94
                                        'T_FINALLY' => array(
95
                                            '5.4' => false,
96
                                            '5.5' => true,
97
                                            'description' => '"finally" keyword (in exception handling)'
98
                                        ),
99
                                        'T_START_NOWDOC' => array(
100
                                            '5.2' => false,
101
                                            '5.3' => true,
102
                                            'description' => 'nowdoc functionality'
103
                                        ),
104
                                        'T_END_NOWDOC' => array(
105
                                            '5.2' => false,
106
                                            '5.3' => true,
107
                                            'description' => 'nowdoc functionality'
108
                                        ),
109
                                    );
110
111
112
    /**
113
     * Returns an array of tokens this test wants to listen for.
114
     *
115
     * @return array
116
     */
117 View Code Duplication
    public function register()
0 ignored issues
show
Duplication introduced by
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...
118
    {
119
        $tokens = array();
120
        foreach ($this->newKeywords as $token => $versions) {
121
            if (defined($token)) {
122
                $tokens[] = constant($token);
123
            }
124
        }
125
        return $tokens;
126
127
    }//end register()
128
129
130
    /**
131
     * Processes this test, when one of its tokens is encountered.
132
     *
133
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
134
     * @param int                  $stackPtr  The position of the current token in
135
     *                                        the stack passed in $tokens.
136
     *
137
     * @return void
138
     */
139
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
140
    {
141
        $tokens = $phpcsFile->getTokens();
142
143
        $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
144
        $prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
145
146
        // Skip attempts to use keywords as functions or class names - the former
147
        // will be reported by FrobiddenNamesAsInvokedFunctionsSniff, whilst the
148
        // latter doesn't yet have an appropriate sniff.
149
        // Either type will result in false-positives when targetting lower versions
150
        // of PHP where the name was not reserved, unless we explicitly check for
151
        // them.
152
        if (
153
            $tokens[$nextToken]['type'] != 'T_OPEN_PARENTHESIS'
154
            &&
155
            $tokens[$prevToken]['type'] != 'T_CLASS'
156
        ) {
157
            $this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['type']);
158
        }
159
    }//end process()
160
161
162
    /**
163
     * Generates the error or wanrning for this sniff.
164
     *
165
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
166
     * @param int                  $stackPtr  The position of the function
167
     *                                        in the token array.
168
     * @param string               $function  The name of the function.
0 ignored issues
show
Bug introduced by
There is no parameter named $function. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
169
     * @param string               $pattern   The pattern used for the match.
170
     *
171
     * @return void
172
     */
173 View Code Duplication
    protected function addError($phpcsFile, $stackPtr, $keywordName, $pattern=null)
0 ignored issues
show
Duplication introduced by
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...
174
    {
175
        if ($pattern === null) {
176
            $pattern = $keywordName;
177
        }
178
179
        $error = '';
180
181
        $isError = false;
182
        foreach ($this->newKeywords[$pattern] as $version => $present) {
183
            if ($this->supportsBelow($version)) {
184
                if ($present === false) {
185
                    $isError = true;
186
                    $error .= 'not present in PHP version ' . $version . ' or earlier';
187
                }
188
            }
189
        }
190
        if (strlen($error) > 0) {
191
            $error = $this->newKeywords[$keywordName]['description'] . ' is ' . $error;
192
193
            if ($isError === true) {
194
                $phpcsFile->addError($error, $stackPtr);
195
            } else {
196
                $phpcsFile->addWarning($error, $stackPtr);
197
            }
198
        }
199
    }//end addError()
200
201
}//end class
202