Skip to content

Tokenizer: remove support for the deprecated @codingStandard annotation syntax #975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,38 +361,7 @@ public function process()
) {
$commentText = ltrim($this->tokens[$stackPtr]['content'], " \t/*#");
$commentTextLower = strtolower($commentText);
if (strpos($commentText, '@codingStandards') !== false) {
if (strpos($commentText, '@codingStandardsIgnoreFile') !== false) {
// Ignoring the whole file, just a little late.
$this->errors = [];
$this->warnings = [];
$this->errorCount = 0;
$this->warningCount = 0;
$this->fixableCount = 0;
return;
} else if (strpos($commentText, '@codingStandardsChangeSetting') !== false) {
$start = strpos($commentText, '@codingStandardsChangeSetting');
$comment = substr($commentText, ($start + 30));
$parts = explode(' ', $comment);
if (count($parts) >= 2) {
$sniffParts = explode('.', $parts[0]);
if (count($sniffParts) >= 3) {
// If the sniff code is not known to us, it has not been registered in this run.
// But don't throw an error as it could be there for a different standard to use.
if (isset($this->ruleset->sniffCodes[$parts[0]]) === true) {
$listenerCode = array_shift($parts);
$propertyCode = array_shift($parts);
$settings = [
'value' => rtrim(implode(' ', $parts), " */\r\n"),
'scope' => 'sniff',
];
$listenerClass = $this->ruleset->sniffCodes[$listenerCode];
$this->ruleset->setSniffProperty($listenerClass, $propertyCode, $settings);
}
}
}
}//end if
} else if (substr($commentTextLower, 0, 16) === 'phpcs:ignorefile'
if (substr($commentTextLower, 0, 16) === 'phpcs:ignorefile'
|| substr($commentTextLower, 0, 17) === '@phpcs:ignorefile'
) {
// Ignoring the whole file, just a little late.
Expand Down
4 changes: 1 addition & 3 deletions src/Files/LocalFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public function __construct($path, Ruleset $ruleset, Config $config)
$firstContent .= fgets($handle);
fclose($handle);

if (strpos($firstContent, '@codingStandardsIgnoreFile') !== false
|| stripos($firstContent, 'phpcs:ignorefile') !== false
) {
if (stripos($firstContent, 'phpcs:ignorefile') !== false) {
// We are ignoring the whole file.
$this->ignored = true;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ function test() {
$foo = function() { echo 'foo'; };
}

/* @codingStandardsIgnoreStart */
/* phpcs:disable */
class MyClass
{
/* @codingStandardsIgnoreEnd */
/* phpcs:enable */
public function __construct() {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ function test() {
$foo = function() { echo 'foo'; };
}

/* @codingStandardsIgnoreStart */
/* phpcs:disable */
class MyClass
{
/* @codingStandardsIgnoreEnd */
/* phpcs:enable */
public function __construct() {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ function xmlParser() {}

echo preg_replace_callback('~-([a-z])~', function ($match) { return strtoupper($match[1]); }, 'hello-world');

/* @codingStandardsIgnoreStart */
/* phpcs:disable */
class MyClass
{
/* @codingStandardsIgnoreEnd */
/* phpcs:enable */
public function __construct() {}
}
?>
51 changes: 1 addition & 50 deletions src/Tokenizers/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,56 +259,7 @@ private function createPositionMap()
$commentText = ltrim($this->tokens[$i]['content'], " \t/*#");
$commentText = rtrim($commentText, " */\t\r\n");
$commentTextLower = strtolower($commentText);
if (strpos($commentText, '@codingStandards') !== false) {
// 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.
$ownLine = false;
if ($i > 0) {
for ($prev = ($i - 1); $prev >= 0; $prev--) {
if ($this->tokens[$prev]['code'] === T_WHITESPACE) {
continue;
}

break;
}

if ($this->tokens[$prev]['line'] !== $this->tokens[$i]['line']) {
$ownLine = true;
}
}

if ($ignoring === null
&& strpos($commentText, '@codingStandardsIgnoreStart') !== false
) {
$ignoring = ['.all' => true];
if ($ownLine === true) {
$this->ignoredLines[$this->tokens[$i]['line']] = $ignoring;
}
} else if ($ignoring !== null
&& strpos($commentText, '@codingStandardsIgnoreEnd') !== false
) {
if ($ownLine === true) {
$this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true];
} else {
$this->ignoredLines[$this->tokens[$i]['line']] = $ignoring;
}

$ignoring = null;
} else if ($ignoring === null
&& strpos($commentText, '@codingStandardsIgnoreLine') !== false
) {
$ignoring = ['.all' => true];
if ($ownLine === true) {
$this->ignoredLines[$this->tokens[$i]['line']] = $ignoring;
$this->ignoredLines[($this->tokens[$i]['line'] + 1)] = $ignoring;
} else {
$this->ignoredLines[$this->tokens[$i]['line']] = $ignoring;
}

$ignoring = null;
}//end if
} else if (substr($commentTextLower, 0, 6) === 'phpcs:'
if (substr($commentTextLower, 0, 6) === 'phpcs:'
|| substr($commentTextLower, 0, 7) === '@phpcs:'
) {
// If the @phpcs: syntax is being used, strip the @ to make
Expand Down
85 changes: 0 additions & 85 deletions tests/Core/ErrorSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,6 @@ public static function dataSuppressError()
'before' => '/** phpcs:disable */'.PHP_EOL,
'after' => '/** phpcs:enable */',
],

// Deprecated syntax.
'old style: slash comment' => [
'before' => '// @codingStandardsIgnoreStart'.PHP_EOL,
'after' => '// @codingStandardsIgnoreEnd',
],
'old style: star comment' => [
'before' => '/* @codingStandardsIgnoreStart */'.PHP_EOL,
'after' => '/* @codingStandardsIgnoreEnd */',
],
'old style: multi-line star comment' => [
'before' => '/*'.PHP_EOL.' @codingStandardsIgnoreStart'.PHP_EOL.' */'.PHP_EOL,
'after' => '/*'.PHP_EOL.' @codingStandardsIgnoreEnd'.PHP_EOL.' */',
],
'old style: single line docblock comment' => [
'before' => '/** @codingStandardsIgnoreStart */'.PHP_EOL,
'after' => '/** @codingStandardsIgnoreEnd */',
],
];

}//end dataSuppressError()
Expand Down Expand Up @@ -233,16 +215,6 @@ public static function dataSuppressSomeErrors()
'before' => '/** phpcs:disable */',
'between' => '/** phpcs:enable */',
],

// Deprecated syntax.
'old style: slash comment' => [
'before' => '// @codingStandardsIgnoreStart',
'between' => '// @codingStandardsIgnoreEnd',
],
'old style: single line docblock comment' => [
'before' => '/** @codingStandardsIgnoreStart */',
'between' => '/** @codingStandardsIgnoreEnd */',
],
];

}//end dataSuppressSomeErrors()
Expand Down Expand Up @@ -316,16 +288,6 @@ public static function dataSuppressWarning()
'before' => '/** phpcs:disable */',
'after' => '/** phpcs:enable */',
],

// Deprecated syntax.
'old style: slash comment' => [
'before' => '// @codingStandardsIgnoreStart',
'after' => '// @codingStandardsIgnoreEnd',
],
'old style: single line docblock comment' => [
'before' => '/** @codingStandardsIgnoreStart */',
'after' => '/** @codingStandardsIgnoreEnd */',
],
];

}//end dataSuppressWarning()
Expand Down Expand Up @@ -424,15 +386,6 @@ public static function dataSuppressLine()
'before' => '',
'after' => ' # @phpcs:ignore',
],

