@@ -129,7 +129,7 @@ final class FileNameSniff extends Sniff {
129129 /**
130130 * Returns an array of tokens this test wants to listen for.
131131 *
132- * @return array<int,int>
132+ * @return array<int|string ,int|string >
133133 */
134134 public function register () {
135135 if ( \defined ( '\PHP_CODESNIFFER_IN_TESTS ' ) ) {
@@ -302,9 +302,14 @@ protected function check_filename_is_hyphenated( $file_name ) {
302302 * @return bool
303303 */
304304 protected function check_filename_has_class_prefix ( $ class_ptr , $ file_name ) {
305- $ extension = strrchr ( $ file_name , '. ' );
306- $ class_name = ObjectDeclarations::getName ( $ this ->phpcsFile , $ class_ptr );
307- $ properties = ObjectDeclarations::getClassProperties ( $ this ->phpcsFile , $ class_ptr );
305+ $ extension = strrchr ( $ file_name , '. ' );
306+ $ class_name = ObjectDeclarations::getName ( $ this ->phpcsFile , $ class_ptr );
307+ $ properties = ObjectDeclarations::getClassProperties ( $ this ->phpcsFile , $ class_ptr );
308+
309+ if ( null === $ class_name ) {
310+ return true ;
311+ }
312+
308313 $ expected = 'class- ' . $ this ->kebab ( $ class_name ) . $ extension ;
309314 $ err_message = 'Class file names should be based on the class name with "class-" prepended. Expected %s, but found %s. ' ;
310315
@@ -332,8 +337,13 @@ protected function check_filename_has_class_prefix( $class_ptr, $file_name ) {
332337 * @return bool
333338 */
334339 protected function check_filename_has_trait_prefix ( $ trait_ptr , $ file_name ) {
335- $ extension = strrchr ( $ file_name , '. ' );
336- $ trait_name = ObjectDeclarations::getName ( $ this ->phpcsFile , $ trait_ptr );
340+ $ extension = strrchr ( $ file_name , '. ' );
341+ $ trait_name = ObjectDeclarations::getName ( $ this ->phpcsFile , $ trait_ptr );
342+
343+ if ( null === $ trait_name ) {
344+ return true ;
345+ }
346+
337347 $ expected = 'trait- ' . $ this ->kebab ( $ trait_name ) . $ extension ;
338348 $ err_message = 'Trait file names should be based on the trait name with "trait-" prepended. Expected %s, but found %s. ' ;
339349
@@ -358,8 +368,13 @@ protected function check_filename_has_trait_prefix( $trait_ptr, $file_name ) {
358368 protected function check_filename_has_interface_prefix ( $ interface_ptr , $ file_name ) {
359369 $ extension = strrchr ( $ file_name , '. ' );
360370 $ interface_name = ObjectDeclarations::getName ( $ this ->phpcsFile , $ interface_ptr );
361- $ expected = 'interface- ' . $ this ->kebab ( $ interface_name ) . $ extension ;
362- $ err_message = 'Interface file names should be based on the interface name with "interface-" prepended. Expected %s, but found %s. ' ;
371+
372+ if ( null === $ interface_name ) {
373+ return true ;
374+ }
375+
376+ $ expected = 'interface- ' . $ this ->kebab ( $ interface_name ) . $ extension ;
377+ $ err_message = 'Interface file names should be based on the interface name with "interface-" prepended. Expected %s, but found %s. ' ;
363378
364379 if ( $ file_name === $ expected ) {
365380 return true ;
@@ -380,8 +395,13 @@ protected function check_filename_has_interface_prefix( $interface_ptr, $file_na
380395 * @return bool
381396 */
382397 protected function check_filename_has_enum_prefix ( $ enum_ptr , $ file_name ) {
383- $ extension = strrchr ( $ file_name , '. ' );
384- $ enum_name = ObjectDeclarations::getName ( $ this ->phpcsFile , $ enum_ptr );
398+ $ extension = strrchr ( $ file_name , '. ' );
399+ $ enum_name = ObjectDeclarations::getName ( $ this ->phpcsFile , $ enum_ptr );
400+
401+ if ( null === $ enum_name ) {
402+ return true ;
403+ }
404+
385405 $ expected = 'enum- ' . $ this ->kebab ( $ enum_name ) . $ extension ;
386406 $ err_message = 'Enum file names should be based on the enum name with "enum-" prepended. Expected %s, but found %s. ' ;
387407
@@ -404,6 +424,11 @@ protected function check_filename_has_enum_prefix( $enum_ptr, $file_name ) {
404424 protected function kebab ( $ filename = '' ) {
405425 $ kebab = preg_replace ( '`[[:punct:]]` ' , '- ' , $ filename );
406426 $ kebab = preg_replace ( '/(?>(?!^[A-Z]))([a-z])([A-Z])/ ' , '$1-$2 ' , $ filename );
427+
428+ if ( null === $ kebab ) {
429+ $ kebab = $ filename ;
430+ }
431+
407432 $ kebab = strtolower ( $ kebab );
408433 $ kebab = str_replace ( '_ ' , '- ' , $ kebab );
409434
0 commit comments