@@ -59,37 +59,14 @@ public function process_token( $stackPtr ) {
59
59
60
60
unset( $ expected );
61
61
62
- /*
63
- * Check files containing a class for the "class-" prefix and that the rest of
64
- * the file name reflects the class name. Accounts for abstract classes.
65
- */
66
- if ( true === $ this ->strict_class_file_names ) {
67
- $ has_class = $ this ->phpcsFile ->findNext ( \T_CLASS , $ stackPtr );
68
-
69
- if ( false !== $ has_class && false === $ this ->is_test_class ( $ has_class ) ) {
70
- $ is_abstract = $ this ->phpcsFile ->findPrevious ( \T_ABSTRACT , $ has_class );
71
- $ class_name = $ this ->phpcsFile ->getDeclarationName ( $ has_class );
72
-
73
- if ( is_null ( $ class_name ) ) {
74
- $ class_name = 'anonymous ' ;
75
- }
76
-
77
- $ expected = 'class- ' . $ this ->kebab ( $ class_name );
78
- $ err_message = 'Class file names should be based on the class name with "class-" prepended. Expected %s, but found %s. ' ;
79
-
80
- if ( $ is_abstract ) {
81
- $ expected = 'abstract- ' . $ expected ;
82
- $ err_message = 'Abstract class file names should be based on the class name with "abstract-class-" prepended. Expected %s, but found %s. ' ;
83
- }
84
-
85
- if ( substr ( $ fileName , 0 , -4 ) !== $ expected ) {
86
- $ this ->phpcsFile ->addError ( $ err_message , 0 , 'InvalidClassFileName ' , [ $ expected . '.php ' , $ fileName ] );
87
- }
88
-
89
- unset( $ expected );
90
- }
62
+ if ( true !== $ this ->strict_class_file_names ) {
63
+ return ( $ this ->phpcsFile ->numTokens + 1 );
91
64
}
92
65
66
+ $ this ->check_maybe_class ( $ stackPtr , $ fileName );
67
+ $ this ->check_maybe_trait ( $ stackPtr , $ fileName );
68
+ $ this ->check_maybe_interface ( $ stackPtr , $ fileName );
69
+
93
70
// Only run this sniff once per file, no need to run it again.
94
71
return ( $ this ->phpcsFile ->numTokens + 1 );
95
72
}
@@ -129,6 +106,99 @@ protected function is_disabled_by_comments() {
129
106
return false ;
130
107
}
131
108
109
+ /**
110
+ * Check files containing a class for the "class-" prefix,
111
+ * and that the rest of the file name reflects the class name.
112
+ * Accounts for abstract classes.
113
+ *
114
+ * @param mixed $stackPtr the token stack
115
+ * @param string $fileName the name of the file
116
+ *
117
+ * @return void
118
+ */
119
+ protected function check_maybe_class ( $ stackPtr , $ fileName ) {
120
+ $ has_class = $ this ->phpcsFile ->findNext ( \T_CLASS , $ stackPtr );
121
+
122
+ if ( false === $ has_class || false !== $ this ->is_test_class ( $ has_class ) ) {
123
+ return ;
124
+ }
125
+
126
+ $ is_abstract = $ this ->phpcsFile ->findPrevious ( \T_ABSTRACT , $ has_class );
127
+ $ class_name = $ this ->phpcsFile ->getDeclarationName ( $ has_class );
128
+
129
+ if ( is_null ( $ class_name ) ) {
130
+ $ class_name = 'anonymous ' ;
131
+ }
132
+
133
+ $ expected = 'class- ' . $ this ->kebab ( $ class_name );
134
+ $ err_message = 'Class file names should be based on the class name with "class-" prepended. Expected %s, but found %s. ' ;
135
+
136
+ if ( $ is_abstract ) {
137
+ $ expected = 'abstract- ' . $ expected ;
138
+ $ err_message = 'Abstract class file names should be based on the class name with "abstract-class-" prepended. Expected %s, but found %s. ' ;
139
+ }
140
+
141
+ if ( substr ( $ fileName , 0 , -4 ) === $ expected ) {
142
+ return ;
143
+ }
144
+
145
+ $ this ->phpcsFile ->addError ( $ err_message , 0 , 'InvalidClassFileName ' , [ $ expected . '.php ' , $ fileName ] );
146
+ }
147
+
148
+ /**
149
+ * Check files containing a trait for the "trait-" prefix,
150
+ * and that the rest of the file name reflects the trait name.
151
+ *
152
+ * @param mixed $stackPtr the token stack
153
+ * @param string $fileName the name of the file
154
+ *
155
+ * @return void
156
+ */
157
+ protected function check_maybe_trait ( $ stackPtr , $ fileName ) {
158
+ $ has_trait = $ this ->phpcsFile ->findNext ( \T_TRAIT , $ stackPtr );
159
+
160
+ if ( false === $ has_trait || false !== $ this ->is_test_class ( $ has_trait ) ) {
161
+ return ;
162
+ }
163
+
164
+ $ trait_name = $ this ->phpcsFile ->getDeclarationName ( $ has_trait );
165
+ $ expected = 'trait- ' . $ this ->kebab ( $ trait_name );
166
+ $ err_message = 'Trait file names should be based on the class name with "trait-" prepended. Expected %s, but found %s. ' ;
167
+
168
+ if ( substr ( $ fileName , 0 , -4 ) === $ expected ) {
169
+ return ;
170
+ }
171
+
172
+ $ this ->phpcsFile ->addError ( $ err_message , 0 , 'InvalidTraitFileName ' , [ $ expected . '.php ' , $ fileName ] );
173
+ }
174
+
175
+ /**
176
+ * Check files containing an interface for the "interface-" prefix,
177
+ * and that the rest of the file name reflects the interface name.
178
+ *
179
+ * @param mixed $stackPtr the token stack
180
+ * @param string $fileName the name of the file
181
+ *
182
+ * @return void
183
+ */
184
+ protected function check_maybe_interface ( $ stackPtr , $ fileName ) {
185
+ $ has_interface = $ this ->phpcsFile ->findNext ( \T_INTERFACE , $ stackPtr );
186
+
187
+ if ( false === $ has_interface || false !== $ this ->is_test_class ( $ has_interface ) ) {
188
+ return ;
189
+ }
190
+
191
+ $ interface_name = $ this ->phpcsFile ->getDeclarationName ( $ has_interface );
192
+ $ expected = 'interface- ' . $ this ->kebab ( $ interface_name );
193
+ $ err_message = 'Interface file names should be based on the interface name with "interface-" prepended. Expected %s, but found %s. ' ;
194
+
195
+ if ( substr ( $ fileName , 0 , -4 ) === $ expected ) {
196
+ return ;
197
+ }
198
+
199
+ $ this ->phpcsFile ->addError ( $ err_message , 0 , 'InvalidInterfaceFileName ' , [ $ expected . '.php ' , $ fileName ] );
200
+ }
201
+
132
202
/**
133
203
* Convert a string to kebab-case
134
204
*
0 commit comments