Skip to content

Commit a25482e

Browse files
authored
Merge pull request #63 from magento-commerce/imported-svera-magento-coding-standard-270
[Imported] Fixed wrongly returning error for valid descriptions
2 parents b00d7cf + 0fb55fc commit a25482e

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php

+31-16
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
112112
if ($varParts[1]) {
113113
return;
114114
}
115-
$error = 'Short description duplicates class property name.';
116-
$phpcsFile->addWarning($error, $isShortDescriptionAfterVar, 'AlreadyHaveMeaningFulNameVar');
115+
$error = 'Short description must be before @var tag.';
116+
$phpcsFile->addWarning($error, $isShortDescriptionAfterVar, 'ShortDescriptionAfterVar');
117117
return;
118118
}
119+
119120
// Check if class has already have meaningful description before @var tag
120121
$isShortDescriptionPreviousVar = $phpcsFile->findPrevious(
121122
T_DOC_COMMENT_STRING,
@@ -125,23 +126,37 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
125126
null,
126127
false
127128
);
128-
if ($this->PHPDocFormattingValidator->providesMeaning(
129-
$isShortDescriptionPreviousVar,
130-
$commentStart,
131-
$tokens
132-
) !== true) {
133-
preg_match(
134-
'`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i',
135-
$tokens[($foundVar + 2)]['content'],
136-
$varParts
137-
);
138-
if ($varParts[1]) {
139-
return;
140-
}
129+
130+
if ($isShortDescriptionPreviousVar === false) {
131+
return;
132+
}
133+
134+
$propertyNamePosition = $phpcsFile->findNext(
135+
T_VARIABLE,
136+
$foundVar,
137+
null,
138+
false,
139+
null,
140+
false
141+
);
142+
if ($propertyNamePosition === false) {
143+
return;
144+
};
145+
$propertyName = trim($tokens[$propertyNamePosition]['content'], '$');
146+
$shortDescription = strtolower($tokens[$isShortDescriptionPreviousVar]['content']);
147+
148+
if ($shortDescription === strtolower($propertyName)) {
141149
$error = 'Short description duplicates class property name.';
142-
$phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningFulNameVar');
150+
$phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningfulNameVar');
143151
return;
144152
}
153+
154+
$propertyNameParts = array_filter(preg_split('/(?=[A-Z])/', $propertyName));
155+
156+
if ($shortDescription === strtolower(implode(' ', $propertyNameParts))) {
157+
$error = 'Short description duplicates class property name.';
158+
$phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningfulNameVar');
159+
}
145160
}
146161

147162
/**

Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc

+24-3
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,18 @@ class Bar {
6565
private $variableName;
6666

6767
/**
68-
* Some more invalid description
68+
* Correctly Formatted Protected Class Member
6969
*
70-
* @var test
70+
* @var correctlyFormattedProtectedClassMember
7171
*/
72-
protected $test;
72+
protected $correctlyFormattedProtectedClassMember;
73+
74+
/**
75+
* anotherCorrectlyFormattedProtectedClassMember
76+
*
77+
* @var anotherCorrectlyFormattedProtectedClassMember
78+
*/
79+
protected $anotherCorrectlyFormattedProtectedClassMember;
7380
}
7481

7582
class correctlyFormattedClassMemberDocBlock
@@ -99,4 +106,18 @@ class correctlyFormattedClassMemberDocBlock
99106
* \FooObject_TEST_C
100107
*/
101108
private $testObject;
109+
110+
/**
111+
* Fallback factory
112+
*
113+
* @var RulePool
114+
*/
115+
protected $rulePool;
116+
117+
/**
118+
* A description that includes test which is the same name as the variable is allowed
119+
*
120+
* @var test
121+
*/
122+
protected $test;
102123
}

Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public function getWarningList()
3232
49 => 1,
3333
56 => 1,
3434
63 => 1,
35-
68 => 1
35+
68 => 1,
36+
75 => 1,
3637
];
3738
}
3839
}

0 commit comments

Comments
 (0)