From 22ce2c556a8fa649193b6098cf22e9cb929ce196 Mon Sep 17 00:00:00 2001 From: Michael Moll Date: Sun, 8 Nov 2020 23:50:57 +0100 Subject: [PATCH] Use Slevomat sniffs instead of TypeHinting --- .../Sniffs/Commenting/TypeHintingSniff.php | 138 ------------------ .../Tests/Commenting/TypeHintingUnitTest.inc | 30 ---- .../Commenting/TypeHintingUnitTest.inc.fixed | 30 ---- .../Tests/Commenting/TypeHintingUnitTest.php | 65 --------- Symfony/ruleset.xml | 2 + 5 files changed, 2 insertions(+), 263 deletions(-) delete mode 100644 Symfony/Sniffs/Commenting/TypeHintingSniff.php delete mode 100644 Symfony/Tests/Commenting/TypeHintingUnitTest.inc delete mode 100644 Symfony/Tests/Commenting/TypeHintingUnitTest.inc.fixed delete mode 100644 Symfony/Tests/Commenting/TypeHintingUnitTest.php diff --git a/Symfony/Sniffs/Commenting/TypeHintingSniff.php b/Symfony/Sniffs/Commenting/TypeHintingSniff.php deleted file mode 100644 index 28709d9..0000000 --- a/Symfony/Sniffs/Commenting/TypeHintingSniff.php +++ /dev/null @@ -1,138 +0,0 @@ - - * @license http://spdx.org/licenses/MIT MIT License - * @link https://github.com/djoos/Symfony-coding-standard - */ - -namespace Symfony\Sniffs\Commenting; - -use PHP_CodeSniffer\Files\File; -use PHP_CodeSniffer\Sniffs\Sniff; - -/** - * Checks for proper type hinting. - * - * @category PHP - * @package Symfony-coding-standard - * @author wicliff wolda - * @license http://spdx.org/licenses/MIT MIT License - * @link http://pear.php.net/package/PHP_CodeSniffer - */ -class TypeHintingSniff implements Sniff -{ - /** - * Blacklisted types - * - * @var array - */ - private static $_blacklist = [ - 'boolean' => 'bool', - 'integer' => 'int', - 'double' => 'float', - 'real' => 'float', - ]; - - /** - * Cast tokens - * - * @var array - */ - private static $_casts = [ - T_BOOL_CAST, - T_INT_CAST, - T_DOUBLE_CAST, - ]; - - /** - * Registers the tokens that this sniff wants to listen for. - * - * @return array - */ - public function register() - { - return [ - T_DOC_COMMENT_TAG, - T_BOOL_CAST, - T_INT_CAST, - T_DOUBLE_CAST, - ]; - } - - /** - * Called when one of the token types that this sniff is listening for - * is found. - * - * @param File $phpcsFile The file where the token was found. - * @param int $stackPtr The position of the current token - * in the stack passed in $tokens. - * - * @return void|int Optionally returns a stack pointer. The sniff will not be - * called again on the current file until the returned stack - * pointer is reached. Return (count($tokens) + 1) to skip - * the rest of the file. - */ - public function process(File $phpcsFile, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - $tag = $tokens[$stackPtr]; - - $fixPtr = $stackPtr; - - if ('@var' === $tag['content']) { - $type = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $stackPtr + 1); - $fixPtr = $type; - $hint = strtolower( - preg_replace( - '/([^\s]+)[\s]+.*/', - '$1', - $tokens[$type]['content'] - ) - ); - } elseif (in_array($tag['code'], self::$_casts, true)) { - $hint = strtolower( - preg_replace( - '/\(([^\s]+)\)/', - '$1', - $tag['content'] - ) - ); - } - - if (isset($hint, self::$_blacklist[$hint])) { - $error = sprintf( - 'For type-hinting in PHPDocs and casting, use %s instead of %s', - self::$_blacklist[$hint], - $hint - ); - - $fixable = $phpcsFile->addFixableError($error, $stackPtr, 'Invalid'); - - if (true === $fixable) { - - if ($fixPtr === $stackPtr) { - $fixedContent = self::$_blacklist[$hint]; - $fixedContent = "({$fixedContent})"; - } else { - $fixedContent = $tokens[$fixPtr]['content']; - $fixedContent = preg_replace( - "/^$hint/", - self::$_blacklist[$hint], - $fixedContent - ); - } - - $phpcsFile->fixer->beginChangeset(); - $phpcsFile->fixer->replaceToken($fixPtr, $fixedContent); - $phpcsFile->fixer->endChangeset(); - } - } - } -} diff --git a/Symfony/Tests/Commenting/TypeHintingUnitTest.inc b/Symfony/Tests/Commenting/TypeHintingUnitTest.inc deleted file mode 100644 index 5bcbca1..0000000 --- a/Symfony/Tests/Commenting/TypeHintingUnitTest.inc +++ /dev/null @@ -1,30 +0,0 @@ - - * @license http://spdx.org/licenses/MIT MIT License - * @link https://github.com/djoos/Symfony2-coding-standard - */ - -namespace Symfony\Tests\Commenting; - -use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; - -/** - * Unit test class for the TypeHintingUnitTest sniff. - * - * A sniff unit test checks a .inc file for expected violations of a single - * coding standard. Expected errors and warnings are stored in this class. - * - * PHP version 5 - * - * @category PHP - * @package Symfony2-coding-standard - * @author wicliff - * @license http://spdx.org/licenses/MIT MIT License - * @link https://github.com/djoos/Symfony2-coding-standard - */ -class TypeHintingUnitTest extends AbstractSniffUnitTest -{ - /** - * Returns the lines where errors should occur. - * - * The key of the array should represent the line number and the value - * should represent the number of errors that should occur on that line. - * - * @return array - */ - public function getErrorList() - { - return array( - 6 => 1, - 11 => 1, - 22 => 1, - 23 => 1, - ); - } - - /** - * Returns the lines where warnings should occur. - * - * The key of the array should represent the line number and the value - * should represent the number of errors that should occur on that line. - * - * @return array - */ - public function getWarningList() - { - return array(); - } -} diff --git a/Symfony/ruleset.xml b/Symfony/ruleset.xml index ef93bc2..8833208 100755 --- a/Symfony/ruleset.xml +++ b/Symfony/ruleset.xml @@ -113,6 +113,8 @@ There should always be a description, followed by a blank line, before the tags of a class comment. + +