1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility, an external standard for PHP_CodeSniffer. |
4
|
|
|
* |
5
|
|
|
* @package PHPCompatibility |
6
|
|
|
* @copyright 2012-2019 PHPCompatibility Contributors |
7
|
|
|
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
8
|
|
|
* @link https://github.com/PHPCompatibility/PHPCompatibility |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PHPCompatibility\Sniffs\LanguageConstructs; |
12
|
|
|
|
13
|
|
|
use PHPCompatibility\AbstractNewFeatureSniff; |
14
|
|
|
use PHP_CodeSniffer_File as File; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Detect use of new PHP language constructs. |
18
|
|
|
* |
19
|
|
|
* PHP version All |
20
|
|
|
* |
21
|
|
|
* @link https://wiki.php.net/rfc/namespaceseparator |
22
|
|
|
* @link https://wiki.php.net/rfc/variadics |
23
|
|
|
* @link https://wiki.php.net/rfc/argument_unpacking |
24
|
|
|
* |
25
|
|
|
* @since 5.6 |
26
|
|
|
* @since 7.1.0 Now extends the `AbstractNewFeatureSniff` instead of the base `Sniff` class.. |
27
|
|
|
* @since 9.0.0 Detection for new operator tokens has been moved to the `NewOperators` sniff. |
28
|
|
|
*/ |
29
|
|
|
class NewLanguageConstructsSniff extends AbstractNewFeatureSniff |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A list of new language constructs, not present in older versions. |
34
|
|
|
* |
35
|
|
|
* The array lists : version number with false (not present) or true (present). |
36
|
|
|
* If's sufficient to list the first version where the keyword appears. |
37
|
|
|
* |
38
|
|
|
* @since 5.6 |
39
|
|
|
* |
40
|
|
|
* @var array(string => array(string => bool|string)) |
41
|
|
|
*/ |
42
|
|
|
protected $newConstructs = array( |
43
|
|
|
'T_NS_SEPARATOR' => array( |
44
|
|
|
'5.2' => false, |
45
|
|
|
'5.3' => true, |
46
|
|
|
'description' => 'the \ operator (for namespaces)', |
47
|
|
|
), |
48
|
|
|
'T_ELLIPSIS' => array( |
49
|
|
|
'5.5' => false, |
50
|
|
|
'5.6' => true, |
51
|
|
|
'description' => 'the ... spread operator', |
52
|
|
|
), |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns an array of tokens this test wants to listen for. |
58
|
|
|
* |
59
|
|
|
* @since 5.6 |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function register() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$tokens = array(); |
66
|
|
|
foreach ($this->newConstructs as $token => $versions) { |
67
|
|
|
$tokens[] = constant($token); |
68
|
|
|
} |
69
|
|
|
return $tokens; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Processes this test, when one of its tokens is encountered. |
75
|
|
|
* |
76
|
|
|
* @since 5.6 |
77
|
|
|
* |
78
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
79
|
|
|
* @param int $stackPtr The position of the current token in |
80
|
|
|
* the stack passed in $tokens. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function process(File $phpcsFile, $stackPtr) |
85
|
|
|
{ |
86
|
|
|
$tokens = $phpcsFile->getTokens(); |
87
|
|
|
$tokenType = $tokens[$stackPtr]['type']; |
88
|
|
|
|
89
|
|
|
$itemInfo = array( |
90
|
|
|
'name' => $tokenType, |
91
|
|
|
); |
92
|
|
|
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the relevant sub-array for a specific item from a multi-dimensional array. |
98
|
|
|
* |
99
|
|
|
* @since 7.1.0 |
100
|
|
|
* |
101
|
|
|
* @param array $itemInfo Base information about the item. |
102
|
|
|
* |
103
|
|
|
* @return array Version and other information about the item. |
104
|
|
|
*/ |
105
|
|
|
public function getItemArray(array $itemInfo) |
106
|
|
|
{ |
107
|
|
|
return $this->newConstructs[$itemInfo['name']]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get an array of the non-PHP-version array keys used in a sub-array. |
113
|
|
|
* |
114
|
|
|
* @since 7.1.0 |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
protected function getNonVersionArrayKeys() |
119
|
|
|
{ |
120
|
|
|
return array('description'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Retrieve the relevant detail (version) information for use in an error message. |
126
|
|
|
* |
127
|
|
|
* @since 7.1.0 |
128
|
|
|
* |
129
|
|
|
* @param array $itemArray Version and other information about the item. |
130
|
|
|
* @param array $itemInfo Base information about the item. |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function getErrorInfo(array $itemArray, array $itemInfo) |
135
|
|
|
{ |
136
|
|
|
$errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
137
|
|
|
$errorInfo['description'] = $itemArray['description']; |
138
|
|
|
|
139
|
|
|
return $errorInfo; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Allow for concrete child classes to filter the error data before it's passed to PHPCS. |
145
|
|
|
* |
146
|
|
|
* @since 7.1.0 |
147
|
|
|
* |
148
|
|
|
* @param array $data The error data array which was created. |
149
|
|
|
* @param array $itemInfo Base information about the item this error message applies to. |
150
|
|
|
* @param array $errorInfo Detail information about an item this error message applies to. |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
155
|
|
|
{ |
156
|
|
|
$data[0] = $errorInfo['description']; |
157
|
|
|
return $data; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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.