diff --git a/Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php b/Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php index 8ec509b4..34cb7dd3 100644 --- a/Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php +++ b/Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php @@ -112,10 +112,11 @@ public function processMemberVar(File $phpcsFile, $stackPtr) if ($varParts[1]) { return; } - $error = 'Short description duplicates class property name.'; - $phpcsFile->addWarning($error, $isShortDescriptionAfterVar, 'AlreadyHaveMeaningFulNameVar'); + $error = 'Short description must be before @var tag.'; + $phpcsFile->addWarning($error, $isShortDescriptionAfterVar, 'ShortDescriptionAfterVar'); return; } + // Check if class has already have meaningful description before @var tag $isShortDescriptionPreviousVar = $phpcsFile->findPrevious( T_DOC_COMMENT_STRING, @@ -125,23 +126,37 @@ public function processMemberVar(File $phpcsFile, $stackPtr) null, false ); - if ($this->PHPDocFormattingValidator->providesMeaning( - $isShortDescriptionPreviousVar, - $commentStart, - $tokens - ) !== true) { - preg_match( - '`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i', - $tokens[($foundVar + 2)]['content'], - $varParts - ); - if ($varParts[1]) { - return; - } + + if ($isShortDescriptionPreviousVar === false) { + return; + } + + $propertyNamePosition = $phpcsFile->findNext( + T_VARIABLE, + $foundVar, + null, + false, + null, + false + ); + if ($propertyNamePosition === false) { + return; + }; + $propertyName = trim($tokens[$propertyNamePosition]['content'], '$'); + $shortDescription = strtolower($tokens[$isShortDescriptionPreviousVar]['content']); + + if ($shortDescription === strtolower($propertyName)) { $error = 'Short description duplicates class property name.'; - $phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningFulNameVar'); + $phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningfulNameVar'); return; } + + $propertyNameParts = array_filter(preg_split('/(?=[A-Z])/', $propertyName)); + + if ($shortDescription === strtolower(implode(' ', $propertyNameParts))) { + $error = 'Short description duplicates class property name.'; + $phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningfulNameVar'); + } } /** diff --git a/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc b/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc index 6dcccf65..05f9cd13 100644 --- a/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc +++ b/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc @@ -65,11 +65,18 @@ class Bar { private $variableName; /** - * Some more invalid description + * Correctly Formatted Protected Class Member * - * @var test + * @var correctlyFormattedProtectedClassMember */ - protected $test; + protected $correctlyFormattedProtectedClassMember; + + /** + * anotherCorrectlyFormattedProtectedClassMember + * + * @var anotherCorrectlyFormattedProtectedClassMember + */ + protected $anotherCorrectlyFormattedProtectedClassMember; } class correctlyFormattedClassMemberDocBlock @@ -99,4 +106,18 @@ class correctlyFormattedClassMemberDocBlock * \FooObject_TEST_C */ private $testObject; + + /** + * Fallback factory + * + * @var RulePool + */ + protected $rulePool; + + /** + * A description that includes test which is the same name as the variable is allowed + * + * @var test + */ + protected $test; } diff --git a/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.php b/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.php index 5ce3f0d2..6a49be76 100644 --- a/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.php +++ b/Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.php @@ -32,7 +32,8 @@ public function getWarningList() 49 => 1, 56 => 1, 63 => 1, - 68 => 1 + 68 => 1, + 75 => 1, ]; } }