// Deprecated syntax.
'old style: line before, slash comment' => [
'before' => '// @codingStandardsIgnoreLine',
],
'old style: end of line, slash comment' => [
'before' => '',
'after' => ' // @codingStandardsIgnoreLine',
],
];

}//end dataSuppressLine()
Expand Down Expand Up @@ -554,10 +507,6 @@ public static function dataNestedSuppressLine()
'before' => '# phpcs:disable',
'after' => '# phpcs:enable',
],
'old style: slash comment, no single line suppression' => [
'before' => '// @codingStandardsIgnoreStart',
'after' => '// @codingStandardsIgnoreEnd',
],

// Process with line suppression nested within disable/enable suppression.
'disable/enable: slash comment, next line nested single line suppression' => [
Expand All @@ -572,10 +521,6 @@ public static function dataNestedSuppressLine()
'before' => '# @phpcs:disable'.PHP_EOL.'# @phpcs:ignore',
'after' => '# @phpcs:enable',
],
'old style: slash comment, next line nested single line suppression' => [
'before' => '// @codingStandardsIgnoreStart'.PHP_EOL.'// @codingStandardsIgnoreLine',
'after' => '// @codingStandardsIgnoreEnd',
],
];

}//end dataNestedSuppressLine()
Expand Down Expand Up @@ -661,16 +606,6 @@ public static function dataSuppressScope()
'before' => '/** @phpcs:disable */',
'after' => '/** @phpcs:enable */',
],

// Deprecated syntax.
'old style: start/end, slash comment' => [
'before' => '//@codingStandardsIgnoreStart',
'after' => '//@codingStandardsIgnoreEnd',
],
'old style: start/end, single line docblock comment' => [
'before' => '/** @codingStandardsIgnoreStart */',
'after' => '/** @codingStandardsIgnoreEnd */',
],
];

}//end dataSuppressScope()
Expand Down Expand Up @@ -765,26 +700,6 @@ public static function dataSuppressFile()
'before' => '',
'after' => '// phpcs:ignoreFile',
],

// Deprecated syntax.
'old style: start of file, slash comment' => [
'before' => '// @codingStandardsIgnoreFile',
],
'old style: start of file, single-line star comment' => [
'before' => '/* @codingStandardsIgnoreFile */',
],
'old style: start of file, multi-line star comment' => [
'before' => '/*'.PHP_EOL.' @codingStandardsIgnoreFile'.PHP_EOL.' */',
],
'old style: start of file, single-line docblock comment' => [
'before' => '/** @codingStandardsIgnoreFile */',
],

// Deprecated syntax, late comment.
'old style: late comment, slash comment' => [
'before' => '',
'after' => '// @codingStandardsIgnoreFile',
],
];

}//end dataSuppressFile()
Expand Down