Skip to content

Commit

Permalink
Added support for additional comments at the end of phpcs: comments (…
Browse files Browse the repository at this point in the history
…ref #604)
  • Loading branch information
gsherwood committed Oct 31, 2017
1 parent 4534876 commit 54627e0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/Tokenizers/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
61 changes: 61 additions & 0 deletions tests/Core/ErrorSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.