Skip to content

Commit 36960e4

Browse files
committed
Implement rule from magento#105
1 parent d2f07a9 commit 36960e4

6 files changed

+234
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento2\Sniffs\Commenting;
8+
9+
use PHP_CodeSniffer\Sniffs\Sniff;
10+
use PHP_CodeSniffer\Files\File;
11+
12+
/**
13+
* Detects PHPDoc formatting for classes and interfaces.
14+
*/
15+
class ClassAndInterfacePHPDocFormattingSniff extends ConstantsPHPDocFormattingSniff implements Sniff
16+
{
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function register()
21+
{
22+
return [
23+
T_CLASS,
24+
T_INTERFACE
25+
];
26+
}
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
public function process(File $phpcsFile, $stackPtr)
32+
{
33+
$tokens = $phpcsFile->getTokens();
34+
35+
$namePtr = $phpcsFile->findNext(T_STRING, $stackPtr + 1, null, false, null, true);
36+
37+
$commentStartPtr = $phpcsFile->findPrevious(
38+
[
39+
T_WHITESPACE,
40+
T_DOC_COMMENT_STAR,
41+
T_DOC_COMMENT_WHITESPACE,
42+
T_DOC_COMMENT_TAG,
43+
T_DOC_COMMENT_STRING,
44+
T_DOC_COMMENT_CLOSE_TAG
45+
],
46+
$stackPtr - 1,
47+
null,
48+
true,
49+
null,
50+
true
51+
);
52+
53+
if ($tokens[$commentStartPtr]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
54+
return;
55+
}
56+
57+
if ($this->providesMeaning($namePtr, $commentStartPtr, $tokens) !== true) {
58+
$phpcsFile->addWarning(
59+
'If exists, description should add additional information beyond what the type name already supplies.',
60+
$stackPtr,
61+
'InvalidDescription'
62+
);
63+
}
64+
65+
}
66+
}

Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php

+34-13
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,34 @@ public function process(File $phpcsFile, $stackPtr)
4545
null,
4646
true
4747
);
48-
$constName = strtolower(trim($tokens[$constNamePtr]['content'], " '\""));
4948

5049
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, null, false, null, true);
5150
if ($commentStartPtr === false) {
5251
return;
5352
}
5453

54+
if ($this->providesMeaning($constNamePtr, $commentStartPtr, $tokens) !== true) {
55+
$phpcsFile->addWarning(
56+
'Constants must have short description if they add information beyond what the constant name supplies.',
57+
$stackPtr,
58+
'MissingConstantPHPDoc'
59+
);
60+
}
61+
}
62+
63+
/**
64+
* Determines if the comment identified by $commentStartPtr provides additional meaning to origin at $namePtr
65+
*
66+
* @param int $namePtr
67+
* @param int $commentStartPtr
68+
* @param array $tokens
69+
* @return bool
70+
*/
71+
protected function providesMeaning($namePtr, $commentStartPtr, $tokens)
72+
{
5573
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
74+
$name = strtolower(str_replace([' ', '"', '_'], '', $tokens[$namePtr]['content']));
75+
5676
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
5777
$token = $tokens[$i];
5878

@@ -61,26 +81,27 @@ public function process(File $phpcsFile, $stackPtr)
6181
continue;
6282
}
6383

64-
// Comment is the same as constant name
65-
$docComment = trim(strtolower($token['content']), ',.');
66-
if ($docComment === $constName) {
84+
// Wrong kind of string
85+
if ($tokens[$i - 2]['code'] === T_DOC_COMMENT_TAG) {
86+
continue;
87+
}
88+
89+
// Comment is the same as the origin name
90+
$docComment = str_replace(['_', ' ', '.', ','], '', strtolower($token['content']));
91+
if ($docComment === $name) {
6792
continue;
6893
}
6994

70-
// Comment is exactly the same as constant name
71-
$docComment = str_replace(' ', '_', $docComment);
72-
if ($docComment === $constName) {
95+
// Only difference is word Class or Interface
96+
$docComment = str_replace(['class', 'interface'], '', $docComment);
97+
if ($docComment === $name) {
7398
continue;
7499
}
75100

76101
// We have found at lease one meaningful line in comment description
77-
return;
102+
return true;
78103
}
79104

80-
$phpcsFile->addWarning(
81-
'Constants must have short description if they add information beyond what the constant name supplies.',
82-
$stackPtr,
83-
'MissingConstantPHPDoc'
84-
);
105+
return false;
85106
}
86107
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* Handler for PHP errors/warnings/notices that converts them to exceptions.
5+
*/
6+
class ErrorHandler
7+
{
8+
9+
}
10+
11+
class NotAnErrorHandler
12+
{
13+
14+
}
15+
16+
/**
17+
* Faulty Handler
18+
*/
19+
class FaultyHandler
20+
{
21+
22+
}
23+
24+
/**
25+
* Class SomeHandler
26+
*/
27+
class SomeHandler
28+
{
29+
30+
}
31+
32+
/**
33+
* YetAnotherHandler
34+
*/
35+
class YetAnotherHandler
36+
{
37+
38+
}
39+
40+
/**
41+
* GreenHandler
42+
* @package NotFromArroundHere
43+
*/
44+
class GreenHandler
45+
{
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* Handler for PHP errors/warnings/notices that converts them to exceptions.
5+
*/
6+
interface ErrorHandler
7+
{
8+
9+
}
10+
11+
interface NotAnErrorHandler
12+
{
13+
14+
}
15+
16+
/**
17+
* Faulty Handler
18+
*/
19+
interface FaultyHandler
20+
{
21+
22+
}
23+
24+
/**
25+
* Interface SomeHandler
26+
*/
27+
interface SomeHandler
28+
{
29+
30+
}
31+
32+
/**
33+
* YetAnotherHandler
34+
*/
35+
interface YetAnotherHandler
36+
{
37+
38+
}
39+
40+
/**
41+
* GreenHandler
42+
* @package NotFromArroundHere
43+
*/
44+
interface GreenHandler
45+
{
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento2\Tests\Commenting;
8+
9+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
10+
11+
/**
12+
* Class ClassAndInterfacePHPDocFormattingUnitTest
13+
*/
14+
class ClassAndInterfacePHPDocFormattingUnitTest extends AbstractSniffUnitTest
15+
{
16+
/**
17+
* @inheritdoc
18+
*/
19+
public function getErrorList()
20+
{
21+
return [];
22+
}
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function getWarningList($testFile = '')
28+
{
29+
return [
30+
19 => 1,
31+
27 => 1,
32+
35 => 1,
33+
44 => 1
34+
];
35+
}
36+
}

Magento2/ruleset.xml

+4
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@
496496
</rule>
497497

498498
<!-- Severity 5 warnings: PHPDoc formatting and commenting issues. -->
499+
<rule ref="Magento2.Commenting.ClassAndInterfacePHPDocFormatting">
500+
<severity>5</severity>
501+
<type>warning</type>
502+
</rule>
499503
<rule ref="Magento2.Commenting.ConstantsPHPDocFormatting">
500504
<severity>5</severity>
501505
<type>warning</type>

0 commit comments

Comments
 (0)