Skip to content

Fixed wrongly returning error for valid descriptions #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function getWarningList()
49 => 1,
56 => 1,
63 => 1,
68 => 1
68 => 1,
75 => 1,
];
}
}