Skip to content

Commit 8c154ae

Browse files
committed
Implement rule from #107
1 parent 73a7b7f commit 8c154ae

5 files changed

+169
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
/**
29+
* @inheritDoc
30+
*/
31+
public function process(File $phpcsFile, $stackPtr)
32+
{
33+
$tokens = $phpcsFile->getTokens();
34+
35+
if ($tokens[$stackPtr]['code'] != T_CONST
36+
&& !($tokens[$stackPtr]['content'] == 'define' && $tokens[$stackPtr+1]['code'] == T_OPEN_PARENTHESIS)
37+
) {
38+
return;
39+
}
40+
41+
$constNamePtr = $phpcsFile->findNext(
42+
($tokens[$stackPtr]['code'] === T_CONST) ? T_STRING : T_CONSTANT_ENCAPSED_STRING,
43+
$stackPtr + 1,
44+
null,
45+
false,
46+
null,
47+
true
48+
);
49+
$constName = strtolower(trim($tokens[$constNamePtr]['content'], " '\""));
50+
51+
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, null, false, null, true);
52+
if ($commentStartPtr === false) {
53+
return;
54+
}
55+
56+
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
57+
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
58+
$token = $tokens[$i];
59+
60+
// Not an interesting string
61+
if ($token['code'] !== T_DOC_COMMENT_STRING) {
62+
continue;
63+
}
64+
65+
// Comment is the same as constant name
66+
$docComment = trim(strtolower($token['content']), ',.');
67+
if ($docComment === $constName) {
68+
continue;
69+
}
70+
71+
// Comment is exactly the same as constant name
72+
$docComment = str_replace(' ', '_', $docComment);
73+
if ($docComment === $constName) {
74+
continue;
75+
}
76+
77+
// We have found at lease one meaningful line in comment description
78+
return;
79+
}
80+
81+
$phpcsFile->addWarning(
82+
'Constants must have short description if they add information beyond what the constant name supplies.',
83+
$stackPtr,
84+
'MissingConstantPHPDoc'
85+
);
86+
}
87+
}
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)