Skip to content

Commit ef00afd

Browse files
committed
Fixed wrongly returning error for valid descriptions
1 parent 0d6cb3c commit ef00afd

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php

Lines changed: 22 additions & 16 deletions
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,28 @@ 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 (stripos($tokens[$isShortDescriptionPreviousVar]['content'], $tokens[$string]['content']) !== false) {
141131
$error = 'Short description duplicates class property name.';
142-
$phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningFulNameVar');
132+
$phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningfulNameVar');
143133
return;
144134
}
135+
$re = '/
136+
# Split camelCase "words". Two global alternatives. Either g1of2:
137+
(?<=[a-z]) # Position is after a lowercase,
138+
(?=[A-Z]) # and before an uppercase letter.
139+
| (?<=[A-Z]) # Or g2of2; Position is after uppercase,
140+
(?=[A-Z][a-z]) # and before upper-then-lower case.
141+
/x';
142+
$varTagParts = preg_split($re, $tokens[$string]['content']);
143+
144+
foreach ($varTagParts as $part) {
145+
if (stripos($tokens[$isShortDescriptionPreviousVar]['content'], $part) === false) {
146+
return;
147+
}
148+
}
149+
$error = 'Short description duplicates class property name.';
150+
$phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningfulNameVar');
145151
}
146152

147153
/**

Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,25 @@ class Bar {
6565
private $variableName;
6666

6767
/**
68-
* Some more invalid description
68+
* Some more invalid description with test which is the same name as the variable and is not allowed
6969
*
7070
* @var test
7171
*/
7272
protected $test;
73+
74+
/**
75+
* Formatted Correctly Protected Class Member
76+
*
77+
* @var correctlyFormattedProtectedClassMember
78+
*/
79+
protected $correctlyFormattedProtectedClassMember;
80+
81+
/**
82+
* anotherCorrectlyFormattedProtectedClassMember
83+
*
84+
* @var anotherCorrectlyFormattedProtectedClassMember
85+
*/
86+
protected $anotherCorrectlyFormattedProtectedClassMember;
7387
}
7488

7589
class correctlyFormattedClassMemberDocBlock
@@ -99,4 +113,11 @@ class correctlyFormattedClassMemberDocBlock
99113
* \FooObject_TEST_C
100114
*/
101115
private $testObject;
116+
117+
/**
118+
* Fallback factory
119+
*
120+
* @var RulePool
121+
*/
122+
protected $rulePool;
102123
}

Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public function getWarningList()
3232
49 => 1,
3333
56 => 1,
3434
63 => 1,
35-
68 => 1
35+
68 => 1,
36+
75 => 1,
37+
82 => 1,
3638
];
3739
}
3840
}

0 commit comments

Comments
 (0)