Permalink
Browse files

Added support for additional comments at the end of phpcs: comments (…

…ref #604)
  • Loading branch information...
gsherwood committed Oct 31, 2017
1 parent 4534876 commit 54627e00999f290c29b96c466be6e67878d03f68
Showing with 71 additions and 0 deletions.
  1. +3 −0 package.xml
  2. +7 −0 src/Tokenizers/Tokenizer.php
  3. +61 −0 tests/Core/ErrorSuppressionTest.php
View
@@ -34,6 +34,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
--- @codingStandardsIgnoreEnd becomes phpcs:enable
--- @codingStandardsIgnoreLine becomes phpcs:ignore
--- @codingStandardsChangeSetting becomes phpcs:set
-- The new syntax allows for additional developer comments to be added after a -- seperator
--- This is useful for describing why a code block is being ignored, or why a setting is being changed
--- E.g., phpcs:disable -- This code block must be left as-is.
-- Comments using the new syntax are assigned new comment token types to allow them to be detected:
--- phpcs:ignoreFile has the token T_PHPCS_IGNORE_FILE
--- phpcs:disable has the token T_PHPCS_DISABLE
@@ -277,6 +277,13 @@ private function createPositionMap()
$ignoring = null;
}//end if
} else if (substr($commentTextLower, 0, 6) === 'phpcs:') {
// If there is a comment on the end, strip it off.
$commentStart = strpos($commentTextLower, ' --');
if ($commentStart !== false) {
$commentText = substr($commentText, 0, $commentStart);
$commentTextLower = strtolower($commentText);
}
// If this comment is the only thing on the line, it tells us
// to ignore the following line. If the line contains other content
// then we are just ignoring this one single line.
@@ -827,4 +827,65 @@ public function testIgnoreSelected()
}//end testIgnoreSelected()
/**
* Test ignoring specific sniffs.
*
* @return void
*/
public function testCommenting()
{
$config = new Config();
$config->standards = array('Generic');
$config->sniffs = array(
'Generic.PHP.LowerCaseConstant',
'Generic.Commenting.Todo',
);
$ruleset = new Ruleset($config);
// Suppress a single sniff.
$content = '<?php '.PHP_EOL.'// phpcs:ignore Generic.Commenting.Todo -- Because reasons'.PHP_EOL.'$var = FALSE; //TODO: write some code'.PHP_EOL.'$var = FALSE; //TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->process();
$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$warnings = $file->getWarnings();
$numWarnings = $file->getWarningCount();
$this->assertEquals(2, $numErrors);
$this->assertEquals(2, count($errors));
$this->assertEquals(1, $numWarnings);
$this->assertEquals(1, count($warnings));
// Suppress a single sniff and re-enable.
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting.Todo --Because reasons'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic.Commenting.Todo -- Because reasons'.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->process();
$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$warnings = $file->getWarnings();
$numWarnings = $file->getWarningCount();
$this->assertEquals(1, $numErrors);
$this->assertEquals(1, count($errors));
$this->assertEquals(1, $numWarnings);
$this->assertEquals(1, count($warnings));
// Suppress a single sniff.
$content = '<?php '.PHP_EOL.'/*'.PHP_EOL.' Disable some checks'.PHP_EOL.' phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'*/'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->process();
$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$warnings = $file->getWarnings();
$numWarnings = $file->getWarningCount();
$this->assertEquals(1, $numErrors);
$this->assertEquals(1, count($errors));
$this->assertEquals(0, $numWarnings);
$this->assertEquals(0, count($warnings));
}//end testCommenting()
}//end class

0 comments on commit 54627e0

Please sign in to comment.