Skip to content

Commit e181258

Browse files
Merge pull request #50 from VincentLanglet/varWithoutName
✨ Add check for var name in comment
2 parents 30c0e34 + 0ddd487 commit e181258

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
9494
return;
9595
}
9696

97+
$content = explode(' ', $tokens[$string]['content']);
98+
if (in_array($tokens[$stackPtr]['content'], $content)) {
99+
$error = '@var annotations should not contain the variable name';
100+
$fix = $phpcsFile->addFixableError($error, $foundVar, 'NamedVar');
101+
102+
if (true === $fix) {
103+
$newContent = array_filter($content, function ($value) use ($tokens, $stackPtr) {
104+
return $tokens[$stackPtr]['content'] !== $value;
105+
});
106+
$phpcsFile->fixer->replaceToken($string, implode(' ', $newContent));
107+
}
108+
}
109+
97110
$this->processWhitespace($phpcsFile, $commentStart);
98111
}
99112

SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class VariableCommentUnitTest
104104
*/
105105
protected function checkVariable($var1, $var2)
106106
{
107+
/** @var string $var4 */
107108
$var4 = 'A normal variable';
108109
$var5 = PHP_CodeSniffer_Tokens::$operators;
109110
echo "Printing $var1 $var2 in a double quoted string\n";
@@ -125,4 +126,9 @@ class VariableCommentUnitTest
125126
* @var int
126127
*/
127128
private $noNewLine;
129+
130+
/**
131+
* @var string $withVarName
132+
*/
133+
private $withVarName;
128134
}

SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.inc.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class VariableCommentUnitTest
104104
*/
105105
protected function checkVariable($var1, $var2)
106106
{
107+
/** @var string $var4 */
107108
$var4 = 'A normal variable';
108109
$var5 = PHP_CodeSniffer_Tokens::$operators;
109110
echo "Printing $var1 $var2 in a double quoted string\n";
@@ -126,4 +127,9 @@ class VariableCommentUnitTest
126127
* @var int
127128
*/
128129
private $noNewLine;
130+
131+
/**
132+
* @var string
133+
*/
134+
private $withVarName;
129135
}

SymfonyCustom/Tests/Commenting/VariableCommentUnitTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public function getErrorList()
2828
64 => 1,
2929
73 => 1,
3030
84 => 1,
31-
122 => 1,
32-
124 => 1,
31+
123 => 1,
32+
125 => 1,
33+
131 => 1,
3334
];
3435
}
3536

0 commit comments

Comments
 (0)