Skip to content

Commit 5fd88b5

Browse files
New rule added for class property PHP Doc block
1 parent 24607be commit 5fd88b5

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
namespace Magento2\Sniffs\Commenting;
3+
4+
use PHP_CodeSniffer\Files\File;
5+
use PHP_CodeSniffer\Sniffs\Sniff;
6+
7+
class ClassPropertyPHPDocFormattingSniff implements Sniff
8+
{
9+
10+
private $ignoreTokens = [
11+
T_PUBLIC,
12+
T_PRIVATE,
13+
T_PROTECTED,
14+
T_VAR,
15+
T_STATIC,
16+
T_WHITESPACE,
17+
];
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function register()
23+
{
24+
return [
25+
T_VARIABLE
26+
];
27+
}
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function process(File $phpcsFile, $stackPtr)
33+
{
34+
$tokens = $phpcsFile->getTokens();
35+
36+
$commentEnd = $phpcsFile->findPrevious($this->ignoreTokens, ($stackPtr - 1), null, true);
37+
if ($commentEnd === false
38+
|| ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
39+
&& $tokens[$commentEnd]['code'] !== T_COMMENT)
40+
) {
41+
$phpcsFile->addWarning('Missing class property doc comment', $stackPtr, 'Missing');
42+
return;
43+
}
44+
45+
$commentStart = $tokens[$commentEnd]['comment_opener'];
46+
47+
$foundVar = null;
48+
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
49+
if ($tokens[$tag]['content'] === '@var') {
50+
if ($foundVar !== null) {
51+
$error = 'Only one @var tag is allowed in a class property comment';
52+
$phpcsFile->addWarning($error, $tag, 'DuplicateVar');
53+
} else {
54+
$foundVar = $tag;
55+
}
56+
}
57+
}
58+
59+
if ($foundVar === null) {
60+
$error = 'Missing @var tag in class property comment';
61+
$phpcsFile->addWarning($error, $commentEnd, 'MissingVar');
62+
return;
63+
}
64+
65+
$string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd);
66+
if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) {
67+
$error = 'Content missing for @var tag in class property comment';
68+
$phpcsFile->addWarning($error, $foundVar, 'EmptyVar');
69+
return;
70+
}
71+
}
72+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
class Foo
4+
{
5+
/**
6+
* @var Foo
7+
*/
8+
private $_classAttribute = '';
9+
10+
/**
11+
* Foo
12+
*/
13+
private $_withoutClassAttribute = '';
14+
15+
/**
16+
* @var Test
17+
*
18+
* Short Description
19+
*/
20+
private $_classAttributeWithShortDescription = '';
21+
22+
/**
23+
* @var
24+
*/
25+
private $_emptyClassAttributeContent = '';
26+
27+
28+
/**
29+
* @var Foo
30+
* @var Bar
31+
*/
32+
private $_multipleClassAttribute = '';
33+
34+
private $_missingDocBlockClassAttribute = '';
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Commenting;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
class ClassPropertyPHPDocFormattingUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList()
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getWarningList()
24+
{
25+
return [
26+
12 => 1,
27+
23 => 1,
28+
30 => 1,
29+
34 => 1
30+
];
31+
}
32+
}

Magento2/ruleset.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,10 @@
528528
<severity>5</severity>
529529
<type>warning</type>
530530
</rule>
531+
<rule ref="Magento2.Commenting.ClassPropertyPHPDocFormatting">
532+
<severity>5</severity>
533+
<type>warning</type>
534+
</rule>
531535
<rule ref="Magento2.Commenting.ConstantsPHPDocFormatting">
532536
<severity>5</severity>
533537
<type>warning</type>

0 commit comments

Comments
 (0)