Skip to content

Commit 13ceb9a

Browse files
committed
implement rule from #131
1 parent a916b11 commit 13ceb9a

9 files changed

+254
-8
lines changed

Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ public function process(File $phpcsFile, $stackPtr)
8787
);
8888
}
8989

90+
if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStartPtr, $tokens) !== true) {
91+
$phpcsFile->addWarning(
92+
'Motivation behind the added @deprecated tag MUST be explained. '
93+
. '@see tag MUST be used with reference to new implementation.',
94+
$stackPtr,
95+
'InvalidDeprecatedTagUsage'
96+
);
97+
}
98+
9099
$this->validateTags($phpcsFile, $commentStartPtr, $tokens);
91100
}
92101

Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php

+9
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,14 @@ public function process(File $phpcsFile, $stackPtr)
7171
'MissingConstantPHPDoc'
7272
);
7373
}
74+
75+
if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStartPtr, $tokens) !== true) {
76+
$phpcsFile->addWarning(
77+
'Motivation behind the added @deprecated tag MUST be explained. '
78+
. '@see tag MUST be used with reference to new implementation.',
79+
$stackPtr,
80+
'InvalidDeprecatedTagUsage'
81+
);
82+
}
7483
}
7584
}

Magento2/Sniffs/Commenting/PHPDocFormattingValidator.php

+50
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,54 @@ public function providesMeaning($namePtr, $commentStartPtr, $tokens)
7171

7272
return false;
7373
}
74+
75+
/**
76+
* In case comment has deprecated tag, it must be explained and followed by see tag with details
77+
*
78+
* @param int $commentStartPtr
79+
* @param array $tokens
80+
* @return bool
81+
*/
82+
public function hasDeprecatedWellFormatted($commentStartPtr, $tokens)
83+
{
84+
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
85+
86+
$hasDeprecated = false;
87+
$hasSee = false;
88+
89+
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
90+
$token = $tokens[$i];
91+
92+
// Not interesting
93+
if ($token['code'] !== T_DOC_COMMENT_TAG) {
94+
continue;
95+
}
96+
97+
$tag = $token['content'];
98+
99+
// Not an interesting tag
100+
if ($tag !== '@deprecated' && $tag !== '@see') {
101+
continue;
102+
}
103+
104+
if ($tag === '@deprecated') {
105+
$hasDeprecated = true;
106+
}
107+
108+
if ($tag === '@see') {
109+
$hasSee = true;
110+
}
111+
112+
// Tag not followed by description
113+
if ($tokens[$i + 2]['code'] !== T_DOC_COMMENT_STRING) {
114+
return false;
115+
}
116+
}
117+
118+
if ($hasDeprecated === true && $hasSee !== true) {
119+
return false;
120+
}
121+
122+
return true;
123+
}
74124
}

Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.1.inc

+56-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class EmptyHandler
5959
*
6060
* @api is ok here
6161
* @deprecated can be used in this context
62+
* @see is ok here
6263
* @author is actually ok
6364
* @category is irrelevant
6465
* @package is not ment to be used
@@ -90,4 +91,58 @@ class AsyncApiHandler
9091
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
9192
*/
9293
class GroupRepositoryHandler
93-
{}
94+
{
95+
96+
}
97+
98+
/**
99+
* @deprecated
100+
*/
101+
class DeprecatedHandler
102+
{
103+
104+
}
105+
106+
/**
107+
* @deprecated Should not be used
108+
*/
109+
class AncientHandler
110+
{
111+
112+
}
113+
114+
/**
115+
* @deprecated
116+
* @see
117+
*/
118+
class AgedHandler
119+
{
120+
121+
}
122+
123+
/**
124+
* @deprecated Should not be used
125+
* @see
126+
*/
127+
class ArhaicHandler
128+
{
129+
130+
}
131+
132+
/**
133+
* @deprecated Should not be used
134+
* @see Magento\Framework\NewHandler
135+
*/
136+
class OldHandler
137+
{
138+
139+
}
140+
141+
/**
142+
* @see Magento\Framework\NewHandler
143+
*/
144+
interface SomethingHandler
145+
{
146+
147+
}
148+

Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.2.inc

