Skip to content

Commit 10fadb1

Browse files
committed
Merge branch 'feature/pear-multilinecondition-phpcs-annotations' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 8c962cb + 933cb16 commit 10fadb1

File tree

6 files changed

+303
-15
lines changed

6 files changed

+303
-15
lines changed

src/Standards/PEAR/Sniffs/ControlStructures/MultiLineConditionSniff.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ public function process(File $phpcsFile, $stackPtr)
117117
if ($fix === true) {
118118
// Account for a comment at the end of the line.
119119
$next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
120-
if ($tokens[$next]['code'] !== T_COMMENT) {
120+
if ($tokens[$next]['code'] !== T_COMMENT
121+
&& isset(Tokens::$phpcsCommentTokens[$tokens[$next]['code']]) === false
122+
) {
121123
$phpcsFile->fixer->addNewlineBefore($closeBracket);
122124
} else {
123125
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
@@ -144,7 +146,9 @@ public function process(File $phpcsFile, $stackPtr)
144146
$expectedIndent = ($statementIndent + $this->indent);
145147
}//end if
146148

147-
if ($tokens[$i]['code'] === T_COMMENT) {
149+
if ($tokens[$i]['code'] === T_COMMENT
150+
|| isset(Tokens::$phpcsCommentTokens[$tokens[$i]['code']]) === true
151+
) {
148152
$prevLine = $tokens[$i]['line'];
149153
continue;
150154
}
@@ -175,24 +179,36 @@ public function process(File $phpcsFile, $stackPtr)
175179
}
176180

177181
$next = $phpcsFile->findNext(Tokens::$emptyTokens, $i, null, true);
178-
if ($next !== $closeBracket) {
182+
if ($next !== $closeBracket && $tokens[$next]['line'] === $tokens[$i]['line']) {
179183
if (isset(Tokens::$booleanOperators[$tokens[$next]['code']]) === false) {
184+
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), $openBracket, true);
185+
$fixable = true;
186+
if (isset(Tokens::$booleanOperators[$tokens[$prev]['code']]) === false
187+
&& $phpcsFile->findNext(T_WHITESPACE, ($prev + 1), $next, true) !== false
188+
) {
189+
// Condition spread over multi-lines interspersed with comments.
190+
$fixable = false;
191+
}
192+
180193
$error = 'Each line in a multi-line IF statement must begin with a boolean operator';
181-
$fix = $phpcsFile->addFixableError($error, $i, 'StartWithBoolean');
182-
if ($fix === true) {
183-
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), $openBracket, true);
184-
if (isset(Tokens::$booleanOperators[$tokens[$prev]['code']]) === true) {
185-
$phpcsFile->fixer->beginChangeset();
186-
$phpcsFile->fixer->replaceToken($prev, '');
187-
$phpcsFile->fixer->addContentBefore($next, $tokens[$prev]['content'].' ');
188-
$phpcsFile->fixer->endChangeset();
189-
} else {
190-
for ($x = ($prev + 1); $x < $next; $x++) {
191-
$phpcsFile->fixer->replaceToken($x, '');
194+
if ($fixable === false) {
195+
$phpcsFile->addError($error, $next, 'StartWithBoolean');
196+
} else {
197+
$fix = $phpcsFile->addFixableError($error, $next, 'StartWithBoolean');
198+
if ($fix === true) {
199+
if (isset(Tokens::$booleanOperators[$tokens[$prev]['code']]) === true) {
200+
$phpcsFile->fixer->beginChangeset();
201+
$phpcsFile->fixer->replaceToken($prev, '');
202+
$phpcsFile->fixer->addContentBefore($next, $tokens[$prev]['content'].' ');
203+
$phpcsFile->fixer->endChangeset();
204+
} else {
205+
for ($x = ($prev + 1); $x < $next; $x++) {
206+
$phpcsFile->fixer->replaceToken($x, '');
207+
}
192208
}
193209
}
194210
}
195-
}
211+
}//end if
196212
}//end if
197213

198214
$prevLine = $tokens[$i]['line'];

src/Standards/PEAR/Tests/ControlStructures/MultiLineConditionUnitTest.inc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,64 @@ if ($a
188188
?>
189189
<?php endif; ?>
190190
<?php endforeach; ?>
191+
<?php
192+
193+
if ($IPP->errorCode() == 401 || // phpcs:ignore Standard.Category.Sniff -- for reasons.
194+
$IPP->errorCode() == 3200) /*
195+
phpcs:ignore Standard.Category.Sniff -- for reasons.
196+
*/
197+
{
198+
return false;
199+
}
200+
201+
if ($IPP->errorCode() == 401 || // phpcs:disable Standard.Category.Sniff -- for reasons.
202+
$IPP->errorCode() == 3200) // phpcs:enable
203+
{
204+
return false;
205+
}
206+
207+
if ($IPP->errorCode() == 401
208+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
209+
|| $IPP->errorCode() == 3200
210+
) {
211+
return false;
212+
}
213+
214+
if ($IPP->errorCode() == 401 ||
215+
/*
216+
* phpcs:disable Standard.Category.Sniff -- for reasons.
217+
*/
218+
$IPP->errorCode() == 3200
219+
) {
220+
return false;
221+
}
222+
223+
if ($IPP->errorCode() == 401
224+
|| $IPP->errorCode() == 3200
225+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
226+
) {
227+
return false;
228+
}
229+
230+
if ($IPP->errorCode() == 401
231+
|| $IPP->errorCode()
232+
=== 'someverylongexpectedoutput'
233+
) {
234+
return false;
235+
}
236+
237+
if ($IPP->errorCode() == 401
238+
|| $IPP->errorCode()
239+
// A comment.
240+
=== 'someverylongexpectedoutput'
241+
) {
242+
return false;
243+
}
244+
245+
if ($IPP->errorCode() == 401
246+
|| $IPP->errorCode()
247+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
248+
=== 'someverylongexpectedoutput'
249+
) {
250+
return false;
251+
}

src/Standards/PEAR/Tests/ControlStructures/MultiLineConditionUnitTest.inc.fixed

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,63 @@ if ($a
185185
?>
186186
<?php endif; ?>
187187
<?php endforeach; ?>
188+
<?php
189+
190+
if ($IPP->errorCode() == 401 // phpcs:ignore Standard.Category.Sniff -- for reasons.
191+
|| $IPP->errorCode() == 3200 /*
192+
phpcs:ignore Standard.Category.Sniff -- for reasons.
193+
*/
194+
) {
195+
return false;
196+
}
197+
198+
if ($IPP->errorCode() == 401 // phpcs:disable Standard.Category.Sniff -- for reasons.
199+
|| $IPP->errorCode() == 3200 // phpcs:enable
200+
) {
201+
return false;
202+
}
203+
204+
if ($IPP->errorCode() == 401
205+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
206+
|| $IPP->errorCode() == 3200
207+
) {
208+
return false;
209+
}
210+
211+
if ($IPP->errorCode() == 401
212+
/*
213+
* phpcs:disable Standard.Category.Sniff -- for reasons.
214+
*/
215+
|| $IPP->errorCode() == 3200
216+
) {
217+
return false;
218+
}
219+
220+
if ($IPP->errorCode() == 401
221+
|| $IPP->errorCode() == 3200
222+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
223+
) {
224+
return false;
225+
}
226+
227+
if ($IPP->errorCode() == 401
228+
|| $IPP->errorCode() === 'someverylongexpectedoutput'
229+
) {
230+
return false;
231+
}
232+
233+
if ($IPP->errorCode() == 401
234+
|| $IPP->errorCode()
235+
// A comment.
236+
=== 'someverylongexpectedoutput'
237+
) {
238+
return false;
239+
}
240+
241+
if ($IPP->errorCode() == 401
242+
|| $IPP->errorCode()
243+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
244+
=== 'someverylongexpectedoutput'
245+
) {
246+
return false;
247+
}

src/Standards/PEAR/Tests/ControlStructures/MultiLineConditionUnitTest.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,75 @@ if (a
177177
)) {
178178
return false;
179179
}
180+
181+
182+
183+
184+
185+
186+
187+
188+
189+
190+
191+
192+
193+
if (foo == 401 || // phpcs:ignore Standard.Category.Sniff -- for reasons.
194+
bar == 3200) /*
195+
phpcs:ignore Standard.Category.Sniff -- for reasons.
196+
*/
197+
{
198+
return false;
199+
}
200+
201+
if (foo == 401 || // phpcs:disable Standard.Category.Sniff -- for reasons.
202+
bar == 3200) // phpcs:enable
203+
{
204+
return false;
205+
}
206+
207+
if (IPP.errorCode() == 401
208+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
209+
|| IPP.errorCode() == 3200
210+
) {
211+
return false;
212+
}
213+
214+
if (foo == 401 ||
215+
/*
216+
* phpcs:disable Standard.Category.Sniff -- for reasons.
217+
*/
218+
bar == 3200
219+
) {
220+
return false;
221+
}
222+
223+
if (IPP.errorCode() == 401
224+
|| IPP.errorCode() == 3200
225+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
226+
) {
227+
return false;
228+
}
229+
230+
if (foo == 401
231+
|| bar
232+
== 'someverylongexpectedoutput'
233+
) {
234+
return false;
235+
}
236+
237+
if (IPP.errorCode() == 401
238+
|| bar
239+
// A comment.
240+
== 'someverylongexpectedoutput'
241+
) {
242+
return false;
243+
}
244+
245+
if (foo == 401
246+
|| IPP.errorCode()
247+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
248+
== 'someverylongexpectedoutput'
249+
) {
250+
return false;
251+
}

src/Standards/PEAR/Tests/ControlStructures/MultiLineConditionUnitTest.js.fixed

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,74 @@ if (a
174174
) {
175175
return false;
176176
}
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+
187+
188+
189+
190+
if (foo == 401 // phpcs:ignore Standard.Category.Sniff -- for reasons.
191+
|| bar == 3200 /*
192+
phpcs:ignore Standard.Category.Sniff -- for reasons.
193+
*/
194+
) {
195+
return false;
196+
}
197+
198+
if (foo == 401 // phpcs:disable Standard.Category.Sniff -- for reasons.
199+
|| bar == 3200 // phpcs:enable
200+
) {
201+
return false;
202+
}
203+
204+
if (IPP.errorCode() == 401
205+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
206+
|| IPP.errorCode() == 3200
207+
) {
208+
return false;
209+
}
210+
211+
if (foo == 401
212+
/*
213+
* phpcs:disable Standard.Category.Sniff -- for reasons.
214+
*/
215+
|| bar == 3200
216+
) {
217+
return false;
218+
}
219+
220+
if (IPP.errorCode() == 401
221+
|| IPP.errorCode() == 3200
222+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
223+
) {
224+
return false;
225+
}
226+
227+
if (foo == 401
228+
|| bar == 'someverylongexpectedoutput'
229+
) {
230+
return false;
231+
}
232+
233+
if (IPP.errorCode() == 401
234+
|| bar
235+
// A comment.
236+
== 'someverylongexpectedoutput'
237+
) {
238+
return false;
239+
}
240+
241+
if (foo == 401
242+
|| IPP.errorCode()
243+
// phpcs:ignore Standard.Category.Sniff -- for reasons.
244+
== 'someverylongexpectedoutput'
245+
) {
246+
return false;
247+
}

src/Standards/PEAR/Tests/ControlStructures/MultiLineConditionUnitTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ public function getErrorList($testFile='MultiLineConditionUnitTest.inc')
5454
153 => 2,
5555
168 => 1,
5656
177 => 1,
57+
194 => 2,
58+
202 => 2,
59+
215 => 1,
60+
218 => 2,
61+
232 => 2,
62+
239 => 1,
63+
240 => 2,
64+
248 => 2,
5765
];
5866

5967
if ($testFile === 'MultiLineConditionUnitTest.inc') {

0 commit comments

Comments
 (0)