@@ -28,184 +28,184 @@ |
||
28 | 28 | */ |
29 | 29 | class NewKeyedListSniff extends Sniff |
30 | 30 | { |
31 | - /** |
|
32 | - * Tokens which represent the start of a list construct. |
|
33 | - * |
|
34 | - * @var array |
|
35 | - */ |
|
36 | - protected $sniffTargets = array( |
|
37 | - \T_LIST => \T_LIST, |
|
38 | - \T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY, |
|
39 | - ); |
|
40 | - |
|
41 | - /** |
|
42 | - * The token(s) within the list construct which is being targeted. |
|
43 | - * |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - protected $targetsInList = array( |
|
47 | - \T_DOUBLE_ARROW => \T_DOUBLE_ARROW, |
|
48 | - ); |
|
49 | - |
|
50 | - /** |
|
51 | - * All tokens needed to walk through the list construct and |
|
52 | - * determine whether the target token is contained within. |
|
53 | - * |
|
54 | - * Set by the setUpAllTargets() method which is called from within register(). |
|
55 | - * |
|
56 | - * @var array |
|
57 | - */ |
|
58 | - protected $allTargets; |
|
59 | - |
|
60 | - |
|
61 | - /** |
|
62 | - * Returns an array of tokens this test wants to listen for. |
|
63 | - * |
|
64 | - * @return array |
|
65 | - */ |
|
66 | - public function register() |
|
67 | - { |
|
68 | - $this->setUpAllTargets(); |
|
69 | - |
|
70 | - return $this->sniffTargets; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Prepare the $allTargets array only once. |
|
75 | - * |
|
76 | - * @return array |
|
77 | - */ |
|
78 | - public function setUpAllTargets() |
|
79 | - { |
|
80 | - $this->allTargets = $this->sniffTargets + $this->targetsInList; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Do a version check to determine if this sniff needs to run at all. |
|
85 | - * |
|
86 | - * @return bool |
|
87 | - */ |
|
88 | - protected function bowOutEarly() |
|
89 | - { |
|
90 | - return ($this->supportsBelow('7.0') === false); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Processes this test, when one of its tokens is encountered. |
|
95 | - * |
|
96 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
97 | - * @param int $stackPtr The position of the current token in the |
|
98 | - * stack passed in $tokens. |
|
99 | - * |
|
100 | - * @return void |
|
101 | - */ |
|
102 | - public function process(File $phpcsFile, $stackPtr) |
|
103 | - { |
|
104 | - if ($this->bowOutEarly() === true) { |
|
105 | - return; |
|
106 | - } |
|
107 | - |
|
108 | - $tokens = $phpcsFile->getTokens(); |
|
109 | - |
|
110 | - if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY |
|
111 | - && $this->isShortList($phpcsFile, $stackPtr) === false |
|
112 | - ) { |
|
113 | - // Short array, not short list. |
|
114 | - return; |
|
115 | - } |
|
116 | - |
|
117 | - if ($tokens[$stackPtr]['code'] === \T_LIST) { |
|
118 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
119 | - if ($nextNonEmpty === false |
|
120 | - || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS |
|
121 | - || isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false |
|
122 | - ) { |
|
123 | - // Parse error or live coding. |
|
124 | - return; |
|
125 | - } |
|
126 | - |
|
127 | - $opener = $nextNonEmpty; |
|
128 | - $closer = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
129 | - } else { |
|
130 | - // Short list syntax. |
|
131 | - $opener = $stackPtr; |
|
132 | - |
|
133 | - if (isset($tokens[$stackPtr]['bracket_closer'])) { |
|
134 | - $closer = $tokens[$stackPtr]['bracket_closer']; |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - if (isset($opener, $closer) === false) { |
|
139 | - return; |
|
140 | - } |
|
141 | - |
|
142 | - $this->examineList($phpcsFile, $opener, $closer); |
|
143 | - } |
|
144 | - |
|
145 | - |
|
146 | - /** |
|
147 | - * Examine the contents of a list construct to determine whether an error needs to be thrown. |
|
148 | - * |
|
149 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
150 | - * @param int $opener The position of the list open token. |
|
151 | - * @param int $closer The position of the list close token. |
|
152 | - * |
|
153 | - * @return void |
|
154 | - */ |
|
155 | - protected function examineList(File $phpcsFile, $opener, $closer) |
|
156 | - { |
|
157 | - $start = $opener; |
|
158 | - while (($start = $this->hasTargetInList($phpcsFile, $start, $closer)) !== false) { |
|
159 | - $phpcsFile->addError( |
|
160 | - 'Specifying keys in list constructs is not supported in PHP 7.0 or earlier.', |
|
161 | - $start, |
|
162 | - 'Found' |
|
163 | - ); |
|
164 | - } |
|
165 | - } |
|
166 | - |
|
167 | - |
|
168 | - /** |
|
169 | - * Check whether a certain target token exists within a list construct. |
|
170 | - * |
|
171 | - * Skips past nested list constructs, so these can be examined based on their own token. |
|
172 | - * |
|
173 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
174 | - * @param int $start The position of the list open token or a token |
|
175 | - * within the list to start (resume) the examination from. |
|
176 | - * @param int $closer The position of the list close token. |
|
177 | - * |
|
178 | - * @return int|bool Stack pointer to the target token if encountered. False otherwise. |
|
179 | - */ |
|
180 | - protected function hasTargetInList(File $phpcsFile, $start, $closer) |
|
181 | - { |
|
182 | - $tokens = $phpcsFile->getTokens(); |
|
183 | - |
|
184 | - for ($i = ($start + 1); $i < $closer; $i++) { |
|
185 | - if (isset($this->allTargets[$tokens[$i]['code']]) === false) { |
|
186 | - continue; |
|
187 | - } |
|
188 | - |
|
189 | - if (isset($this->targetsInList[$tokens[$i]['code']]) === true) { |
|
190 | - return $i; |
|
191 | - } |
|
192 | - |
|
193 | - // Skip past nested list constructs. |
|
194 | - if ($tokens[$i]['code'] === \T_LIST) { |
|
195 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
196 | - if ($nextNonEmpty !== false |
|
197 | - && $tokens[$nextNonEmpty]['code'] === \T_OPEN_PARENTHESIS |
|
198 | - && isset($tokens[$nextNonEmpty]['parenthesis_closer']) === true |
|
199 | - ) { |
|
200 | - $i = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
201 | - } |
|
202 | - } elseif ($tokens[$i]['code'] === \T_OPEN_SHORT_ARRAY |
|
203 | - && isset($tokens[$i]['bracket_closer']) |
|
204 | - ) { |
|
205 | - $i = $tokens[$i]['bracket_closer']; |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - return false; |
|
210 | - } |
|
31 | + /** |
|
32 | + * Tokens which represent the start of a list construct. |
|
33 | + * |
|
34 | + * @var array |
|
35 | + */ |
|
36 | + protected $sniffTargets = array( |
|
37 | + \T_LIST => \T_LIST, |
|
38 | + \T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY, |
|
39 | + ); |
|
40 | + |
|
41 | + /** |
|
42 | + * The token(s) within the list construct which is being targeted. |
|
43 | + * |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + protected $targetsInList = array( |
|
47 | + \T_DOUBLE_ARROW => \T_DOUBLE_ARROW, |
|
48 | + ); |
|
49 | + |
|
50 | + /** |
|
51 | + * All tokens needed to walk through the list construct and |
|
52 | + * determine whether the target token is contained within. |
|
53 | + * |
|
54 | + * Set by the setUpAllTargets() method which is called from within register(). |
|
55 | + * |
|
56 | + * @var array |
|
57 | + */ |
|
58 | + protected $allTargets; |
|
59 | + |
|
60 | + |
|
61 | + /** |
|
62 | + * Returns an array of tokens this test wants to listen for. |
|
63 | + * |
|
64 | + * @return array |
|
65 | + */ |
|
66 | + public function register() |
|
67 | + { |
|
68 | + $this->setUpAllTargets(); |
|
69 | + |
|
70 | + return $this->sniffTargets; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Prepare the $allTargets array only once. |
|
75 | + * |
|
76 | + * @return array |
|
77 | + */ |
|
78 | + public function setUpAllTargets() |
|
79 | + { |
|
80 | + $this->allTargets = $this->sniffTargets + $this->targetsInList; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Do a version check to determine if this sniff needs to run at all. |
|
85 | + * |
|
86 | + * @return bool |
|
87 | + */ |
|
88 | + protected function bowOutEarly() |
|
89 | + { |
|
90 | + return ($this->supportsBelow('7.0') === false); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Processes this test, when one of its tokens is encountered. |
|
95 | + * |
|
96 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
97 | + * @param int $stackPtr The position of the current token in the |
|
98 | + * stack passed in $tokens. |
|
99 | + * |
|
100 | + * @return void |
|
101 | + */ |
|
102 | + public function process(File $phpcsFile, $stackPtr) |
|
103 | + { |
|
104 | + if ($this->bowOutEarly() === true) { |
|
105 | + return; |
|
106 | + } |
|
107 | + |
|
108 | + $tokens = $phpcsFile->getTokens(); |
|
109 | + |
|
110 | + if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY |
|
111 | + && $this->isShortList($phpcsFile, $stackPtr) === false |
|
112 | + ) { |
|
113 | + // Short array, not short list. |
|
114 | + return; |
|
115 | + } |
|
116 | + |
|
117 | + if ($tokens[$stackPtr]['code'] === \T_LIST) { |
|
118 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
119 | + if ($nextNonEmpty === false |
|
120 | + || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS |
|
121 | + || isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false |
|
122 | + ) { |
|
123 | + // Parse error or live coding. |
|
124 | + return; |
|
125 | + } |
|
126 | + |
|
127 | + $opener = $nextNonEmpty; |
|
128 | + $closer = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
129 | + } else { |
|
130 | + // Short list syntax. |
|
131 | + $opener = $stackPtr; |
|
132 | + |
|
133 | + if (isset($tokens[$stackPtr]['bracket_closer'])) { |
|
134 | + $closer = $tokens[$stackPtr]['bracket_closer']; |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + if (isset($opener, $closer) === false) { |
|
139 | + return; |
|
140 | + } |
|
141 | + |
|
142 | + $this->examineList($phpcsFile, $opener, $closer); |
|
143 | + } |
|
144 | + |
|
145 | + |
|
146 | + /** |
|
147 | + * Examine the contents of a list construct to determine whether an error needs to be thrown. |
|
148 | + * |
|
149 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
150 | + * @param int $opener The position of the list open token. |
|
151 | + * @param int $closer The position of the list close token. |
|
152 | + * |
|
153 | + * @return void |
|
154 | + */ |
|
155 | + protected function examineList(File $phpcsFile, $opener, $closer) |
|
156 | + { |
|
157 | + $start = $opener; |
|
158 | + while (($start = $this->hasTargetInList($phpcsFile, $start, $closer)) !== false) { |
|
159 | + $phpcsFile->addError( |
|
160 | + 'Specifying keys in list constructs is not supported in PHP 7.0 or earlier.', |
|
161 | + $start, |
|
162 | + 'Found' |
|
163 | + ); |
|
164 | + } |
|
165 | + } |
|
166 | + |
|
167 | + |
|
168 | + /** |
|
169 | + * Check whether a certain target token exists within a list construct. |
|
170 | + * |
|
171 | + * Skips past nested list constructs, so these can be examined based on their own token. |
|
172 | + * |
|
173 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
174 | + * @param int $start The position of the list open token or a token |
|
175 | + * within the list to start (resume) the examination from. |
|
176 | + * @param int $closer The position of the list close token. |
|
177 | + * |
|
178 | + * @return int|bool Stack pointer to the target token if encountered. False otherwise. |
|
179 | + */ |
|
180 | + protected function hasTargetInList(File $phpcsFile, $start, $closer) |
|
181 | + { |
|
182 | + $tokens = $phpcsFile->getTokens(); |
|
183 | + |
|
184 | + for ($i = ($start + 1); $i < $closer; $i++) { |
|
185 | + if (isset($this->allTargets[$tokens[$i]['code']]) === false) { |
|
186 | + continue; |
|
187 | + } |
|
188 | + |
|
189 | + if (isset($this->targetsInList[$tokens[$i]['code']]) === true) { |
|
190 | + return $i; |
|
191 | + } |
|
192 | + |
|
193 | + // Skip past nested list constructs. |
|
194 | + if ($tokens[$i]['code'] === \T_LIST) { |
|
195 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
196 | + if ($nextNonEmpty !== false |
|
197 | + && $tokens[$nextNonEmpty]['code'] === \T_OPEN_PARENTHESIS |
|
198 | + && isset($tokens[$nextNonEmpty]['parenthesis_closer']) === true |
|
199 | + ) { |
|
200 | + $i = $tokens[$nextNonEmpty]['parenthesis_closer']; |
|
201 | + } |
|
202 | + } elseif ($tokens[$i]['code'] === \T_OPEN_SHORT_ARRAY |
|
203 | + && isset($tokens[$i]['bracket_closer']) |
|
204 | + ) { |
|
205 | + $i = $tokens[$i]['bracket_closer']; |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + return false; |
|
210 | + } |
|
211 | 211 | } |
@@ -30,51 +30,51 @@ |
||
30 | 30 | class NewShortListSniff 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(\T_OPEN_SHORT_ARRAY); |
|
41 | - } |
|
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(\T_OPEN_SHORT_ARRAY); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Processes this test, when one of its tokens is encountered. |
|
45 | - * |
|
46 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
47 | - * @param int $stackPtr The position of the current token in the |
|
48 | - * stack passed in $tokens. |
|
49 | - * |
|
50 | - * @return int|void Integer stack pointer to skip forward or void to continue |
|
51 | - * normal file processing. |
|
52 | - */ |
|
53 | - public function process(File $phpcsFile, $stackPtr) |
|
54 | - { |
|
55 | - if ($this->supportsBelow('7.0') === false) { |
|
56 | - return; |
|
57 | - } |
|
43 | + /** |
|
44 | + * Processes this test, when one of its tokens is encountered. |
|
45 | + * |
|
46 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
47 | + * @param int $stackPtr The position of the current token in the |
|
48 | + * stack passed in $tokens. |
|
49 | + * |
|
50 | + * @return int|void Integer stack pointer to skip forward or void to continue |
|
51 | + * normal file processing. |
|
52 | + */ |
|
53 | + public function process(File $phpcsFile, $stackPtr) |
|
54 | + { |
|
55 | + if ($this->supportsBelow('7.0') === false) { |
|
56 | + return; |
|
57 | + } |
|
58 | 58 | |
59 | - if ($this->isShortList($phpcsFile, $stackPtr) === false) { |
|
60 | - return; |
|
61 | - } |
|
59 | + if ($this->isShortList($phpcsFile, $stackPtr) === false) { |
|
60 | + return; |
|
61 | + } |
|
62 | 62 | |
63 | - $tokens = $phpcsFile->getTokens(); |
|
64 | - $closer = $tokens[$stackPtr]['bracket_closer']; |
|
63 | + $tokens = $phpcsFile->getTokens(); |
|
64 | + $closer = $tokens[$stackPtr]['bracket_closer']; |
|
65 | 65 | |
66 | - $hasVariable = $phpcsFile->findNext(\T_VARIABLE, ($stackPtr + 1), $closer); |
|
67 | - if ($hasVariable === false) { |
|
68 | - // List syntax is only valid if there are variables in it. |
|
69 | - return; |
|
70 | - } |
|
66 | + $hasVariable = $phpcsFile->findNext(\T_VARIABLE, ($stackPtr + 1), $closer); |
|
67 | + if ($hasVariable === false) { |
|
68 | + // List syntax is only valid if there are variables in it. |
|
69 | + return; |
|
70 | + } |
|
71 | 71 | |
72 | - $phpcsFile->addError( |
|
73 | - 'The shorthand list syntax "[]" to destructure arrays is not available in PHP 7.0 or earlier.', |
|
74 | - $stackPtr, |
|
75 | - 'Found' |
|
76 | - ); |
|
72 | + $phpcsFile->addError( |
|
73 | + 'The shorthand list syntax "[]" to destructure arrays is not available in PHP 7.0 or earlier.', |
|
74 | + $stackPtr, |
|
75 | + 'Found' |
|
76 | + ); |
|
77 | 77 | |
78 | - return ($closer + 1); |
|
79 | - } |
|
78 | + return ($closer + 1); |
|
79 | + } |
|
80 | 80 | } |
@@ -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 | } |