@@ -25,43 +25,43 @@ |
||
25 | 25 | */ |
26 | 26 | class NewListReferenceAssignmentSniff extends NewKeyedListSniff |
27 | 27 | { |
28 | - /** |
|
29 | - * The token(s) within the list construct which is being targeted. |
|
30 | - * |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - protected $targetsInList = array( |
|
34 | - \T_BITWISE_AND => \T_BITWISE_AND, |
|
35 | - ); |
|
28 | + /** |
|
29 | + * The token(s) within the list construct which is being targeted. |
|
30 | + * |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + protected $targetsInList = array( |
|
34 | + \T_BITWISE_AND => \T_BITWISE_AND, |
|
35 | + ); |
|
36 | 36 | |
37 | - /** |
|
38 | - * Do a version check to determine if this sniff needs to run at all. |
|
39 | - * |
|
40 | - * @return bool |
|
41 | - */ |
|
42 | - protected function bowOutEarly() |
|
43 | - { |
|
44 | - return ($this->supportsBelow('7.2') === false); |
|
45 | - } |
|
37 | + /** |
|
38 | + * Do a version check to determine if this sniff needs to run at all. |
|
39 | + * |
|
40 | + * @return bool |
|
41 | + */ |
|
42 | + protected function bowOutEarly() |
|
43 | + { |
|
44 | + return ($this->supportsBelow('7.2') === false); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Examine the contents of a list construct to determine whether an error needs to be thrown. |
|
49 | - * |
|
50 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
51 | - * @param int $opener The position of the list open token. |
|
52 | - * @param int $closer The position of the list close token. |
|
53 | - * |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - protected function examineList(File $phpcsFile, $opener, $closer) |
|
57 | - { |
|
58 | - $start = $opener; |
|
59 | - while (($start = $this->hasTargetInList($phpcsFile, $start, $closer)) !== false) { |
|
60 | - $phpcsFile->addError( |
|
61 | - 'Reference assignments within list constructs are not supported in PHP 7.2 or earlier.', |
|
62 | - $start, |
|
63 | - 'Found' |
|
64 | - ); |
|
65 | - } |
|
66 | - } |
|
47 | + /** |
|
48 | + * Examine the contents of a list construct to determine whether an error needs to be thrown. |
|
49 | + * |
|
50 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
51 | + * @param int $opener The position of the list open token. |
|
52 | + * @param int $closer The position of the list close token. |
|
53 | + * |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + protected function examineList(File $phpcsFile, $opener, $closer) |
|
57 | + { |
|
58 | + $start = $opener; |
|
59 | + while (($start = $this->hasTargetInList($phpcsFile, $start, $closer)) !== false) { |
|
60 | + $phpcsFile->addError( |
|
61 | + 'Reference assignments within list constructs are not supported in PHP 7.2 or earlier.', |
|
62 | + $start, |
|
63 | + 'Found' |
|
64 | + ); |
|
65 | + } |
|
66 | + } |
|
67 | 67 | } |
@@ -30,154 +30,154 @@ |
||
30 | 30 | class AssignmentOrderSniff extends Sniff |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * Returns an array of tokens this test wants to listen for. |
|
35 | - * |
|
36 | - * @return array |
|
37 | - */ |
|
38 | - public function register() |
|
39 | - { |
|
40 | - return array( |
|
41 | - \T_LIST, |
|
42 | - \T_OPEN_SHORT_ARRAY, |
|
43 | - ); |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * Processes this test, when one of its tokens is encountered. |
|
49 | - * |
|
50 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
51 | - * @param int $stackPtr The position of the current token in the |
|
52 | - * stack passed in $tokens. |
|
53 | - * |
|
54 | - * @return void|int Void if not a valid list. If a list construct has been |
|
55 | - * examined, the stack pointer to the list closer to skip |
|
56 | - * passed any nested lists which don't need to be examined again. |
|
57 | - */ |
|
58 | - public function process(File $phpcsFile, $stackPtr) |
|
59 | - { |
|
60 | - if ($this->supportsAbove('7.0') === false) { |
|
61 | - return; |
|
62 | - } |
|
63 | - |
|
64 | - $tokens = $phpcsFile->getTokens(); |
|
65 | - |
|
66 | - if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY |
|
67 | - && $this->isShortList($phpcsFile, $stackPtr) === false |
|
68 | - ) { |
|
69 | - // Short array, not short list. |
|
70 | - return; |
|
71 | - } |
|
72 | - |
|
73 | - if ($tokens[$stackPtr]['code'] === \T_LIST) { |
|
74 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
75 | - if ($nextNonEmpty === false |
|
76 | - || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS |
|
77 | - || isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false |
|
78 | - ) { |
|
79 | - // Parse error or live coding. |
|
80 | - return; |
|
81 | - } |
|
82 | - |
|
83 | - $opener = $nextNonEmpty; |
|
84 | - $closer = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
85 | - } else { |
|
86 | - // Short list syntax. |
|
87 | - $opener = $stackPtr; |
|
88 | - |
|
89 | - if (isset($tokens[$stackPtr]['bracket_closer'])) { |
|
90 | - $closer = $tokens[$stackPtr]['bracket_closer']; |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - if (isset($opener, $closer) === false) { |
|
95 | - return; |
|
96 | - } |
|
97 | - |
|
98 | - /* |
|
33 | + /** |
|
34 | + * Returns an array of tokens this test wants to listen for. |
|
35 | + * |
|
36 | + * @return array |
|
37 | + */ |
|
38 | + public function register() |
|
39 | + { |
|
40 | + return array( |
|
41 | + \T_LIST, |
|
42 | + \T_OPEN_SHORT_ARRAY, |
|
43 | + ); |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * Processes this test, when one of its tokens is encountered. |
|
49 | + * |
|
50 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
51 | + * @param int $stackPtr The position of the current token in the |
|
52 | + * stack passed in $tokens. |
|
53 | + * |
|
54 | + * @return void|int Void if not a valid list. If a list construct has been |
|
55 | + * examined, the stack pointer to the list closer to skip |
|
56 | + * passed any nested lists which don't need to be examined again. |
|
57 | + */ |
|
58 | + public function process(File $phpcsFile, $stackPtr) |
|
59 | + { |
|
60 | + if ($this->supportsAbove('7.0') === false) { |
|
61 | + return; |
|
62 | + } |
|
63 | + |
|
64 | + $tokens = $phpcsFile->getTokens(); |
|
65 | + |
|
66 | + if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY |
|
67 | + && $this->isShortList($phpcsFile, $stackPtr) === false |
|
68 | + ) { |
|
69 | + // Short array, not short list. |
|
70 | + return; |
|
71 | + } |
|
72 | + |
|
73 | + if ($tokens[$stackPtr]['code'] === \T_LIST) { |
|
74 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
75 | + if ($nextNonEmpty === false |
|
76 | + || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS |
|
77 | + || isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false |
|
78 | + ) { |
|
79 | + // Parse error or live coding. |
|
80 | + return; |
|
81 | + } |
|
82 | + |
|
83 | + $opener = $nextNonEmpty; |
|
84 | + $closer = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
85 | + } else { |
|
86 | + // Short list syntax. |
|
87 | + $opener = $stackPtr; |
|
88 | + |
|
89 | + if (isset($tokens[$stackPtr]['bracket_closer'])) { |
|
90 | + $closer = $tokens[$stackPtr]['bracket_closer']; |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + if (isset($opener, $closer) === false) { |
|
95 | + return; |
|
96 | + } |
|
97 | + |
|
98 | + /* |
|
99 | 99 | * OK, so we have the opener & closer, now we need to check all the variables in the |
100 | 100 | * list() to see if there are duplicates as that's the problem. |
101 | 101 | */ |
102 | - $hasVars = $phpcsFile->findNext(array(\T_VARIABLE, \T_DOLLAR), ($opener + 1), $closer); |
|
103 | - if ($hasVars === false) { |
|
104 | - // Empty list, not our concern. |
|
105 | - return ($closer + 1); |
|
106 | - } |
|
107 | - |
|
108 | - // Set the variable delimiters based on the list type being examined. |
|
109 | - $stopPoints = array(\T_COMMA); |
|
110 | - if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) { |
|
111 | - $stopPoints[] = \T_CLOSE_SHORT_ARRAY; |
|
112 | - } else { |
|
113 | - $stopPoints[] = \T_CLOSE_PARENTHESIS; |
|
114 | - } |
|
115 | - |
|
116 | - $listVars = array(); |
|
117 | - $lastStopPoint = $opener; |
|
118 | - |
|
119 | - /* |
|
102 | + $hasVars = $phpcsFile->findNext(array(\T_VARIABLE, \T_DOLLAR), ($opener + 1), $closer); |
|
103 | + if ($hasVars === false) { |
|
104 | + // Empty list, not our concern. |
|
105 | + return ($closer + 1); |
|
106 | + } |
|
107 | + |
|
108 | + // Set the variable delimiters based on the list type being examined. |
|
109 | + $stopPoints = array(\T_COMMA); |
|
110 | + if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY) { |
|
111 | + $stopPoints[] = \T_CLOSE_SHORT_ARRAY; |
|
112 | + } else { |
|
113 | + $stopPoints[] = \T_CLOSE_PARENTHESIS; |
|
114 | + } |
|
115 | + |
|
116 | + $listVars = array(); |
|
117 | + $lastStopPoint = $opener; |
|
118 | + |
|
119 | + /* |
|
120 | 120 | * Create a list of all variables used within the `list()` construct. |
121 | 121 | * We're not concerned with whether these are nested or not, as any duplicate |
122 | 122 | * variable name used will be problematic, independent of nesting. |
123 | 123 | */ |
124 | - do { |
|
125 | - $nextStopPoint = $phpcsFile->findNext($stopPoints, ($lastStopPoint + 1), $closer); |
|
126 | - if ($nextStopPoint === false) { |
|
127 | - $nextStopPoint = $closer; |
|
128 | - } |
|
129 | - |
|
130 | - // Also detect this in PHP 7.1 keyed lists. |
|
131 | - $hasDoubleArrow = $phpcsFile->findNext(\T_DOUBLE_ARROW, ($lastStopPoint + 1), $nextStopPoint); |
|
132 | - if ($hasDoubleArrow !== false) { |
|
133 | - $lastStopPoint = $hasDoubleArrow; |
|
134 | - } |
|
135 | - |
|
136 | - // Find the start of the variable, allowing for variable variables. |
|
137 | - $nextStartPoint = $phpcsFile->findNext(array(\T_VARIABLE, \T_DOLLAR), ($lastStopPoint + 1), $nextStopPoint); |
|
138 | - if ($nextStartPoint === false) { |
|
139 | - // Skip past empty bits in the list, i.e. `list( $a, , ,)`. |
|
140 | - $lastStopPoint = $nextStopPoint; |
|
141 | - continue; |
|
142 | - } |
|
143 | - |
|
144 | - /* |
|
124 | + do { |
|
125 | + $nextStopPoint = $phpcsFile->findNext($stopPoints, ($lastStopPoint + 1), $closer); |
|
126 | + if ($nextStopPoint === false) { |
|
127 | + $nextStopPoint = $closer; |
|
128 | + } |
|
129 | + |
|
130 | + // Also detect this in PHP 7.1 keyed lists. |
|
131 | + $hasDoubleArrow = $phpcsFile->findNext(\T_DOUBLE_ARROW, ($lastStopPoint + 1), $nextStopPoint); |
|
132 | + if ($hasDoubleArrow !== false) { |
|
133 | + $lastStopPoint = $hasDoubleArrow; |
|
134 | + } |
|
135 | + |
|
136 | + // Find the start of the variable, allowing for variable variables. |
|
137 | + $nextStartPoint = $phpcsFile->findNext(array(\T_VARIABLE, \T_DOLLAR), ($lastStopPoint + 1), $nextStopPoint); |
|
138 | + if ($nextStartPoint === false) { |
|
139 | + // Skip past empty bits in the list, i.e. `list( $a, , ,)`. |
|
140 | + $lastStopPoint = $nextStopPoint; |
|
141 | + continue; |
|
142 | + } |
|
143 | + |
|
144 | + /* |
|
145 | 145 | * Gather the content of all non-empty tokens to determine the "variable name". |
146 | 146 | * Variable name in this context includes array or object property syntaxes, such |
147 | 147 | * as `$a['name']` and `$b->property`. |
148 | 148 | */ |
149 | - $varContent = ''; |
|
149 | + $varContent = ''; |
|
150 | 150 | |
151 | - for ($i = $nextStartPoint; $i < $nextStopPoint; $i++) { |
|
152 | - if (isset(Tokens::$emptyTokens[$tokens[$i]['code']])) { |
|
153 | - continue; |
|
154 | - } |
|
151 | + for ($i = $nextStartPoint; $i < $nextStopPoint; $i++) { |
|
152 | + if (isset(Tokens::$emptyTokens[$tokens[$i]['code']])) { |
|
153 | + continue; |
|
154 | + } |
|
155 | 155 | |
156 | - $varContent .= $tokens[$i]['content']; |
|
157 | - } |
|
156 | + $varContent .= $tokens[$i]['content']; |
|
157 | + } |
|
158 | 158 | |
159 | - if ($varContent !== '') { |
|
160 | - $listVars[] = $varContent; |
|
161 | - } |
|
159 | + if ($varContent !== '') { |
|
160 | + $listVars[] = $varContent; |
|
161 | + } |
|
162 | 162 | |
163 | - $lastStopPoint = $nextStopPoint; |
|
163 | + $lastStopPoint = $nextStopPoint; |
|
164 | 164 | |
165 | - } while ($lastStopPoint < $closer); |
|
165 | + } while ($lastStopPoint < $closer); |
|
166 | 166 | |
167 | - if (empty($listVars)) { |
|
168 | - // Shouldn't be possible, but just in case. |
|
169 | - return ($closer + 1); |
|
170 | - } |
|
167 | + if (empty($listVars)) { |
|
168 | + // Shouldn't be possible, but just in case. |
|
169 | + return ($closer + 1); |
|
170 | + } |
|
171 | 171 | |
172 | - // Verify that all variables used in the list() construct are unique. |
|
173 | - if (\count($listVars) !== \count(array_unique($listVars))) { |
|
174 | - $phpcsFile->addError( |
|
175 | - 'list() will assign variable from left-to-right since PHP 7.0. Ensure all variables in list() are unique to prevent unexpected results.', |
|
176 | - $stackPtr, |
|
177 | - 'Affected' |
|
178 | - ); |
|
179 | - } |
|
172 | + // Verify that all variables used in the list() construct are unique. |
|
173 | + if (\count($listVars) !== \count(array_unique($listVars))) { |
|
174 | + $phpcsFile->addError( |
|
175 | + 'list() will assign variable from left-to-right since PHP 7.0. Ensure all variables in list() are unique to prevent unexpected results.', |
|
176 | + $stackPtr, |
|
177 | + 'Affected' |
|
178 | + ); |
|
179 | + } |
|
180 | 180 | |
181 | - return ($closer + 1); |
|
182 | - } |
|
181 | + return ($closer + 1); |
|
182 | + } |
|
183 | 183 | } |
@@ -30,56 +30,56 @@ |
||
30 | 30 | class NewDynamicAccessToStaticSniff extends Sniff |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * Returns an array of tokens this test wants to listen for. |
|
35 | - * |
|
36 | - * @return array |
|
37 | - */ |
|
38 | - public function register() |
|
39 | - { |
|
40 | - return array( |
|
41 | - \T_DOUBLE_COLON, |
|
42 | - ); |
|
43 | - } |
|
33 | + /** |
|
34 | + * Returns an array of tokens this test wants to listen for. |
|
35 | + * |
|
36 | + * @return array |
|
37 | + */ |
|
38 | + public function register() |
|
39 | + { |
|
40 | + return array( |
|
41 | + \T_DOUBLE_COLON, |
|
42 | + ); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Processes this test, when one of its tokens is encountered. |
|
47 | - * |
|
48 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
49 | - * @param int $stackPtr The position of the current token in the |
|
50 | - * stack passed in $tokens. |
|
51 | - * |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function process(File $phpcsFile, $stackPtr) |
|
55 | - { |
|
56 | - if ($this->supportsBelow('5.2') === false) { |
|
57 | - return; |
|
58 | - } |
|
45 | + /** |
|
46 | + * Processes this test, when one of its tokens is encountered. |
|
47 | + * |
|
48 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
49 | + * @param int $stackPtr The position of the current token in the |
|
50 | + * stack passed in $tokens. |
|
51 | + * |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function process(File $phpcsFile, $stackPtr) |
|
55 | + { |
|
56 | + if ($this->supportsBelow('5.2') === false) { |
|
57 | + return; |
|
58 | + } |
|
59 | 59 | |
60 | - $tokens = $phpcsFile->getTokens(); |
|
61 | - $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
60 | + $tokens = $phpcsFile->getTokens(); |
|
61 | + $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
62 | 62 | |
63 | - // Disregard `static::` as well. Late static binding is reported by another sniff. |
|
64 | - if ($tokens[$prevNonEmpty]['code'] === \T_SELF |
|
65 | - || $tokens[$prevNonEmpty]['code'] === \T_PARENT |
|
66 | - || $tokens[$prevNonEmpty]['code'] === \T_STATIC |
|
67 | - ) { |
|
68 | - return; |
|
69 | - } |
|
63 | + // Disregard `static::` as well. Late static binding is reported by another sniff. |
|
64 | + if ($tokens[$prevNonEmpty]['code'] === \T_SELF |
|
65 | + || $tokens[$prevNonEmpty]['code'] === \T_PARENT |
|
66 | + || $tokens[$prevNonEmpty]['code'] === \T_STATIC |
|
67 | + ) { |
|
68 | + return; |
|
69 | + } |
|
70 | 70 | |
71 | - if ($tokens[$prevNonEmpty]['code'] === \T_STRING) { |
|
72 | - $prevPrevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevNonEmpty - 1), null, true); |
|
71 | + if ($tokens[$prevNonEmpty]['code'] === \T_STRING) { |
|
72 | + $prevPrevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevNonEmpty - 1), null, true); |
|
73 | 73 | |
74 | - if ($tokens[$prevPrevNonEmpty]['code'] !== \T_OBJECT_OPERATOR) { |
|
75 | - return; |
|
76 | - } |
|
77 | - } |
|
74 | + if ($tokens[$prevPrevNonEmpty]['code'] !== \T_OBJECT_OPERATOR) { |
|
75 | + return; |
|
76 | + } |
|
77 | + } |
|
78 | 78 | |
79 | - $phpcsFile->addError( |
|
80 | - 'Static class properties and methods, as well as class constants, could not be accessed using a dynamic (variable) classname in PHP 5.2 or earlier.', |
|
81 | - $stackPtr, |
|
82 | - 'Found' |
|
83 | - ); |
|
84 | - } |
|
79 | + $phpcsFile->addError( |
|
80 | + 'Static class properties and methods, as well as class constants, could not be accessed using a dynamic (variable) classname in PHP 5.2 or earlier.', |
|
81 | + $stackPtr, |
|
82 | + 'Found' |
|
83 | + ); |
|
84 | + } |
|
85 | 85 | } |
@@ -28,47 +28,47 @@ |
||
28 | 28 | class NewShortArraySniff extends Sniff |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * Returns an array of tokens this test wants to listen for. |
|
33 | - * |
|
34 | - * @return array |
|
35 | - */ |
|
36 | - public function register() |
|
37 | - { |
|
38 | - return array( |
|
39 | - \T_OPEN_SHORT_ARRAY, |
|
40 | - \T_CLOSE_SHORT_ARRAY, |
|
41 | - ); |
|
42 | - } |
|
31 | + /** |
|
32 | + * Returns an array of tokens this test wants to listen for. |
|
33 | + * |
|
34 | + * @return array |
|
35 | + */ |
|
36 | + public function register() |
|
37 | + { |
|
38 | + return array( |
|
39 | + \T_OPEN_SHORT_ARRAY, |
|
40 | + \T_CLOSE_SHORT_ARRAY, |
|
41 | + ); |
|
42 | + } |
|
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * Processes this test, when one of its tokens is encountered. |
|
47 | - * |
|
48 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
49 | - * @param int $stackPtr The position of the current token in |
|
50 | - * the stack passed in $tokens. |
|
51 | - * |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function process(File $phpcsFile, $stackPtr) |
|
55 | - { |
|
56 | - if ($this->supportsBelow('5.3') === false) { |
|
57 | - return; |
|
58 | - } |
|
45 | + /** |
|
46 | + * Processes this test, when one of its tokens is encountered. |
|
47 | + * |
|
48 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
49 | + * @param int $stackPtr The position of the current token in |
|
50 | + * the stack passed in $tokens. |
|
51 | + * |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function process(File $phpcsFile, $stackPtr) |
|
55 | + { |
|
56 | + if ($this->supportsBelow('5.3') === false) { |
|
57 | + return; |
|
58 | + } |
|
59 | 59 | |
60 | - $tokens = $phpcsFile->getTokens(); |
|
61 | - $token = $tokens[$stackPtr]; |
|
60 | + $tokens = $phpcsFile->getTokens(); |
|
61 | + $token = $tokens[$stackPtr]; |
|
62 | 62 | |
63 | - $error = '%s is available since 5.4'; |
|
64 | - $data = array(); |
|
63 | + $error = '%s is available since 5.4'; |
|
64 | + $data = array(); |
|
65 | 65 | |
66 | - if ($token['type'] === 'T_OPEN_SHORT_ARRAY') { |
|
67 | - $data[] = 'Short array syntax (open)'; |
|
68 | - } elseif ($token['type'] === 'T_CLOSE_SHORT_ARRAY') { |
|
69 | - $data[] = 'Short array syntax (close)'; |
|
70 | - } |
|
66 | + if ($token['type'] === 'T_OPEN_SHORT_ARRAY') { |
|
67 | + $data[] = 'Short array syntax (open)'; |
|
68 | + } elseif ($token['type'] === 'T_CLOSE_SHORT_ARRAY') { |
|
69 | + $data[] = 'Short array syntax (close)'; |
|
70 | + } |
|
71 | 71 | |
72 | - $phpcsFile->addError($error, $stackPtr, 'Found', $data); |
|
73 | - } |
|
72 | + $phpcsFile->addError($error, $stackPtr, 'Found', $data); |
|
73 | + } |
|
74 | 74 | } |
@@ -31,47 +31,47 @@ |
||
31 | 31 | class RemovedNewReferenceSniff extends Sniff |
32 | 32 | { |
33 | 33 | |
34 | - /** |
|
35 | - * Returns an array of tokens this test wants to listen for. |
|
36 | - * |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - public function register() |
|
40 | - { |
|
41 | - return array(\T_NEW); |
|
42 | - } |
|
34 | + /** |
|
35 | + * Returns an array of tokens this test wants to listen for. |
|
36 | + * |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + public function register() |
|
40 | + { |
|
41 | + return array(\T_NEW); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Processes this test, when one of its tokens is encountered. |
|
46 | - * |
|
47 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
48 | - * @param int $stackPtr The position of the current token in the |
|
49 | - * stack passed in $tokens. |
|
50 | - * |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function process(File $phpcsFile, $stackPtr) |
|
54 | - { |
|
55 | - if ($this->supportsAbove('5.3') === false) { |
|
56 | - return; |
|
57 | - } |
|
44 | + /** |
|
45 | + * Processes this test, when one of its tokens is encountered. |
|
46 | + * |
|
47 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
48 | + * @param int $stackPtr The position of the current token in the |
|
49 | + * stack passed in $tokens. |
|
50 | + * |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function process(File $phpcsFile, $stackPtr) |
|
54 | + { |
|
55 | + if ($this->supportsAbove('5.3') === false) { |
|
56 | + return; |
|
57 | + } |
|
58 | 58 | |
59 | - $tokens = $phpcsFile->getTokens(); |
|
60 | - $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
61 | - if ($prevNonEmpty === false || $tokens[$prevNonEmpty]['type'] !== 'T_BITWISE_AND') { |
|
62 | - return; |
|
63 | - } |
|
59 | + $tokens = $phpcsFile->getTokens(); |
|
60 | + $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
61 | + if ($prevNonEmpty === false || $tokens[$prevNonEmpty]['type'] !== 'T_BITWISE_AND') { |
|
62 | + return; |
|
63 | + } |
|
64 | 64 | |
65 | - $error = 'Assigning the return value of new by reference is deprecated in PHP 5.3'; |
|
66 | - $isError = false; |
|
67 | - $errorCode = 'Deprecated'; |
|
65 | + $error = 'Assigning the return value of new by reference is deprecated in PHP 5.3'; |
|
66 | + $isError = false; |
|
67 | + $errorCode = 'Deprecated'; |
|
68 | 68 | |
69 | - if ($this->supportsAbove('7.0') === true) { |
|
70 | - $error .= ' and has been removed in PHP 7.0'; |
|
71 | - $isError = true; |
|
72 | - $errorCode = 'Removed'; |
|
73 | - } |
|
69 | + if ($this->supportsAbove('7.0') === true) { |
|
70 | + $error .= ' and has been removed in PHP 7.0'; |
|
71 | + $isError = true; |
|
72 | + $errorCode = 'Removed'; |
|
73 | + } |
|
74 | 74 | |
75 | - $this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode); |
|
76 | - } |
|
75 | + $this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode); |
|
76 | + } |
|
77 | 77 | } |
@@ -26,90 +26,90 @@ |
||
26 | 26 | */ |
27 | 27 | class NewFunctionCallTrailingCommaSniff extends Sniff |
28 | 28 | { |
29 | - /** |
|
30 | - * Returns an array of tokens this test wants to listen for. |
|
31 | - * |
|
32 | - * @return array |
|
33 | - */ |
|
34 | - public function register() |
|
35 | - { |
|
36 | - return array( |
|
37 | - \T_STRING, |
|
38 | - \T_VARIABLE, |
|
39 | - \T_ISSET, |
|
40 | - \T_UNSET, |
|
41 | - ); |
|
42 | - } |
|
29 | + /** |
|
30 | + * Returns an array of tokens this test wants to listen for. |
|
31 | + * |
|
32 | + * @return array |
|
33 | + */ |
|
34 | + public function register() |
|
35 | + { |
|
36 | + return array( |
|
37 | + \T_STRING, |
|
38 | + \T_VARIABLE, |
|
39 | + \T_ISSET, |
|
40 | + \T_UNSET, |
|
41 | + ); |
|
42 | + } |
|
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * Processes this test, when one of its tokens is encountered. |
|
47 | - * |
|
48 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
49 | - * @param int $stackPtr The position of the current token in |
|
50 | - * the stack passed in $tokens. |
|
51 | - * |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function process(File $phpcsFile, $stackPtr) |
|
55 | - { |
|
56 | - if ($this->supportsBelow('7.2') === false) { |
|
57 | - return; |
|
58 | - } |
|
45 | + /** |
|
46 | + * Processes this test, when one of its tokens is encountered. |
|
47 | + * |
|
48 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
49 | + * @param int $stackPtr The position of the current token in |
|
50 | + * the stack passed in $tokens. |
|
51 | + * |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function process(File $phpcsFile, $stackPtr) |
|
55 | + { |
|
56 | + if ($this->supportsBelow('7.2') === false) { |
|
57 | + return; |
|
58 | + } |
|
59 | 59 | |
60 | - $tokens = $phpcsFile->getTokens(); |
|
60 | + $tokens = $phpcsFile->getTokens(); |
|
61 | 61 | |
62 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
63 | - if ($tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS |
|
64 | - || isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false |
|
65 | - ) { |
|
66 | - return; |
|
67 | - } |
|
62 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
63 | + if ($tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS |
|
64 | + || isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false |
|
65 | + ) { |
|
66 | + return; |
|
67 | + } |
|
68 | 68 | |
69 | - if ($tokens[$stackPtr]['code'] === \T_STRING) { |
|
70 | - $ignore = array( |
|
71 | - \T_FUNCTION => true, |
|
72 | - \T_CONST => true, |
|
73 | - \T_USE => true, |
|
74 | - ); |
|
69 | + if ($tokens[$stackPtr]['code'] === \T_STRING) { |
|
70 | + $ignore = array( |
|
71 | + \T_FUNCTION => true, |
|
72 | + \T_CONST => true, |
|
73 | + \T_USE => true, |
|
74 | + ); |
|
75 | 75 | |
76 | - $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
77 | - if (isset($ignore[$tokens[$prevNonEmpty]['code']]) === true) { |
|
78 | - // Not a function call. |
|
79 | - return; |
|
80 | - } |
|
81 | - } |
|
76 | + $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
77 | + if (isset($ignore[$tokens[$prevNonEmpty]['code']]) === true) { |
|
78 | + // Not a function call. |
|
79 | + return; |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | - $closer = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
84 | - $lastInParenthesis = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($closer - 1), $nextNonEmpty, true); |
|
83 | + $closer = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
84 | + $lastInParenthesis = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($closer - 1), $nextNonEmpty, true); |
|
85 | 85 | |
86 | - if ($tokens[$lastInParenthesis]['code'] !== \T_COMMA) { |
|
87 | - return; |
|
88 | - } |
|
86 | + if ($tokens[$lastInParenthesis]['code'] !== \T_COMMA) { |
|
87 | + return; |
|
88 | + } |
|
89 | 89 | |
90 | - $data = array(); |
|
91 | - switch ($tokens[$stackPtr]['code']) { |
|
92 | - case \T_ISSET: |
|
93 | - $data[] = 'calls to isset()'; |
|
94 | - $errorCode = 'FoundInIsset'; |
|
95 | - break; |
|
90 | + $data = array(); |
|
91 | + switch ($tokens[$stackPtr]['code']) { |
|
92 | + case \T_ISSET: |
|
93 | + $data[] = 'calls to isset()'; |
|
94 | + $errorCode = 'FoundInIsset'; |
|
95 | + break; |
|
96 | 96 | |
97 | - case \T_UNSET: |
|
98 | - $data[] = 'calls to unset()'; |
|
99 | - $errorCode = 'FoundInUnset'; |
|
100 | - break; |
|
97 | + case \T_UNSET: |
|
98 | + $data[] = 'calls to unset()'; |
|
99 | + $errorCode = 'FoundInUnset'; |
|
100 | + break; |
|
101 | 101 | |
102 | - default: |
|
103 | - $data[] = 'function calls'; |
|
104 | - $errorCode = 'FoundInFunctionCall'; |
|
105 | - break; |
|
106 | - } |
|
102 | + default: |
|
103 | + $data[] = 'function calls'; |
|
104 | + $errorCode = 'FoundInFunctionCall'; |
|
105 | + break; |
|
106 | + } |
|
107 | 107 | |
108 | - $phpcsFile->addError( |
|
109 | - 'Trailing comma\'s are not allowed in %s in PHP 7.2 or earlier', |
|
110 | - $lastInParenthesis, |
|
111 | - $errorCode, |
|
112 | - $data |
|
113 | - ); |
|
114 | - } |
|
108 | + $phpcsFile->addError( |
|
109 | + 'Trailing comma\'s are not allowed in %s in PHP 7.2 or earlier', |
|
110 | + $lastInParenthesis, |
|
111 | + $errorCode, |
|
112 | + $data |
|
113 | + ); |
|
114 | + } |
|
115 | 115 | } |
@@ -33,215 +33,215 @@ |
||
33 | 33 | class NewFlexibleHeredocNowdocSniff extends Sniff |
34 | 34 | { |
35 | 35 | |
36 | - /** |
|
37 | - * Returns an array of tokens this test wants to listen for. |
|
38 | - * |
|
39 | - * @return array |
|
40 | - */ |
|
41 | - public function register() |
|
42 | - { |
|
43 | - $targets = array( |
|
44 | - \T_END_HEREDOC, |
|
45 | - \T_END_NOWDOC, |
|
46 | - ); |
|
47 | - |
|
48 | - if (version_compare(\PHP_VERSION_ID, '70299', '>') === false) { |
|
49 | - // Start identifier of a PHP 7.3 flexible heredoc/nowdoc. |
|
50 | - $targets[] = \T_STRING; |
|
51 | - } |
|
52 | - |
|
53 | - return $targets; |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * Processes this test, when one of its tokens is encountered. |
|
59 | - * |
|
60 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
61 | - * @param int $stackPtr The position of the current token in the |
|
62 | - * stack passed in $tokens. |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function process(File $phpcsFile, $stackPtr) |
|
67 | - { |
|
68 | - /* |
|
36 | + /** |
|
37 | + * Returns an array of tokens this test wants to listen for. |
|
38 | + * |
|
39 | + * @return array |
|
40 | + */ |
|
41 | + public function register() |
|
42 | + { |
|
43 | + $targets = array( |
|
44 | + \T_END_HEREDOC, |
|
45 | + \T_END_NOWDOC, |
|
46 | + ); |
|
47 | + |
|
48 | + if (version_compare(\PHP_VERSION_ID, '70299', '>') === false) { |
|
49 | + // Start identifier of a PHP 7.3 flexible heredoc/nowdoc. |
|
50 | + $targets[] = \T_STRING; |
|
51 | + } |
|
52 | + |
|
53 | + return $targets; |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * Processes this test, when one of its tokens is encountered. |
|
59 | + * |
|
60 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
61 | + * @param int $stackPtr The position of the current token in the |
|
62 | + * stack passed in $tokens. |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function process(File $phpcsFile, $stackPtr) |
|
67 | + { |
|
68 | + /* |
|
69 | 69 | * Due to a tokenizer bug which gets hit when the PHP 7.3 heredoc/nowdoc syntax |
70 | 70 | * is used, this part of the sniff cannot possibly work on PHPCS < 2.6.0. |
71 | 71 | * See upstream issue #928. |
72 | 72 | */ |
73 | - if ($this->supportsBelow('7.2') === true && version_compare(PHPCSHelper::getVersion(), '2.6.0', '>=')) { |
|
74 | - $this->detectIndentedNonStandAloneClosingMarker($phpcsFile, $stackPtr); |
|
75 | - } |
|
76 | - |
|
77 | - $tokens = $phpcsFile->getTokens(); |
|
78 | - if ($this->supportsAbove('7.3') === true && $tokens[$stackPtr]['code'] !== \T_STRING) { |
|
79 | - $this->detectClosingMarkerInBody($phpcsFile, $stackPtr); |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Detect indented and/or non-stand alone closing markers. |
|
86 | - * |
|
87 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
88 | - * @param int $stackPtr The position of the current token in the |
|
89 | - * stack passed in $tokens. |
|
90 | - * |
|
91 | - * @return void |
|
92 | - */ |
|
93 | - protected function detectIndentedNonStandAloneClosingMarker(File $phpcsFile, $stackPtr) |
|
94 | - { |
|
95 | - $tokens = $phpcsFile->getTokens(); |
|
96 | - $indentError = 'Heredoc/nowdoc with an indented closing marker is not supported in PHP 7.2 or earlier.'; |
|
97 | - $indentErrorCode = 'IndentedClosingMarker'; |
|
98 | - $trailingError = 'Having code - other than a semi-colon or new line - after the closing marker of a heredoc/nowdoc is not supported in PHP 7.2 or earlier.'; |
|
99 | - $trailingErrorCode = 'ClosingMarkerNoNewLine'; |
|
100 | - |
|
101 | - if (version_compare(\PHP_VERSION_ID, '70299', '>') === true) { |
|
102 | - |
|
103 | - /* |
|
73 | + if ($this->supportsBelow('7.2') === true && version_compare(PHPCSHelper::getVersion(), '2.6.0', '>=')) { |
|
74 | + $this->detectIndentedNonStandAloneClosingMarker($phpcsFile, $stackPtr); |
|
75 | + } |
|
76 | + |
|
77 | + $tokens = $phpcsFile->getTokens(); |
|
78 | + if ($this->supportsAbove('7.3') === true && $tokens[$stackPtr]['code'] !== \T_STRING) { |
|
79 | + $this->detectClosingMarkerInBody($phpcsFile, $stackPtr); |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Detect indented and/or non-stand alone closing markers. |
|
86 | + * |
|
87 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
88 | + * @param int $stackPtr The position of the current token in the |
|
89 | + * stack passed in $tokens. |
|
90 | + * |
|
91 | + * @return void |
|
92 | + */ |
|
93 | + protected function detectIndentedNonStandAloneClosingMarker(File $phpcsFile, $stackPtr) |
|
94 | + { |
|
95 | + $tokens = $phpcsFile->getTokens(); |
|
96 | + $indentError = 'Heredoc/nowdoc with an indented closing marker is not supported in PHP 7.2 or earlier.'; |
|
97 | + $indentErrorCode = 'IndentedClosingMarker'; |
|
98 | + $trailingError = 'Having code - other than a semi-colon or new line - after the closing marker of a heredoc/nowdoc is not supported in PHP 7.2 or earlier.'; |
|
99 | + $trailingErrorCode = 'ClosingMarkerNoNewLine'; |
|
100 | + |
|
101 | + if (version_compare(\PHP_VERSION_ID, '70299', '>') === true) { |
|
102 | + |
|
103 | + /* |
|
104 | 104 | * Check for indented closing marker. |
105 | 105 | */ |
106 | - if (ltrim($tokens[$stackPtr]['content']) !== $tokens[$stackPtr]['content']) { |
|
107 | - $phpcsFile->addError($indentError, $stackPtr, $indentErrorCode); |
|
108 | - } |
|
106 | + if (ltrim($tokens[$stackPtr]['content']) !== $tokens[$stackPtr]['content']) { |
|
107 | + $phpcsFile->addError($indentError, $stackPtr, $indentErrorCode); |
|
108 | + } |
|
109 | 109 | |
110 | - /* |
|
110 | + /* |
|
111 | 111 | * Check for tokens after the closing marker. |
112 | 112 | */ |
113 | - $nextNonWhitespace = $phpcsFile->findNext(array(\T_WHITESPACE, \T_SEMICOLON), ($stackPtr + 1), null, true); |
|
114 | - if ($tokens[$stackPtr]['line'] === $tokens[$nextNonWhitespace]['line']) { |
|
115 | - $phpcsFile->addError($trailingError, $stackPtr, $trailingErrorCode); |
|
116 | - } |
|
117 | - } else { |
|
118 | - // For PHP < 7.3, we're only interested in T_STRING tokens. |
|
119 | - if ($tokens[$stackPtr]['code'] !== \T_STRING) { |
|
120 | - return; |
|
121 | - } |
|
113 | + $nextNonWhitespace = $phpcsFile->findNext(array(\T_WHITESPACE, \T_SEMICOLON), ($stackPtr + 1), null, true); |
|
114 | + if ($tokens[$stackPtr]['line'] === $tokens[$nextNonWhitespace]['line']) { |
|
115 | + $phpcsFile->addError($trailingError, $stackPtr, $trailingErrorCode); |
|
116 | + } |
|
117 | + } else { |
|
118 | + // For PHP < 7.3, we're only interested in T_STRING tokens. |
|
119 | + if ($tokens[$stackPtr]['code'] !== \T_STRING) { |
|
120 | + return; |
|
121 | + } |
|
122 | 122 | |
123 | - if (preg_match('`^<<<([\'"]?)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\1[\r\n]+`', $tokens[$stackPtr]['content'], $matches) !== 1) { |
|
124 | - // Not the start of a PHP 7.3 flexible heredoc/nowdoc. |
|
125 | - return; |
|
126 | - } |
|
123 | + if (preg_match('`^<<<([\'"]?)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\1[\r\n]+`', $tokens[$stackPtr]['content'], $matches) !== 1) { |
|
124 | + // Not the start of a PHP 7.3 flexible heredoc/nowdoc. |
|
125 | + return; |
|
126 | + } |
|
127 | 127 | |
128 | - $identifier = $matches[2]; |
|
128 | + $identifier = $matches[2]; |
|
129 | 129 | |
130 | - for ($i = ($stackPtr + 1); $i <= $phpcsFile->numTokens; $i++) { |
|
131 | - if ($tokens[$i]['code'] !== \T_ENCAPSED_AND_WHITESPACE) { |
|
132 | - continue; |
|
133 | - } |
|
130 | + for ($i = ($stackPtr + 1); $i <= $phpcsFile->numTokens; $i++) { |
|
131 | + if ($tokens[$i]['code'] !== \T_ENCAPSED_AND_WHITESPACE) { |
|
132 | + continue; |
|
133 | + } |
|
134 | 134 | |
135 | - $trimmed = ltrim($tokens[$i]['content']); |
|
135 | + $trimmed = ltrim($tokens[$i]['content']); |
|
136 | 136 | |
137 | - if (strpos($trimmed, $identifier) !== 0) { |
|
138 | - continue; |
|
139 | - } |
|
137 | + if (strpos($trimmed, $identifier) !== 0) { |
|
138 | + continue; |
|
139 | + } |
|
140 | 140 | |
141 | - // OK, we've found the PHP 7.3 flexible heredoc/nowdoc closing marker. |
|
141 | + // OK, we've found the PHP 7.3 flexible heredoc/nowdoc closing marker. |
|
142 | 142 | |
143 | - /* |
|
143 | + /* |
|
144 | 144 | * Check for indented closing marker. |
145 | 145 | */ |
146 | - if ($trimmed !== $tokens[$i]['content']) { |
|
147 | - // Indent found before closing marker. |
|
148 | - $phpcsFile->addError($indentError, $i, $indentErrorCode); |
|
149 | - } |
|
146 | + if ($trimmed !== $tokens[$i]['content']) { |
|
147 | + // Indent found before closing marker. |
|
148 | + $phpcsFile->addError($indentError, $i, $indentErrorCode); |
|
149 | + } |
|
150 | 150 | |
151 | - /* |
|
151 | + /* |
|
152 | 152 | * Check for tokens after the closing marker. |
153 | 153 | */ |
154 | - // Remove the identifier. |
|
155 | - $afterMarker = substr($trimmed, \strlen($identifier)); |
|
156 | - // Remove a potential semi-colon at the beginning of what's left of the string. |
|
157 | - $afterMarker = ltrim($afterMarker, ';'); |
|
158 | - // Remove new line characters at the end of the string. |
|
159 | - $afterMarker = rtrim($afterMarker, "\r\n"); |
|
160 | - |
|
161 | - if ($afterMarker !== '') { |
|
162 | - $phpcsFile->addError($trailingError, $i, $trailingErrorCode); |
|
163 | - } |
|
164 | - |
|
165 | - break; |
|
166 | - } |
|
167 | - } |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * Detect heredoc/nowdoc identifiers at the start of lines in the heredoc/nowdoc body. |
|
173 | - * |
|
174 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
175 | - * @param int $stackPtr The position of the current token in the |
|
176 | - * stack passed in $tokens. |
|
177 | - * |
|
178 | - * @return void |
|
179 | - */ |
|
180 | - protected function detectClosingMarkerInBody(File $phpcsFile, $stackPtr) |
|
181 | - { |
|
182 | - $tokens = $phpcsFile->getTokens(); |
|
183 | - $error = 'The body of a heredoc/nowdoc can not contain the heredoc/nowdoc closing marker as text at the start of a line since PHP 7.3.'; |
|
184 | - $errorCode = 'ClosingMarkerNoNewLine'; |
|
185 | - |
|
186 | - if (version_compare(\PHP_VERSION_ID, '70299', '>') === true) { |
|
187 | - $nextNonWhitespace = $phpcsFile->findNext(\T_WHITESPACE, ($stackPtr + 1), null, true, null, true); |
|
188 | - if ($nextNonWhitespace === false |
|
189 | - || $tokens[$nextNonWhitespace]['code'] === \T_SEMICOLON |
|
190 | - || (($tokens[$nextNonWhitespace]['code'] === \T_COMMA |
|
191 | - || $tokens[$nextNonWhitespace]['code'] === \T_STRING_CONCAT) |
|
192 | - && $tokens[$nextNonWhitespace]['line'] !== $tokens[$stackPtr]['line']) |
|
193 | - ) { |
|
194 | - // This is most likely a correctly identified closing marker. |
|
195 | - return; |
|
196 | - } |
|
197 | - |
|
198 | - // The real closing tag has to be before the next heredoc/nowdoc. |
|
199 | - $nextHereNowDoc = $phpcsFile->findNext(array(\T_START_HEREDOC, \T_START_NOWDOC), ($stackPtr + 1)); |
|
200 | - if ($nextHereNowDoc === false) { |
|
201 | - $nextHereNowDoc = null; |
|
202 | - } |
|
203 | - |
|
204 | - $identifier = trim($tokens[$stackPtr]['content']); |
|
205 | - $realClosingMarker = $stackPtr; |
|
206 | - |
|
207 | - while (($realClosingMarker = $phpcsFile->findNext(\T_STRING, ($realClosingMarker + 1), $nextHereNowDoc, false, $identifier)) !== false) { |
|
208 | - |
|
209 | - $prevNonWhitespace = $phpcsFile->findPrevious(\T_WHITESPACE, ($realClosingMarker - 1), null, true); |
|
210 | - if ($prevNonWhitespace === false |
|
211 | - || $tokens[$prevNonWhitespace]['line'] === $tokens[$realClosingMarker]['line'] |
|
212 | - ) { |
|
213 | - // Marker text found, but not at the start of the line. |
|
214 | - continue; |
|
215 | - } |
|
216 | - |
|
217 | - // The original T_END_HEREDOC/T_END_NOWDOC was most likely incorrect as we've found |
|
218 | - // a possible alternative closing marker. |
|
219 | - $phpcsFile->addError($error, $stackPtr, $errorCode); |
|
220 | - |
|
221 | - break; |
|
222 | - } |
|
223 | - |
|
224 | - } else { |
|
225 | - if (isset($tokens[$stackPtr]['scope_closer'], $tokens[$stackPtr]['scope_opener']) === true |
|
226 | - && $tokens[$stackPtr]['scope_closer'] === $stackPtr |
|
227 | - ) { |
|
228 | - $opener = $tokens[$stackPtr]['scope_opener']; |
|
229 | - } else { |
|
230 | - // PHPCS < 3.0.2 did not add scope_* values for Nowdocs. |
|
231 | - $opener = $phpcsFile->findPrevious(\T_START_NOWDOC, ($stackPtr - 1)); |
|
232 | - if ($opener === false) { |
|
233 | - return; |
|
234 | - } |
|
235 | - } |
|
236 | - |
|
237 | - $quotedIdentifier = preg_quote($tokens[$stackPtr]['content'], '`'); |
|
238 | - |
|
239 | - // Throw an error for each line in the body which starts with the identifier. |
|
240 | - for ($i = ($opener + 1); $i < $stackPtr; $i++) { |
|
241 | - if (preg_match('`^[ \t]*' . $quotedIdentifier . '\b`', $tokens[$i]['content']) === 1) { |
|
242 | - $phpcsFile->addError($error, $i, $errorCode); |
|
243 | - } |
|
244 | - } |
|
245 | - } |
|
246 | - } |
|
154 | + // Remove the identifier. |
|
155 | + $afterMarker = substr($trimmed, \strlen($identifier)); |
|
156 | + // Remove a potential semi-colon at the beginning of what's left of the string. |
|
157 | + $afterMarker = ltrim($afterMarker, ';'); |
|
158 | + // Remove new line characters at the end of the string. |
|
159 | + $afterMarker = rtrim($afterMarker, "\r\n"); |
|
160 | + |
|
161 | + if ($afterMarker !== '') { |
|
162 | + $phpcsFile->addError($trailingError, $i, $trailingErrorCode); |
|
163 | + } |
|
164 | + |
|
165 | + break; |
|
166 | + } |
|
167 | + } |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * Detect heredoc/nowdoc identifiers at the start of lines in the heredoc/nowdoc body. |
|
173 | + * |
|
174 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
175 | + * @param int $stackPtr The position of the current token in the |
|
176 | + * stack passed in $tokens. |
|
177 | + * |
|
178 | + * @return void |
|
179 | + */ |
|
180 | + protected function detectClosingMarkerInBody(File $phpcsFile, $stackPtr) |
|
181 | + { |
|
182 | + $tokens = $phpcsFile->getTokens(); |
|
183 | + $error = 'The body of a heredoc/nowdoc can not contain the heredoc/nowdoc closing marker as text at the start of a line since PHP 7.3.'; |
|
184 | + $errorCode = 'ClosingMarkerNoNewLine'; |
|
185 | + |
|
186 | + if (version_compare(\PHP_VERSION_ID, '70299', '>') === true) { |
|
187 | + $nextNonWhitespace = $phpcsFile->findNext(\T_WHITESPACE, ($stackPtr + 1), null, true, null, true); |
|
188 | + if ($nextNonWhitespace === false |
|
189 | + || $tokens[$nextNonWhitespace]['code'] === \T_SEMICOLON |
|
190 | + || (($tokens[$nextNonWhitespace]['code'] === \T_COMMA |
|
191 | + || $tokens[$nextNonWhitespace]['code'] === \T_STRING_CONCAT) |
|
192 | + && $tokens[$nextNonWhitespace]['line'] !== $tokens[$stackPtr]['line']) |
|
193 | + ) { |
|
194 | + // This is most likely a correctly identified closing marker. |
|
195 | + return; |
|
196 | + } |
|
197 | + |
|
198 | + // The real closing tag has to be before the next heredoc/nowdoc. |
|
199 | + $nextHereNowDoc = $phpcsFile->findNext(array(\T_START_HEREDOC, \T_START_NOWDOC), ($stackPtr + 1)); |
|
200 | + if ($nextHereNowDoc === false) { |
|
201 | + $nextHereNowDoc = null; |
|
202 | + } |
|
203 | + |
|
204 | + $identifier = trim($tokens[$stackPtr]['content']); |
|
205 | + $realClosingMarker = $stackPtr; |
|
206 | + |
|
207 | + while (($realClosingMarker = $phpcsFile->findNext(\T_STRING, ($realClosingMarker + 1), $nextHereNowDoc, false, $identifier)) !== false) { |
|
208 | + |
|
209 | + $prevNonWhitespace = $phpcsFile->findPrevious(\T_WHITESPACE, ($realClosingMarker - 1), null, true); |
|
210 | + if ($prevNonWhitespace === false |
|
211 | + || $tokens[$prevNonWhitespace]['line'] === $tokens[$realClosingMarker]['line'] |
|
212 | + ) { |
|
213 | + // Marker text found, but not at the start of the line. |
|
214 | + continue; |
|
215 | + } |
|
216 | + |
|
217 | + // The original T_END_HEREDOC/T_END_NOWDOC was most likely incorrect as we've found |
|
218 | + // a possible alternative closing marker. |
|
219 | + $phpcsFile->addError($error, $stackPtr, $errorCode); |
|
220 | + |
|
221 | + break; |
|
222 | + } |
|
223 | + |
|
224 | + } else { |
|
225 | + if (isset($tokens[$stackPtr]['scope_closer'], $tokens[$stackPtr]['scope_opener']) === true |
|
226 | + && $tokens[$stackPtr]['scope_closer'] === $stackPtr |
|
227 | + ) { |
|
228 | + $opener = $tokens[$stackPtr]['scope_opener']; |
|
229 | + } else { |
|
230 | + // PHPCS < 3.0.2 did not add scope_* values for Nowdocs. |
|
231 | + $opener = $phpcsFile->findPrevious(\T_START_NOWDOC, ($stackPtr - 1)); |
|
232 | + if ($opener === false) { |
|
233 | + return; |
|
234 | + } |
|
235 | + } |
|
236 | + |
|
237 | + $quotedIdentifier = preg_quote($tokens[$stackPtr]['content'], '`'); |
|
238 | + |
|
239 | + // Throw an error for each line in the body which starts with the identifier. |
|
240 | + for ($i = ($opener + 1); $i < $stackPtr; $i++) { |
|
241 | + if (preg_match('`^[ \t]*' . $quotedIdentifier . '\b`', $tokens[$i]['content']) === 1) { |
|
242 | + $phpcsFile->addError($error, $i, $errorCode); |
|
243 | + } |
|
244 | + } |
|
245 | + } |
|
246 | + } |
|
247 | 247 | } |
@@ -33,221 +33,221 @@ |
||
33 | 33 | class ForbiddenCallTimePassByReferenceSniff extends Sniff |
34 | 34 | { |
35 | 35 | |
36 | - /** |
|
37 | - * Tokens that represent assignments or equality comparisons. |
|
38 | - * |
|
39 | - * Near duplicate of Tokens::$assignmentTokens + Tokens::$equalityTokens. |
|
40 | - * Copied in for PHPCS cross-version compatibility. |
|
41 | - * |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - private $assignOrCompare = array( |
|
45 | - // Equality tokens. |
|
46 | - 'T_IS_EQUAL' => true, |
|
47 | - 'T_IS_NOT_EQUAL' => true, |
|
48 | - 'T_IS_IDENTICAL' => true, |
|
49 | - 'T_IS_NOT_IDENTICAL' => true, |
|
50 | - 'T_IS_SMALLER_OR_EQUAL' => true, |
|
51 | - 'T_IS_GREATER_OR_EQUAL' => true, |
|
52 | - |
|
53 | - // Assignment tokens. |
|
54 | - 'T_EQUAL' => true, |
|
55 | - 'T_AND_EQUAL' => true, |
|
56 | - 'T_OR_EQUAL' => true, |
|
57 | - 'T_CONCAT_EQUAL' => true, |
|
58 | - 'T_DIV_EQUAL' => true, |
|
59 | - 'T_MINUS_EQUAL' => true, |
|
60 | - 'T_POW_EQUAL' => true, |
|
61 | - 'T_MOD_EQUAL' => true, |
|
62 | - 'T_MUL_EQUAL' => true, |
|
63 | - 'T_PLUS_EQUAL' => true, |
|
64 | - 'T_XOR_EQUAL' => true, |
|
65 | - 'T_DOUBLE_ARROW' => true, |
|
66 | - 'T_SL_EQUAL' => true, |
|
67 | - 'T_SR_EQUAL' => true, |
|
68 | - 'T_COALESCE_EQUAL' => true, |
|
69 | - 'T_ZSR_EQUAL' => true, |
|
70 | - ); |
|
71 | - |
|
72 | - /** |
|
73 | - * Returns an array of tokens this test wants to listen for. |
|
74 | - * |
|
75 | - * @return array |
|
76 | - */ |
|
77 | - public function register() |
|
78 | - { |
|
79 | - return array( |
|
80 | - \T_STRING, |
|
81 | - \T_VARIABLE, |
|
82 | - ); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Processes this test, when one of its tokens is encountered. |
|
87 | - * |
|
88 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
89 | - * @param int $stackPtr The position of the current token |
|
90 | - * in the stack passed in $tokens. |
|
91 | - * |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - public function process(File $phpcsFile, $stackPtr) |
|
95 | - { |
|
96 | - if ($this->supportsAbove('5.3') === false) { |
|
97 | - return; |
|
98 | - } |
|
99 | - |
|
100 | - $tokens = $phpcsFile->getTokens(); |
|
101 | - |
|
102 | - // Skip tokens that are the names of functions or classes |
|
103 | - // within their definitions. For example: function myFunction... |
|
104 | - // "myFunction" is T_STRING but we should skip because it is not a |
|
105 | - // function or method *call*. |
|
106 | - $findTokens = Tokens::$emptyTokens; |
|
107 | - $findTokens[] = \T_BITWISE_AND; |
|
108 | - |
|
109 | - $prevNonEmpty = $phpcsFile->findPrevious( |
|
110 | - $findTokens, |
|
111 | - ($stackPtr - 1), |
|
112 | - null, |
|
113 | - true |
|
114 | - ); |
|
115 | - |
|
116 | - if ($prevNonEmpty !== false && \in_array($tokens[$prevNonEmpty]['type'], array('T_FUNCTION', 'T_CLASS', 'T_INTERFACE', 'T_TRAIT'), true)) { |
|
117 | - return; |
|
118 | - } |
|
119 | - |
|
120 | - // If the next non-whitespace token after the function or method call |
|
121 | - // is not an opening parenthesis then it can't really be a *call*. |
|
122 | - $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
123 | - |
|
124 | - if ($openBracket === false || $tokens[$openBracket]['code'] !== \T_OPEN_PARENTHESIS |
|
125 | - || isset($tokens[$openBracket]['parenthesis_closer']) === false |
|
126 | - ) { |
|
127 | - return; |
|
128 | - } |
|
129 | - |
|
130 | - // Get the function call parameters. |
|
131 | - $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
|
132 | - if (\count($parameters) === 0) { |
|
133 | - return; |
|
134 | - } |
|
135 | - |
|
136 | - // Which nesting level is the one we are interested in ? |
|
137 | - $nestedParenthesisCount = 1; |
|
138 | - if (isset($tokens[$openBracket]['nested_parenthesis'])) { |
|
139 | - $nestedParenthesisCount = \count($tokens[$openBracket]['nested_parenthesis']) + 1; |
|
140 | - } |
|
141 | - |
|
142 | - foreach ($parameters as $parameter) { |
|
143 | - if ($this->isCallTimePassByReferenceParam($phpcsFile, $parameter, $nestedParenthesisCount) === true) { |
|
144 | - // T_BITWISE_AND represents a pass-by-reference. |
|
145 | - $error = 'Using a call-time pass-by-reference is deprecated since PHP 5.3'; |
|
146 | - $isError = false; |
|
147 | - $errorCode = 'Deprecated'; |
|
148 | - |
|
149 | - if ($this->supportsAbove('5.4')) { |
|
150 | - $error .= ' and prohibited since PHP 5.4'; |
|
151 | - $isError = true; |
|
152 | - $errorCode = 'NotAllowed'; |
|
153 | - } |
|
154 | - |
|
155 | - $this->addMessage($phpcsFile, $error, $parameter['start'], $isError, $errorCode); |
|
156 | - } |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * Determine whether a parameter is passed by reference. |
|
163 | - * |
|
164 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
165 | - * @param array $parameter Information on the current parameter |
|
166 | - * to be examined. |
|
167 | - * @param int $nestingLevel Target nesting level. |
|
168 | - * |
|
169 | - * @return bool |
|
170 | - */ |
|
171 | - protected function isCallTimePassByReferenceParam(File $phpcsFile, $parameter, $nestingLevel) |
|
172 | - { |
|
173 | - $tokens = $phpcsFile->getTokens(); |
|
174 | - |
|
175 | - $searchStartToken = $parameter['start'] - 1; |
|
176 | - $searchEndToken = $parameter['end'] + 1; |
|
177 | - $nextVariable = $searchStartToken; |
|
178 | - do { |
|
179 | - $nextVariable = $phpcsFile->findNext(array(\T_VARIABLE, \T_OPEN_SHORT_ARRAY, \T_CLOSURE), ($nextVariable + 1), $searchEndToken); |
|
180 | - if ($nextVariable === false) { |
|
181 | - return false; |
|
182 | - } |
|
183 | - |
|
184 | - // Ignore anything within short array definition brackets. |
|
185 | - if ($tokens[$nextVariable]['type'] === 'T_OPEN_SHORT_ARRAY' |
|
186 | - && (isset($tokens[$nextVariable]['bracket_opener']) |
|
187 | - && $tokens[$nextVariable]['bracket_opener'] === $nextVariable) |
|
188 | - && isset($tokens[$nextVariable]['bracket_closer']) |
|
189 | - ) { |
|
190 | - // Skip forward to the end of the short array definition. |
|
191 | - $nextVariable = $tokens[$nextVariable]['bracket_closer']; |
|
192 | - continue; |
|
193 | - } |
|
194 | - |
|
195 | - // Skip past closures passed as function parameters. |
|
196 | - if ($tokens[$nextVariable]['type'] === 'T_CLOSURE' |
|
197 | - && (isset($tokens[$nextVariable]['scope_condition']) |
|
198 | - && $tokens[$nextVariable]['scope_condition'] === $nextVariable) |
|
199 | - && isset($tokens[$nextVariable]['scope_closer']) |
|
200 | - ) { |
|
201 | - // Skip forward to the end of the closure declaration. |
|
202 | - $nextVariable = $tokens[$nextVariable]['scope_closer']; |
|
203 | - continue; |
|
204 | - } |
|
205 | - |
|
206 | - // Make sure the variable belongs directly to this function call |
|
207 | - // and is not inside a nested function call or array. |
|
208 | - if (isset($tokens[$nextVariable]['nested_parenthesis']) === false |
|
209 | - || (\count($tokens[$nextVariable]['nested_parenthesis']) !== $nestingLevel) |
|
210 | - ) { |
|
211 | - continue; |
|
212 | - } |
|
213 | - |
|
214 | - // Checking this: $value = my_function(...[*]$arg...). |
|
215 | - $tokenBefore = $phpcsFile->findPrevious( |
|
216 | - Tokens::$emptyTokens, |
|
217 | - ($nextVariable - 1), |
|
218 | - $searchStartToken, |
|
219 | - true |
|
220 | - ); |
|
221 | - |
|
222 | - if ($tokenBefore === false || $tokens[$tokenBefore]['code'] !== \T_BITWISE_AND) { |
|
223 | - // Nothing before the token or no &. |
|
224 | - continue; |
|
225 | - } |
|
226 | - |
|
227 | - if ($phpcsFile->isReference($tokenBefore) === false) { |
|
228 | - continue; |
|
229 | - } |
|
230 | - |
|
231 | - // Checking this: $value = my_function(...[*]&$arg...). |
|
232 | - $tokenBefore = $phpcsFile->findPrevious( |
|
233 | - Tokens::$emptyTokens, |
|
234 | - ($tokenBefore - 1), |
|
235 | - $searchStartToken, |
|
236 | - true |
|
237 | - ); |
|
238 | - |
|
239 | - // Prevent false positive on assign by reference and compare with reference |
|
240 | - // within function call parameters. |
|
241 | - if (isset($this->assignOrCompare[$tokens[$tokenBefore]['type']])) { |
|
242 | - continue; |
|
243 | - } |
|
244 | - |
|
245 | - // The found T_BITWISE_AND represents a pass-by-reference. |
|
246 | - return true; |
|
247 | - |
|
248 | - } while ($nextVariable < $searchEndToken); |
|
249 | - |
|
250 | - // This code should never be reached, but here in case of weird bugs. |
|
251 | - return false; |
|
252 | - } |
|
36 | + /** |
|
37 | + * Tokens that represent assignments or equality comparisons. |
|
38 | + * |
|
39 | + * Near duplicate of Tokens::$assignmentTokens + Tokens::$equalityTokens. |
|
40 | + * Copied in for PHPCS cross-version compatibility. |
|
41 | + * |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + private $assignOrCompare = array( |
|
45 | + // Equality tokens. |
|
46 | + 'T_IS_EQUAL' => true, |
|
47 | + 'T_IS_NOT_EQUAL' => true, |
|
48 | + 'T_IS_IDENTICAL' => true, |
|
49 | + 'T_IS_NOT_IDENTICAL' => true, |
|
50 | + 'T_IS_SMALLER_OR_EQUAL' => true, |
|
51 | + 'T_IS_GREATER_OR_EQUAL' => true, |
|
52 | + |
|
53 | + // Assignment tokens. |
|
54 | + 'T_EQUAL' => true, |
|
55 | + 'T_AND_EQUAL' => true, |
|
56 | + 'T_OR_EQUAL' => true, |
|
57 | + 'T_CONCAT_EQUAL' => true, |
|
58 | + 'T_DIV_EQUAL' => true, |
|
59 | + 'T_MINUS_EQUAL' => true, |
|
60 | + 'T_POW_EQUAL' => true, |
|
61 | + 'T_MOD_EQUAL' => true, |
|
62 | + 'T_MUL_EQUAL' => true, |
|
63 | + 'T_PLUS_EQUAL' => true, |
|
64 | + 'T_XOR_EQUAL' => true, |
|
65 | + 'T_DOUBLE_ARROW' => true, |
|
66 | + 'T_SL_EQUAL' => true, |
|
67 | + 'T_SR_EQUAL' => true, |
|
68 | + 'T_COALESCE_EQUAL' => true, |
|
69 | + 'T_ZSR_EQUAL' => true, |
|
70 | + ); |
|
71 | + |
|
72 | + /** |
|
73 | + * Returns an array of tokens this test wants to listen for. |
|
74 | + * |
|
75 | + * @return array |
|
76 | + */ |
|
77 | + public function register() |
|
78 | + { |
|
79 | + return array( |
|
80 | + \T_STRING, |
|
81 | + \T_VARIABLE, |
|
82 | + ); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Processes this test, when one of its tokens is encountered. |
|
87 | + * |
|
88 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
89 | + * @param int $stackPtr The position of the current token |
|
90 | + * in the stack passed in $tokens. |
|
91 | + * |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + public function process(File $phpcsFile, $stackPtr) |
|
95 | + { |
|
96 | + if ($this->supportsAbove('5.3') === false) { |
|
97 | + return; |
|
98 | + } |
|
99 | + |
|
100 | + $tokens = $phpcsFile->getTokens(); |
|
101 | + |
|
102 | + // Skip tokens that are the names of functions or classes |
|
103 | + // within their definitions. For example: function myFunction... |
|
104 | + // "myFunction" is T_STRING but we should skip because it is not a |
|
105 | + // function or method *call*. |
|
106 | + $findTokens = Tokens::$emptyTokens; |
|
107 | + $findTokens[] = \T_BITWISE_AND; |
|
108 | + |
|
109 | + $prevNonEmpty = $phpcsFile->findPrevious( |
|
110 | + $findTokens, |
|
111 | + ($stackPtr - 1), |
|
112 | + null, |
|
113 | + true |
|
114 | + ); |
|
115 | + |
|
116 | + if ($prevNonEmpty !== false && \in_array($tokens[$prevNonEmpty]['type'], array('T_FUNCTION', 'T_CLASS', 'T_INTERFACE', 'T_TRAIT'), true)) { |
|
117 | + return; |
|
118 | + } |
|
119 | + |
|
120 | + // If the next non-whitespace token after the function or method call |
|
121 | + // is not an opening parenthesis then it can't really be a *call*. |
|
122 | + $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
123 | + |
|
124 | + if ($openBracket === false || $tokens[$openBracket]['code'] !== \T_OPEN_PARENTHESIS |
|
125 | + || isset($tokens[$openBracket]['parenthesis_closer']) === false |
|
126 | + ) { |
|
127 | + return; |
|
128 | + } |
|
129 | + |
|
130 | + // Get the function call parameters. |
|
131 | + $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
|
132 | + if (\count($parameters) === 0) { |
|
133 | + return; |
|
134 | + } |
|
135 | + |
|
136 | + // Which nesting level is the one we are interested in ? |
|
137 | + $nestedParenthesisCount = 1; |
|
138 | + if (isset($tokens[$openBracket]['nested_parenthesis'])) { |
|
139 | + $nestedParenthesisCount = \count($tokens[$openBracket]['nested_parenthesis']) + 1; |
|
140 | + } |
|
141 | + |
|
142 | + foreach ($parameters as $parameter) { |
|
143 | + if ($this->isCallTimePassByReferenceParam($phpcsFile, $parameter, $nestedParenthesisCount) === true) { |
|
144 | + // T_BITWISE_AND represents a pass-by-reference. |
|
145 | + $error = 'Using a call-time pass-by-reference is deprecated since PHP 5.3'; |
|
146 | + $isError = false; |
|
147 | + $errorCode = 'Deprecated'; |
|
148 | + |
|
149 | + if ($this->supportsAbove('5.4')) { |
|
150 | + $error .= ' and prohibited since PHP 5.4'; |
|
151 | + $isError = true; |
|
152 | + $errorCode = 'NotAllowed'; |
|
153 | + } |
|
154 | + |
|
155 | + $this->addMessage($phpcsFile, $error, $parameter['start'], $isError, $errorCode); |
|
156 | + } |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * Determine whether a parameter is passed by reference. |
|
163 | + * |
|
164 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
165 | + * @param array $parameter Information on the current parameter |
|
166 | + * to be examined. |
|
167 | + * @param int $nestingLevel Target nesting level. |
|
168 | + * |
|
169 | + * @return bool |
|
170 | + */ |
|
171 | + protected function isCallTimePassByReferenceParam(File $phpcsFile, $parameter, $nestingLevel) |
|
172 | + { |
|
173 | + $tokens = $phpcsFile->getTokens(); |
|
174 | + |
|
175 | + $searchStartToken = $parameter['start'] - 1; |
|
176 | + $searchEndToken = $parameter['end'] + 1; |
|
177 | + $nextVariable = $searchStartToken; |
|
178 | + do { |
|
179 | + $nextVariable = $phpcsFile->findNext(array(\T_VARIABLE, \T_OPEN_SHORT_ARRAY, \T_CLOSURE), ($nextVariable + 1), $searchEndToken); |
|
180 | + if ($nextVariable === false) { |
|
181 | + return false; |
|
182 | + } |
|
183 | + |
|
184 | + // Ignore anything within short array definition brackets. |
|
185 | + if ($tokens[$nextVariable]['type'] === 'T_OPEN_SHORT_ARRAY' |
|
186 | + && (isset($tokens[$nextVariable]['bracket_opener']) |
|
187 | + && $tokens[$nextVariable]['bracket_opener'] === $nextVariable) |
|
188 | + && isset($tokens[$nextVariable]['bracket_closer']) |
|
189 | + ) { |
|
190 | + // Skip forward to the end of the short array definition. |
|
191 | + $nextVariable = $tokens[$nextVariable]['bracket_closer']; |
|
192 | + continue; |
|
193 | + } |
|
194 | + |
|
195 | + // Skip past closures passed as function parameters. |
|
196 | + if ($tokens[$nextVariable]['type'] === 'T_CLOSURE' |
|
197 | + && (isset($tokens[$nextVariable]['scope_condition']) |
|
198 | + && $tokens[$nextVariable]['scope_condition'] === $nextVariable) |
|
199 | + && isset($tokens[$nextVariable]['scope_closer']) |
|
200 | + ) { |
|
201 | + // Skip forward to the end of the closure declaration. |
|
202 | + $nextVariable = $tokens[$nextVariable]['scope_closer']; |
|
203 | + continue; |
|
204 | + } |
|
205 | + |
|
206 | + // Make sure the variable belongs directly to this function call |
|
207 | + // and is not inside a nested function call or array. |
|
208 | + if (isset($tokens[$nextVariable]['nested_parenthesis']) === false |
|
209 | + || (\count($tokens[$nextVariable]['nested_parenthesis']) !== $nestingLevel) |
|
210 | + ) { |
|
211 | + continue; |
|
212 | + } |
|
213 | + |
|
214 | + // Checking this: $value = my_function(...[*]$arg...). |
|
215 | + $tokenBefore = $phpcsFile->findPrevious( |
|
216 | + Tokens::$emptyTokens, |
|
217 | + ($nextVariable - 1), |
|
218 | + $searchStartToken, |
|
219 | + true |
|
220 | + ); |
|
221 | + |
|
222 | + if ($tokenBefore === false || $tokens[$tokenBefore]['code'] !== \T_BITWISE_AND) { |
|
223 | + // Nothing before the token or no &. |
|
224 | + continue; |
|
225 | + } |
|
226 | + |
|
227 | + if ($phpcsFile->isReference($tokenBefore) === false) { |
|
228 | + continue; |
|
229 | + } |
|
230 | + |
|
231 | + // Checking this: $value = my_function(...[*]&$arg...). |
|
232 | + $tokenBefore = $phpcsFile->findPrevious( |
|
233 | + Tokens::$emptyTokens, |
|
234 | + ($tokenBefore - 1), |
|
235 | + $searchStartToken, |
|
236 | + true |
|
237 | + ); |
|
238 | + |
|
239 | + // Prevent false positive on assign by reference and compare with reference |
|
240 | + // within function call parameters. |
|
241 | + if (isset($this->assignOrCompare[$tokens[$tokenBefore]['type']])) { |
|
242 | + continue; |
|
243 | + } |
|
244 | + |
|
245 | + // The found T_BITWISE_AND represents a pass-by-reference. |
|
246 | + return true; |
|
247 | + |
|
248 | + } while ($nextVariable < $searchEndToken); |
|
249 | + |
|
250 | + // This code should never be reached, but here in case of weird bugs. |
|
251 | + return false; |
|
252 | + } |
|
253 | 253 | } |
@@ -32,82 +32,82 @@ |
||
32 | 32 | class NewClassMemberAccessSniff extends Sniff |
33 | 33 | { |
34 | 34 | |
35 | - /** |
|
36 | - * Returns an array of tokens this test wants to listen for. |
|
37 | - * |
|
38 | - * @return array |
|
39 | - */ |
|
40 | - public function register() |
|
41 | - { |
|
42 | - return array( |
|
43 | - \T_NEW, |
|
44 | - \T_CLONE, |
|
45 | - ); |
|
46 | - } |
|
35 | + /** |
|
36 | + * Returns an array of tokens this test wants to listen for. |
|
37 | + * |
|
38 | + * @return array |
|
39 | + */ |
|
40 | + public function register() |
|
41 | + { |
|
42 | + return array( |
|
43 | + \T_NEW, |
|
44 | + \T_CLONE, |
|
45 | + ); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Processes this test, when one of its tokens is encountered. |
|
50 | - * |
|
51 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
52 | - * @param int $stackPtr The position of the current token in the |
|
53 | - * stack passed in $tokens. |
|
54 | - * |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function process(File $phpcsFile, $stackPtr) |
|
58 | - { |
|
59 | - $tokens = $phpcsFile->getTokens(); |
|
48 | + /** |
|
49 | + * Processes this test, when one of its tokens is encountered. |
|
50 | + * |
|
51 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
52 | + * @param int $stackPtr The position of the current token in the |
|
53 | + * stack passed in $tokens. |
|
54 | + * |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function process(File $phpcsFile, $stackPtr) |
|
58 | + { |
|
59 | + $tokens = $phpcsFile->getTokens(); |
|
60 | 60 | |
61 | - if ($tokens[$stackPtr]['code'] === \T_NEW && $this->supportsBelow('5.3') !== true) { |
|
62 | - return; |
|
63 | - } elseif ($tokens[$stackPtr]['code'] === \T_CLONE && $this->supportsBelow('5.6') !== true) { |
|
64 | - return; |
|
65 | - } |
|
61 | + if ($tokens[$stackPtr]['code'] === \T_NEW && $this->supportsBelow('5.3') !== true) { |
|
62 | + return; |
|
63 | + } elseif ($tokens[$stackPtr]['code'] === \T_CLONE && $this->supportsBelow('5.6') !== true) { |
|
64 | + return; |
|
65 | + } |
|
66 | 66 | |
67 | - if (isset($tokens[$stackPtr]['nested_parenthesis']) === false) { |
|
68 | - // The `new className/clone $a` has to be in parentheses, without is not supported. |
|
69 | - return; |
|
70 | - } |
|
67 | + if (isset($tokens[$stackPtr]['nested_parenthesis']) === false) { |
|
68 | + // The `new className/clone $a` has to be in parentheses, without is not supported. |
|
69 | + return; |
|
70 | + } |
|
71 | 71 | |
72 | - $parenthesisCloser = end($tokens[$stackPtr]['nested_parenthesis']); |
|
73 | - $parenthesisOpener = key($tokens[$stackPtr]['nested_parenthesis']); |
|
72 | + $parenthesisCloser = end($tokens[$stackPtr]['nested_parenthesis']); |
|
73 | + $parenthesisOpener = key($tokens[$stackPtr]['nested_parenthesis']); |
|
74 | 74 | |
75 | - if (isset($tokens[$parenthesisOpener]['parenthesis_owner']) === true) { |
|
76 | - // If there is an owner, these parentheses are for a different purpose. |
|
77 | - return; |
|
78 | - } |
|
75 | + if (isset($tokens[$parenthesisOpener]['parenthesis_owner']) === true) { |
|
76 | + // If there is an owner, these parentheses are for a different purpose. |
|
77 | + return; |
|
78 | + } |
|
79 | 79 | |
80 | - $prevBeforeParenthesis = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($parenthesisOpener - 1), null, true); |
|
81 | - if ($prevBeforeParenthesis !== false && $tokens[$prevBeforeParenthesis]['code'] === \T_STRING) { |
|
82 | - // This is most likely a function call with the new/cloned object as a parameter. |
|
83 | - return; |
|
84 | - } |
|
80 | + $prevBeforeParenthesis = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($parenthesisOpener - 1), null, true); |
|
81 | + if ($prevBeforeParenthesis !== false && $tokens[$prevBeforeParenthesis]['code'] === \T_STRING) { |
|
82 | + // This is most likely a function call with the new/cloned object as a parameter. |
|
83 | + return; |
|
84 | + } |
|
85 | 85 | |
86 | - $nextAfterParenthesis = $phpcsFile->findNext(Tokens::$emptyTokens, ($parenthesisCloser + 1), null, true); |
|
87 | - if ($nextAfterParenthesis === false) { |
|
88 | - // Live coding. |
|
89 | - return; |
|
90 | - } |
|
86 | + $nextAfterParenthesis = $phpcsFile->findNext(Tokens::$emptyTokens, ($parenthesisCloser + 1), null, true); |
|
87 | + if ($nextAfterParenthesis === false) { |
|
88 | + // Live coding. |
|
89 | + return; |
|
90 | + } |
|
91 | 91 | |
92 | - if ($tokens[$nextAfterParenthesis]['code'] !== \T_OBJECT_OPERATOR |
|
93 | - && $tokens[$nextAfterParenthesis]['code'] !== \T_OPEN_SQUARE_BRACKET |
|
94 | - ) { |
|
95 | - return; |
|
96 | - } |
|
92 | + if ($tokens[$nextAfterParenthesis]['code'] !== \T_OBJECT_OPERATOR |
|
93 | + && $tokens[$nextAfterParenthesis]['code'] !== \T_OPEN_SQUARE_BRACKET |
|
94 | + ) { |
|
95 | + return; |
|
96 | + } |
|
97 | 97 | |
98 | - $data = array('instantiation', '5.3'); |
|
99 | - $errorCode = 'OnNewFound'; |
|
98 | + $data = array('instantiation', '5.3'); |
|
99 | + $errorCode = 'OnNewFound'; |
|
100 | 100 | |
101 | - if ($tokens[$stackPtr]['code'] === \T_CLONE) { |
|
102 | - $data = array('cloning', '5.6'); |
|
103 | - $errorCode = 'OnCloneFound'; |
|
104 | - } |
|
101 | + if ($tokens[$stackPtr]['code'] === \T_CLONE) { |
|
102 | + $data = array('cloning', '5.6'); |
|
103 | + $errorCode = 'OnCloneFound'; |
|
104 | + } |
|
105 | 105 | |
106 | - $phpcsFile->addError( |
|
107 | - 'Class member access on object %s was not supported in PHP %s or earlier', |
|
108 | - $parenthesisCloser, |
|
109 | - $errorCode, |
|
110 | - $data |
|
111 | - ); |
|
112 | - } |
|
106 | + $phpcsFile->addError( |
|
107 | + 'Class member access on object %s was not supported in PHP %s or earlier', |
|
108 | + $parenthesisCloser, |
|
109 | + $errorCode, |
|
110 | + $data |
|
111 | + ); |
|
112 | + } |
|
113 | 113 | } |