diff --git a/Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php b/Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php index 0913be26..adeb03fa 100644 --- a/Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php +++ b/Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php @@ -40,6 +40,16 @@ class ClassPropertyPHPDocFormattingSniff extends AbstractVariableSniff */ private $PHPDocFormattingValidator; + /** + * @var array + */ + private $invalidTypes = [ + 'null', + 'false', + 'true', + 'self' + ]; + /** * Constructs an ClassPropertyPHPDocFormattingSniff. */ @@ -65,7 +75,15 @@ public function processMemberVar(File $phpcsFile, $stackPtr) ); if ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) { - $phpcsFile->addWarning('Missing PHP DocBlock for class property.', $stackPtr, 'Missing'); + // if the property has a valid type definition, we don't require a doc block + if (!$this->hasValidType($phpcsFile, $stackPtr)) { + $phpcsFile->addWarning( + 'Missing PHP DocBlock for class property without valid type.', + $stackPtr, + 'Missing' + ); + } + // no comment, so nothing further to process return; } @@ -146,6 +164,30 @@ public function processMemberVar(File $phpcsFile, $stackPtr) $this->processPropertyShortDescription($phpcsFile, $stackPtr, $varAnnotationPosition, $commentStart); } + /** + * Check if class property has a valid (not specifically invalid) type + * + * @param File $phpcsFile + * @param int $propertyPosition + * @return bool + */ + private function hasValidType(File $phpcsFile, int $propertyPosition): bool + { + // type token should be 2 before property. If not present, visibility token should be in its place + $typePosition = $phpcsFile->findPrevious( + [T_STRING], + $propertyPosition, + $propertyPosition - 2 + ); + + if (!$typePosition) { + return false; + } + + $type = $phpcsFile->getTokensAsString($typePosition, 1); + return !in_array(strtolower($type), $this->invalidTypes); + } + /** * Check if class has already have meaningful description before var tag * diff --git a/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc b/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc index aca8a73b..d88c9d04 100644 --- a/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc +++ b/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc @@ -201,6 +201,20 @@ class correctlyFormattedClassMemberDocBlock */ protected string $deprecatedWithKeyword; + private string $typedPropertyWithoutComment; + + private string $typedPropertyWithoutComment2; + + private ?string $typedPropertyWithoutComment3; + + private string|int $typedPropertyWithoutComment4; + + private Test $typedPropertyWithoutComment5; + + private Test|Test2 $typedPropertyWithoutComment6; + + private ?Test $typedPropertyWithoutComment7; + /** * @var string */