Skip to content

Commit 2cb805a

Browse files
committed
Implement rule from #107
1 parent 73a7b7f commit 2cb805a

5 files changed

+168
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Sniffs\Commenting;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
11+
/**
12+
* Detects PHPDoc formatting for constants.
13+
*/
14+
class ConstantsPHPDocFormattingSniff implements Sniff
15+
{
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function register()
20+
{
21+
return [
22+
T_CONST,
23+
T_STRING
24+
];
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function process(File $phpcsFile, $stackPtr)
31+
{
32+
$tokens = $phpcsFile->getTokens();
33+
34+
if ($tokens[$stackPtr]['code'] != T_CONST
35+
&& !($tokens[$stackPtr]['content'] == 'define' && $tokens[$stackPtr+1]['code'] == T_OPEN_PARENTHESIS)
36+
) {
37+
return;
38+
}
39+
40+
$constNamePtr = $phpcsFile->findNext(
41+
($tokens[$stackPtr]['code'] === T_CONST) ? T_STRING : T_CONSTANT_ENCAPSED_STRING,
42+
$stackPtr + 1,
43+
null,
44+
false,
45+
null,
46+
true
47+
);
48+
$constName = strtolower(trim($tokens[$constNamePtr]['content'], " '\""));
49+
50+
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, null, false, null, true);
51+
if ($commentStartPtr === false) {
52+
return;
53+
}
54+
55+
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
56+
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
57+
$token = $tokens[$i];
58+
59+
// Not an interesting string
60+
if ($token['code'] !== T_DOC_COMMENT_STRING) {
61+
continue;
62+
}
63+
64+
// Comment is the same as constant name
65+
$docComment = trim(strtolower($token['content']), ',.');
66+
if ($docComment === $constName) {
67+
continue;
68+
}
69+
70+
// Comment is exactly the same as constant name
71+
$docComment = str_replace(' ', '_', $docComment);
72+
if ($docComment === $constName) {
73+
continue;
74+
}
75+
76+
// We have found at lease one meaningful line in comment description
77+
return;
78+
}
79+
80+
$phpcsFile->addWarning(
81+
'Constants must have short description if they add information beyond what the constant name supplies.',
82+
$stackPtr,
83+
'MissingConstantPHPDoc'
84+
);
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* Directory separator shorthand, intended to make code more readable.
5+
*/
6+
define('DS', DIRECTORY_SEPARATOR);
7+
8+
define('BP', dirname(__FILE__));
9+
10+
class Profiler
11+
{
12+
const NESTING_SEPARATOR = '->';
13+
14+
/**
15+
* Unlike first const, this one is not self explanatory.
16+
*/
17+
const NUMBER_TWO = 2;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Nesting separator.
4+
*/
5+
define("NESTING_SEPARATOR", '->0');
6+
7+
/** */
8+
define('NUMBER_ONE', 1);
9+
10+
class Profiler
11+
{
12+
/**
13+
* Nesting separator.
14+
*/
15+
const NESTING_SEPARATOR = '->';
16+
17+
/**
18+
*
19+
*/
20+
const NUMBER_TWO = 2;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
/**
11+
* Class ConstantsPHPDocFormattingUnitTest
12+
*/
13+
class ConstantsPHPDocFormattingUnitTest extends AbstractSniffUnitTest
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function getErrorList()
19+
{
20+
return [];
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
public function getWarningList($testFile = '')
27+
{
28+
if ($testFile === 'ConstantsPHPDocFormattingUnitTest.1.inc') {
29+
return [];
30+
}
31+
32+
return [
33+
5 => 1,
34+
8 => 1,
35+
15 => 1,
36+
20 => 1
37+
];
38+
}
39+
}

Magento2/ruleset.xml

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
<severity>10</severity>
4545
<type>error</type>
4646
</rule>
47+
<rule ref="Magento2.Commenting.ConstantsPHPDocFormatting">
48+
<severity>5</severity>
49+
<type>warning</type>
50+
</rule>
4751
<rule ref="Magento2.PHP.FinalImplementation">
4852
<severity>10</severity>
4953
<type>error</type>

0 commit comments

Comments
 (0)