+56-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ interface EmptyHandler
5959
*
6060
* @api is ok here
6161
* @deprecated can be used in this context
62+
* @see is ok here
6263
* @author is actually ok
6364
* @category is irrelevant
6465
* @package is not ment to be used
@@ -89,5 +90,58 @@ interface AsyncApiHandler
8990
/**
9091
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
9192
*/
92-
class GroupRepositoryHandler
93-
{}
93+
interface GroupRepositoryHandler
94+
{
95+
96+
}
97+
98+
/**
99+
* @deprecated
100+
*/
101+
interface DeprecatedHandler
102+
{
103+
104+
}
105+
106+
/**
107+
* @deprecated Should not be used
108+
*/
109+
interface AncientHandler
110+
{
111+
112+
}
113+
114+
/**
115+
* @deprecated
116+
* @see
117+
*/
118+
interface AgedHandler
119+
{
120+
121+
}
122+
123+
/**
124+
* @deprecated Should not be used
125+
* @see
126+
*/
127+
interface ArhaicHandler
128+
{
129+
130+
}
131+
132+
/**
133+
* @deprecated Should not be used
134+
* @see Magento\Framework\NewHandler
135+
*/
136+
interface OldHandler
137+
{
138+
139+
}
140+
141+
/**
142+
* @see Magento\Framework\NewHandler
143+
*/
144+
interface SomethingHandler
145+
{
146+
147+
}

Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ public function getWarningList($testFile = '')
2929
35 => 1,
3030
44 => 1,
3131
52 => 1,
32-
63 => 1,
3332
64 => 1,
3433
65 => 1,
34+
66 => 1,
35+
101 => 1,
36+
109 => 1,
37+
118 => 1,
38+
127 => 1,
3539
];
3640
}
3741
}

Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.1.inc

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ define('DS', DIRECTORY_SEPARATOR);
77

88
define('BP', dirname(__FILE__));
99

10+
/**
11+
* @deprecated New implementation available
12+
* @see \Ascii\Asterisk
13+
*/
14+
define('ANSWER', '42');
15+
1016
class Profiler
1117
{
1218
const NESTING_SEPARATOR = '->';
@@ -15,4 +21,10 @@ class Profiler
1521
* Unlike first const, this one is not self explanatory.
1622
*/
1723
const NUMBER_TWO = 2;
24+
25+
/**
26+
* @deprecated Unable to identify the question, replaced
27+
* @see \ComputationalMatrix\Earth
28+
*/
29+
const COMPUTER = 'Deep Thought';
1830
}

Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.2.inc

+45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Nesting separator.
45
*/
@@ -7,6 +8,28 @@ define("NESTING_SEPARATOR", '->0');
78
/** */
89
define('NUMBER_ONE', 1);
910

11+
/**
12+
* @deprecated
13+
*/
14+
define('A', 65);
15+
16+
/**
17+
* @deprecated Slightly better, but no see tag
18+
*/
19+
define('B', 66);
20+
21+
/**
22+
* @deprecated
23+
* @see
24+
*/
25+
define('C', 67);
26+
27+
/**
28+
* @deprecated No reference specified
29+
* @see
30+
*/
31+
define('D', 68);
32+
1033
class Profiler
1134
{
1235
/**
@@ -18,4 +41,26 @@ class Profiler
1841
*
1942
*/
2043
const NUMBER_TWO = 2;
44+
45+
/**
46+
* @deprecated
47+
*/
48+
const a = 97;
49+
50+
/**
51+
* @deprecated Slightly better, but no see tag
52+
*/
53+
const b = 98;
54+
55+
/**
56+
* @deprecated
57+
* @see
58+
*/
59+
const c = 99;
60+
61+
/**
62+
* @deprecated No reference specified
63+
* @see
64+
*/
65+
const d = 100;
2166
}

Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ public function getWarningList($testFile = '')
2727
}
2828

2929
return [
30-
5 => 1,
31-
8 => 1,
32-
15 => 1,
33-
20 => 1
30+
6 => 1,
31+
9 => 1,
32+
14 => 1,
33+
19 => 1,
34+
25 => 1,
35+
31 => 1,
36+
38 => 1,
37+
43 => 1,
38+
48 => 1,
39+
53 => 1,
40+
59 => 1,
41+
65 => 1
3442
];
3543
}
3644
}

0 commit comments

Comments
 (0)