Allow @inheritdoc instead of @var on property

This commit is contained in:
Ferdinand Kuhl 2022-04-30 21:27:42 +02:00
parent d6e9f9b41f
commit 308391d6c7
5 changed files with 96 additions and 7 deletions

View file

@ -0,0 +1,48 @@
<?php
namespace DigiComp\PhpCodesniffer\DigiComp\Sniffs\Commenting;
use PHP_CodeSniffer\Files\File;
class VariableCommentSniff extends \PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\VariableCommentSniff
{
public function processMemberVar(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$ignore = [
\T_PUBLIC,
\T_PRIVATE,
\T_PROTECTED,
\T_VAR,
\T_STATIC,
\T_WHITESPACE,
\T_STRING,
\T_NS_SEPARATOR,
\T_NULLABLE,
];
$commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
$commentStart = $tokens[$commentEnd]['comment_opener'];
$foundVar = null;
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
if (
$tokens[$tag]['content'] === '@var'
|| $tokens[$tag]['content'] === '@inheritDoc'
) {
$foundVar = $tag;
}
}
// The @var tag is the only one we require.
if ($foundVar === null) {
$error = 'Missing @var or @inheritDoc tag in member variable comment';
$phpcsFile->addWarning($error, $commentEnd, 'MissingVarOrInheritDoc');
return;
}
parent::processMemberVar($phpcsFile, $stackPtr);
}
}

View file

@ -9,13 +9,14 @@
<rule ref="PSR12"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Squiz.Commenting.VariableComment">
<exclude name="Squiz.Commenting.VariableComment.VarOrder" />
<exclude name="Squiz.Commenting.VariableComment.TagNotAllowed" />
<exclude name="Squiz.Commenting.VariableComment.IncorrectVarType" />
<rule ref="DigiComp.Commenting.VariableComment">
<exclude name="DigiComp.Commenting.VariableComment.VarOrder" />
<exclude name="DigiComp.Commenting.VariableComment.TagNotAllowed" />
<exclude name="DigiComp.Commenting.VariableComment.IncorrectVarType" />
<exclude name="DigiComp.Commenting.VariableComment.MissingVar" />
</rule>
<!-- TODO: what about new PHP 7.4 code base, where property annotations may get useles -->
<rule ref="Squiz.Commenting.VariableComment.Missing">
<!-- TODO: what about new PHP 7.4 code base, where property annotations may get useless? -->
<rule ref="DigiComp.Commenting.VariableComment.Missing">
<type>warning</type>
<severity>4</severity>
</rule>

View file

@ -13,7 +13,11 @@
<property name="spacing" value="0"/>
</properties>
</rule>
<rule ref="Squiz.Commenting.VariableComment.Missing">
<rule ref="DigiComp.Commenting.VariableComment.Missing">
<type>error</type>
<severity>6</severity>
</rule>
<rule ref="DigiComp.Commenting.VariableComment.MissingVarOrInheritDoc">
<type>error</type>
<severity>6</severity>
</rule>

View file

@ -0,0 +1,14 @@
<?php
namespace DigiComp\PhpCodesniffer\DigiComp\Sniffs\Commenting;
use SlevomatCodingStandard\Sniffs\TestCase;
class VariableCommentSniffTest extends TestCase
{
public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/comments.php');
self::assertCount(1, $report->getWarnings()[20][6]);
}
}

View file

@ -0,0 +1,22 @@
<?php
class A {
/**
* @var string
*/
protected $var;
}
class B extends A {
/**
* @inheritDoc
*/
protected $var;
}
class C extends A {
/**
* No inherit
*/
protected $var;
}