From 79a7c573adcf46ac3ba89a9b04dcadc9f413ee2c Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 5 May 2019 23:40:29 +0200 Subject: [PATCH] :sparkles: Add check for var name in comment --- .../Sniffs/Commenting/VariableCommentSniff.php | 13 +++++++++++++ .../Tests/Commenting/VariableCommentUnitTest.inc | 6 ++++++ .../Commenting/VariableCommentUnitTest.inc.fixed | 6 ++++++ .../Tests/Commenting/VariableCommentUnitTest.php | 5 +++-- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php b/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php index c59c352..89e53a4 100644 --- a/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php +++ b/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php @@ -94,6 +94,19 @@ public function processMemberVar(File $phpcsFile, $stackPtr) return; } + $content = explode(' ', $tokens[$string]['content']); + if (in_array($tokens[$stackPtr]['content'], $content)) { + $error = '@var annotations should not contain the variable name'; + $fix = $phpcsFile->addFixableError($error, $foundVar, 'NamedVar'); + + if (true === $fix) { + $newContent = array_filter($content, function ($value) use ($tokens, $stackPtr) { + return $tokens[$stackPtr]['content'] !== $value; + }); + $phpcsFile->fixer->replaceToken($string, implode(' ', $newContent)); + } + } + $this->processWhitespace($phpcsFile, $commentStart); } diff --git a/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc b/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc index 5a4afe8..2e7a7c6 100644 --- a/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc +++ b/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc @@ -104,6 +104,7 @@ class VariableCommentUnitTest */ protected function checkVariable($var1, $var2) { + /** @var string $var4 */ $var4 = 'A normal variable'; $var5 = PHP_CodeSniffer_Tokens::$operators; echo "Printing $var1 $var2 in a double quoted string\n"; @@ -125,4 +126,9 @@ class VariableCommentUnitTest * @var int */ private $noNewLine; + + /** + * @var string $withVarName + */ + private $withVarName; } diff --git a/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc.fixed b/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc.fixed index 1b0fb38..cea13a4 100644 --- a/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc.fixed +++ b/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc.fixed @@ -104,6 +104,7 @@ class VariableCommentUnitTest */ protected function checkVariable($var1, $var2) { + /** @var string $var4 */ $var4 = 'A normal variable'; $var5 = PHP_CodeSniffer_Tokens::$operators; echo "Printing $var1 $var2 in a double quoted string\n"; @@ -126,4 +127,9 @@ class VariableCommentUnitTest * @var int */ private $noNewLine; + + /** + * @var string + */ + private $withVarName; } diff --git a/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.php b/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.php index 8f8890c..827ef68 100644 --- a/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.php +++ b/SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.php @@ -28,8 +28,9 @@ public function getErrorList() 64 => 1, 73 => 1, 84 => 1, - 122 => 1, - 124 => 1, + 123 => 1, + 125 => 1, + 131 => 1, ]; }