Skip to content

Commit 183655f

Browse files
committed
Merge branch 'feature/tokenizer-phpcs-annotations-various-minor-improvements' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 47cdda1 + d9c6e9c commit 183655f

File tree

2 files changed

+101
-8
lines changed

2 files changed

+101
-8
lines changed

src/Tokenizers/Tokenizer.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ private function createPositionMap()
228228
|| $this->tokens[$i]['code'] === T_DOC_COMMENT_TAG
229229
|| ($inTests === true && $this->tokens[$i]['code'] === T_INLINE_HTML))
230230
) {
231-
$commentText = ltrim($this->tokens[$i]['content'], ' /*');
232-
$commentText = rtrim($commentText, " */\r\n");
231+
$commentText = ltrim($this->tokens[$i]['content'], " \t/*");
232+
$commentText = rtrim($commentText, " */\t\r\n");
233233
$commentTextLower = strtolower($commentText);
234234
if (strpos($commentText, '@codingStandards') !== false) {
235235
// If this comment is the only thing on the line, it tells us
@@ -255,7 +255,7 @@ private function createPositionMap()
255255
) {
256256
$ignoring = ['all' => true];
257257
if ($ownLine === true) {
258-
$this->ignoredLines[$this->tokens[$i]['line']] = ['all' => true];
258+
$this->ignoredLines[$this->tokens[$i]['line']] = $ignoring;
259259
}
260260
} else if ($ignoring !== null
261261
&& strpos($commentText, '@codingStandardsIgnoreEnd') !== false
@@ -272,7 +272,7 @@ private function createPositionMap()
272272
) {
273273
$ignoring = ['all' => true];
274274
if ($ownLine === true) {
275-
$this->ignoredLines[$this->tokens[$i]['line']] = ['all' => true];
275+
$this->ignoredLines[$this->tokens[$i]['line']] = $ignoring;
276276
$this->ignoredLines[($this->tokens[$i]['line'] + 1)] = $ignoring;
277277
} else {
278278
$this->ignoredLines[$this->tokens[$i]['line']] = $ignoring;
@@ -320,8 +320,11 @@ private function createPositionMap()
320320
}
321321

322322
if (substr($commentTextLower, 0, 9) === 'phpcs:set') {
323-
// Ignore standards for lines that change sniff settings.
324-
$this->ignoredLines[$this->tokens[$i]['line']] = true;
323+
// Ignore standards for complete lines that change sniff settings.
324+
if ($ownLine === true) {
325+
$this->ignoredLines[$this->tokens[$i]['line']] = true;
326+
}
327+
325328
$this->tokens[$i]['code'] = T_PHPCS_SET;
326329
$this->tokens[$i]['type'] = 'T_PHPCS_SET';
327330
} else if (substr($commentTextLower, 0, 16) === 'phpcs:ignorefile') {

tests/Core/ErrorSuppressionTest.php

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ public function testSuppressError()
5151
$this->assertEquals(0, $numErrors);
5252
$this->assertCount(0, $errors);
5353

54+
// Process with multi-line inline comment suppression, tab-indented.
55+
$content = '<?php '.PHP_EOL."\t".'// For reasons'.PHP_EOL."\t".'// phpcs:disable'.PHP_EOL."\t".'$var = FALSE;'.PHP_EOL."\t".'// phpcs:enable';
56+
$file = new DummyFile($content, $ruleset, $config);
57+
$file->process();
58+
59+
$errors = $file->getErrors();
60+
$numErrors = $file->getErrorCount();
61+
$this->assertEquals(0, $numErrors);
62+
$this->assertCount(0, $errors);
63+
5464
// Process with inline @ comment suppression.
5565
$content = '<?php '.PHP_EOL.'// @phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// @phpcs:enable';
5666
$file = new DummyFile($content, $ruleset, $config);
@@ -82,7 +92,37 @@ public function testSuppressError()
8292
$this->assertCount(0, $errors);
8393

8494
// Process with block comment suppression.
85-
$content = '<?php '.PHP_EOL.'/* phpcs:disable */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/* phpcs:disable */';
95+
$content = '<?php '.PHP_EOL.'/* phpcs:disable */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/* phpcs:enable */';
96+
$file = new DummyFile($content, $ruleset, $config);
97+
$file->process();
98+
99+
$errors = $file->getErrors();
100+
$numErrors = $file->getErrorCount();
101+
$this->assertEquals(0, $numErrors);
102+
$this->assertCount(0, $errors);
103+
104+
// Process with multi-line block comment suppression.
105+
$content = '<?php '.PHP_EOL.'/*'.PHP_EOL.' phpcs:disable'.PHP_EOL.' */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/*'.PHP_EOL.' phpcs:enable'.PHP_EOL.' */';
106+
$file = new DummyFile($content, $ruleset, $config);
107+
$file->process();
108+
109+
$errors = $file->getErrors();
110+
$numErrors = $file->getErrorCount();
111+
$this->assertEquals(0, $numErrors);
112+
$this->assertCount(0, $errors);
113+
114+
// Process with multi-line block comment suppression, each line starred.
115+
$content = '<?php '.PHP_EOL.'/*'.PHP_EOL.' * phpcs:disable'.PHP_EOL.' */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/*'.PHP_EOL.' * phpcs:enable'.PHP_EOL.' */';
116+
$file = new DummyFile($content, $ruleset, $config);
117+
$file->process();
118+
119+
$errors = $file->getErrors();
120+
$numErrors = $file->getErrorCount();
121+
$this->assertEquals(0, $numErrors);
122+
$this->assertCount(0, $errors);
123+
124+
// Process with multi-line block comment suppression, tab-indented.
125+
$content = '<?php '.PHP_EOL."\t".'/*'.PHP_EOL."\t".' * phpcs:disable'.PHP_EOL."\t".' */'.PHP_EOL."\t".'$var = FALSE;'.PHP_EOL."\t".'/*'.PHP_EOL.' * phpcs:enable'.PHP_EOL.' */';
86126
$file = new DummyFile($content, $ruleset, $config);
87127
$file->process();
88128

@@ -101,6 +141,16 @@ public function testSuppressError()
101141
$this->assertEquals(0, $numErrors);
102142
$this->assertCount(0, $errors);
103143

144+
// Process with multi-line block comment suppression (deprecated syntax).
145+
$content = '<?php '.PHP_EOL.'/*'.PHP_EOL.' @codingStandardsIgnoreStart'.PHP_EOL.' */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/*'.PHP_EOL.' @codingStandardsIgnoreEnd'.PHP_EOL.' */';
146+
$file = new DummyFile($content, $ruleset, $config);
147+
$file->process();
148+
149+
$errors = $file->getErrors();
150+
$numErrors = $file->getErrorCount();
151+
$this->assertEquals(0, $numErrors);
152+
$this->assertCount(0, $errors);
153+
104154
// Process with a docblock suppression.
105155
$content = '<?php '.PHP_EOL.'/** phpcs:disable */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/** phpcs:enable */';
106156
$file = new DummyFile($content, $ruleset, $config);
@@ -319,6 +369,26 @@ public function testSuppressLine()
319369
$this->assertEquals(1, $numErrors);
320370
$this->assertCount(1, $errors);
321371

372+
// Process with suppression on line before.
373+
$content = '<?php '.PHP_EOL.'/* phpcs:ignore */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
374+
$file = new DummyFile($content, $ruleset, $config);
375+
$file->process();
376+
377+
$errors = $file->getErrors();
378+
$numErrors = $file->getErrorCount();
379+
$this->assertEquals(1, $numErrors);
380+
$this->assertCount(1, $errors);
381+
382+
// Process with @ suppression on line before.
383+
$content = '<?php '.PHP_EOL.'/* @phpcs:ignore */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
384+
$file = new DummyFile($content, $ruleset, $config);
385+
$file->process();
386+
387+
$errors = $file->getErrors();
388+
$numErrors = $file->getErrorCount();
389+
$this->assertEquals(1, $numErrors);
390+
$this->assertCount(1, $errors);
391+
322392
// Process with suppression on line before (deprecated syntax).
323393
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreLine'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
324394
$file = new DummyFile($content, $ruleset, $config);
@@ -614,6 +684,16 @@ public function testSuppressFile()
614684
$this->assertEquals(0, $numWarnings);
615685
$this->assertCount(0, $warnings);
616686

687+
// Process with a multi-line block comment suppression.
688+
$content = '<?php '.PHP_EOL.'/*'.PHP_EOL.' phpcs:ignoreFile'.PHP_EOL.' */'.PHP_EOL.'//TODO: write some code';
689+
$file = new DummyFile($content, $ruleset, $config);
690+
$file->process();
691+
692+
$warnings = $file->getWarnings();
693+
$numWarnings = $file->getWarningCount();
694+
$this->assertEquals(0, $numWarnings);
695+
$this->assertCount(0, $warnings);
696+
617697
// Process with a block comment suppression (deprecated syntax).
618698
$content = '<?php '.PHP_EOL.'/* @codingStandardsIgnoreFile */'.PHP_EOL.'//TODO: write some code';
619699
$file = new DummyFile($content, $ruleset, $config);
@@ -624,6 +704,16 @@ public function testSuppressFile()
624704
$this->assertEquals(0, $numWarnings);
625705
$this->assertCount(0, $warnings);
626706

707+
// Process with a multi-line block comment suppression (deprecated syntax).
708+
$content = '<?php '.PHP_EOL.'/*'.PHP_EOL.' @codingStandardsIgnoreFile'.PHP_EOL.' */'.PHP_EOL.'//TODO: write some code';
709+
$file = new DummyFile($content, $ruleset, $config);
710+
$file->process();
711+
712+
$warnings = $file->getWarnings();
713+
$numWarnings = $file->getWarningCount();
714+
$this->assertEquals(0, $numWarnings);
715+
$this->assertCount(0, $warnings);
716+
627717
// Process with docblock suppression.
628718
$content = '<?php '.PHP_EOL.'/** phpcs:ignoreFile */'.PHP_EOL.'//TODO: write some code';
629719
$file = new DummyFile($content, $ruleset, $config);
@@ -762,7 +852,7 @@ public function testDisableSelected()
762852
$this->assertEquals(0, $numWarnings);
763853
$this->assertCount(0, $warnings);
764854

765-
// Suppress wrong catergory using docblocks.
855+
// Suppress wrong category using docblocks.
766856
$content = '<?php '.PHP_EOL.'/**
767857
'.PHP_EOL.' * phpcs:disable Generic.Files'.PHP_EOL.' */ '.PHP_EOL.'//TODO: write some code';
768858
$file = new DummyFile($content, $ruleset, $config);

0 commit comments

Comments
 (0)