@@ -45,14 +45,34 @@ public function process(File $phpcsFile, $stackPtr)
45
45
null ,
46
46
true
47
47
);
48
- $ constName = strtolower (trim ($ tokens [$ constNamePtr ]['content ' ], " ' \"" ));
49
48
50
49
$ commentStartPtr = $ phpcsFile ->findPrevious (T_DOC_COMMENT_OPEN_TAG , $ stackPtr - 1 , null , false , null , true );
51
50
if ($ commentStartPtr === false ) {
52
51
return ;
53
52
}
54
53
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
+ {
55
73
$ commentCloserPtr = $ tokens [$ commentStartPtr ]['comment_closer ' ];
74
+ $ name = strtolower (str_replace ([' ' , '" ' , '_ ' ], '' , $ tokens [$ namePtr ]['content ' ]));
75
+
56
76
for ($ i = $ commentStartPtr ; $ i <= $ commentCloserPtr ; $ i ++) {
57
77
$ token = $ tokens [$ i ];
58
78
@@ -61,26 +81,27 @@ public function process(File $phpcsFile, $stackPtr)
61
81
continue ;
62
82
}
63
83
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 ) {
67
92
continue ;
68
93
}
69
94
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 ) {
73
98
continue ;
74
99
}
75
100
76
101
// We have found at lease one meaningful line in comment description
77
- return ;
102
+ return true ;
78
103
}
79
104
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 ;
85
106
}
86
107
}
0 commit comments