Skip to content

Commit f93cabd

Browse files
authored
Merge pull request #1278 from PHPCSStandards/feature/1277-bring-back-whitespace-tolerance-ignore-comments
Bring back whitespace tolerance in selective `phpcs:ignore` comments
2 parents ac65a4c + d3cdf9c commit f93cabd

File tree

2 files changed

+111
-20
lines changed

2 files changed

+111
-20
lines changed

src/Tokenizers/Tokenizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ private function createPositionMap()
447447
$lineIgnoring = IgnoreList::getNewInstanceFrom($ignoring);
448448

449449
foreach ($parts as $sniffCode) {
450-
$ignoreRules[trim($sniffCode)] = true;
450+
$sniffCode = trim($sniffCode);
451+
$ignoreRules[$sniffCode] = true;
451452
$lineIgnoring->set($sniffCode, true);
452453
}
453454
}

tests/Core/ErrorSuppressionTest.php

Lines changed: 109 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -746,59 +746,65 @@ public static function dataDisableSelected()
746746
{
747747
return [
748748
// Single sniff.
749-
'disable: single sniff' => [
749+
'disable: single sniff' => [
750750
'before' => '// phpcs:disable Generic.Commenting.Todo',
751751
'expectedErrors' => 1,
752752
],
753-
'disable: single sniff with reason' => [
753+
'disable: single sniff with reason' => [
754754
'before' => '# phpcs:disable Generic.Commenting.Todo -- for reasons',
755755
'expectedErrors' => 1,
756756
],
757-
'disable: single sniff, docblock' => [
757+
'disable: single sniff, docblock' => [
758758
'before' => "/**\n * phpcs:disable Generic.Commenting.Todo\n */ ",
759759
'expectedErrors' => 1,
760760
],
761-
'disable: single sniff, docblock, with @' => [
761+
'disable: single sniff, docblock, with @' => [
762762
'before' => "/**\n * @phpcs:disable Generic.Commenting.Todo\n */ ",
763763
'expectedErrors' => 1,
764764
],
765765

766766
// Multiple sniffs.
767-
'disable: multiple sniffs in one comment' => [
767+
'disable: multiple sniffs in one comment' => [
768768
'before' => '// phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant',
769769
],
770-
'disable: multiple sniff in multiple comments' => [
770+
'disable: multiple sniffs in one comment with superfluous space after comma' => [
771+
'before' => '// phpcs:disable Generic.Commenting.Todo, Generic.PHP.LowerCaseConstant',
772+
],
773+
'disable: multiple sniff in multiple comments' => [
771774
'before' => "// phpcs:disable Generic.Commenting.Todo\n// phpcs:disable Generic.PHP.LowerCaseConstant",
772775
],
773776

774777
// Selectiveness variations.
775-
'disable: complete category' => [
778+
'disable: complete category' => [
776779
'before' => '// phpcs:disable Generic.Commenting',
777780
'expectedErrors' => 1,
778781
],
779-
'disable: whole standard' => [
782+
'disable: whole standard' => [
780783
'before' => '// phpcs:disable Generic',
781784
],
782-
'disable: single errorcode' => [
785+
'disable: single errorcode' => [
783786
'before' => '# @phpcs:disable Generic.Commenting.Todo.TaskFound',
784787
'expectedErrors' => 1,
785788
],
786-
'disable: single errorcode and a category' => [
789+
'disable: single errorcode and a category' => [
787790
'before' => '// phpcs:disable Generic.PHP.LowerCaseConstant.Found,Generic.Commenting',
788791
],
792+
'disable: single errorcode and a category with superfluous space after comma' => [
793+
'before' => '// phpcs:disable Generic.PHP.LowerCaseConstant.Found, Generic.Commenting',
794+
],
789795

790796
// Wrong category/sniff/code.
791-
'disable: wrong error code and category' => [
797+
'disable: wrong error code and category' => [
792798
'before' => "/**\n * phpcs:disable Generic.PHP.LowerCaseConstant.Upper,Generic.Comments\n */ ",
793799
'expectedErrors' => 1,
794800
'expectedWarnings' => 1,
795801
],
796-
'disable: wrong category, docblock' => [
802+
'disable: wrong category, docblock' => [
797803
'before' => "/**\n * phpcs:disable Generic.Files\n */ ",
798804
'expectedErrors' => 1,
799805
'expectedWarnings' => 1,
800806
],
801-
'disable: wrong category, docblock, with @' => [
807+
'disable: wrong category, docblock, with @' => [
802808
'before' => "/**\n * @phpcs:disable Generic.Files\n */ ",
803809
'expectedErrors' => 1,
804810
'expectedWarnings' => 1,
@@ -876,6 +882,17 @@ public static function dataEnableSelected()
876882
'expectedErrors' => 1,
877883
'expectedWarnings' => 1,
878884
],
885+
'disable/enable: multiple sniffs with superfluous space after comma' => [
886+
'code' => '
887+
// phpcs:disable Generic.Commenting.Todo, Generic.PHP.LowerCaseConstant
888+
$var = FALSE;
889+
//TODO: write some code
890+
// phpcs:enable Generic.Commenting.Todo, Generic.PHP.LowerCaseConstant
891+
//TODO: write some code
892+
$var = FALSE;',
893+
'expectedErrors' => 1,
894+
'expectedWarnings' => 1,
895+
],
879896
'disable: multiple sniffs; enable: one' => [
880897
'code' => '
881898
# phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant
@@ -1028,6 +1045,54 @@ public static function dataEnableSelected()
10281045
'expectedErrors' => 0,
10291046
'expectedWarnings' => 0,
10301047
],
1048+
'disable: two sniffs in one go; enable: both sniffs; ignore: one of those sniffs' => [
1049+
'code' => '
1050+
// phpcs:disable Generic.PHP.LowerCaseConstant,Generic.Commenting.Todo
1051+
//TODO: write some code
1052+
$var = TRUE;
1053+
// phpcs:enable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant
1054+
1055+
$var = FALSE; // phpcs:ignore Generic.PHP.LowerCaseConstant
1056+
',
1057+
'expectedErrors' => 0,
1058+
'expectedWarnings' => 0,
1059+
],
1060+
'disable: two sniffs in one go; enable: one sniff; ignore: enabled sniff' => [
1061+
'code' => '
1062+
// phpcs:disable Generic.PHP.LowerCaseConstant, Generic.Commenting.Todo
1063+
//TODO: write some code
1064+
$var = TRUE;
1065+
// phpcs:enable Generic.PHP.LowerCaseConstant
1066+
1067+
$var = FALSE; // phpcs:ignore Generic.PHP.LowerCaseConstant
1068+
',
1069+
'expectedErrors' => 0,
1070+
'expectedWarnings' => 0,
1071+
],
1072+
'disable: two sniffs in one go; enable: one sniff; ignore: category' => [
1073+
'code' => '
1074+
// phpcs:disable Generic.PHP.LowerCaseConstant,Generic.Commenting.Todo
1075+
//TODO: write some code
1076+
$var = TRUE;
1077+
// phpcs:enable Generic.PHP.LowerCaseConstant
1078+
1079+
$var = FALSE; // phpcs:ignore Generic.PHP
1080+
',
1081+
'expectedErrors' => 0,
1082+
'expectedWarnings' => 0,
1083+
],
1084+
'disable: two sniffs in one go; enable: category; ignore: sniff in category' => [
1085+
'code' => '
1086+
// phpcs:disable Generic.PHP.LowerCaseConstant, Generic.Commenting.Todo
1087+
//TODO: write some code
1088+
$var = TRUE;
1089+
// phpcs:enable Generic.PHP
1090+
1091+
$var = FALSE; // phpcs:ignore Generic.PHP.LowerCaseConstant
1092+
',
1093+
'expectedErrors' => 0,
1094+
'expectedWarnings' => 0,
1095+
],
10311096
'disable: standard; enable: category in standard; disable: sniff in category' => [
10321097
'code' => '
10331098
// phpcs:disable Generic
@@ -1106,34 +1171,49 @@ public function testIgnoreSelected($before, $expectedErrors, $expectedWarnings)
11061171
public static function dataIgnoreSelected()
11071172
{
11081173
return [
1109-
'no suppression' => [
1174+
'no suppression' => [
11101175
'before' => '',
11111176
'expectedErrors' => 2,
11121177
'expectedWarnings' => 2,
11131178
],
11141179

11151180
// With suppression.
1116-
'ignore: single sniff' => [
1181+
'ignore: single sniff' => [
11171182
'before' => '// phpcs:ignore Generic.Commenting.Todo',
11181183
'expectedErrors' => 2,
11191184
'expectedWarnings' => 1,
11201185
],
1121-
'ignore: multiple sniffs' => [
1186+
'ignore: multiple sniffs' => [
11221187
'before' => '// phpcs:ignore Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant',
11231188
'expectedErrors' => 1,
11241189
'expectedWarnings' => 1,
11251190
],
1126-
'disable: single sniff; ignore: single sniff' => [
1191+
'ignore: multiple sniffs with superfluous space after comma' => [
1192+
'before' => '// phpcs:ignore Generic.Commenting.Todo , Generic.PHP.LowerCaseConstant',
1193+
'expectedErrors' => 1,
1194+
'expectedWarnings' => 1,
1195+
],
1196+
'ignore: one sniff, one category with superfluous space after comma' => [
1197+
'before' => '// phpcs:ignore Generic.Commenting.Todo, Generic.PHP',
1198+
'expectedErrors' => 1,
1199+
'expectedWarnings' => 1,
1200+
],
1201+
'ignore: one category, one error code with superfluous space after comma' => [
1202+
'before' => '// phpcs:ignore Generic.Commenting, Generic.PHP.LowerCaseConstant.Found',
1203+
'expectedErrors' => 1,
1204+
'expectedWarnings' => 1,
1205+
],
1206+
'disable: single sniff; ignore: single sniff' => [
11271207
'before' => "// phpcs:disable Generic.Commenting.Todo\n// phpcs:ignore Generic.PHP.LowerCaseConstant",
11281208
'expectedErrors' => 1,
11291209
'expectedWarnings' => 0,
11301210
],
1131-
'ignore: category of sniffs' => [
1211+
'ignore: category of sniffs' => [
11321212
'before' => '# phpcs:ignore Generic.Commenting',
11331213
'expectedErrors' => 2,
11341214
'expectedWarnings' => 1,
11351215
],
1136-
'ignore: whole standard' => [
1216+
'ignore: whole standard' => [
11371217
'before' => '// phpcs:ignore Generic',
11381218
'expectedErrors' => 1,
11391219
'expectedWarnings' => 1,
@@ -1228,6 +1308,16 @@ public static function dataCommenting()
12281308
'expectedErrors' => 2,
12291309
'expectedWarnings' => 1,
12301310
],
1311+
'ignore: multi sniff, line above and trailing - with comment and superfluous whitespace' => [
1312+
'code' => '
1313+
// phpcs:ignore Generic.Commenting.Todo , Generic.PHP.LowerCaseConstant.Found -- Because reasons
1314+
$var = FALSE; //TODO: write some code
1315+
$var = FALSE; // phpcs:ignore Generic.Commenting.Todo , Generic.PHP.LowerCaseConstant.Found --Because reasons
1316+
//TODO: write some code
1317+
$var = FALSE;',
1318+
'expectedErrors' => 1,
1319+
'expectedWarnings' => 1,
1320+
],
12311321
'enable before disable, sniff not in standard' => [
12321322
'code' => '
12331323
// phpcs:enable Generic.PHP.NoSilencedErrors -- Because reasons

0 commit comments

Comments
 (0)