@@ -115,6 +115,12 @@ public function process(File $phpcsFile, $stackPtr) {
115115 return ;
116116 }
117117
118+ // Allow phpdoc block before "return new class extends" expressions,
119+ // we use those anon classes in places like coverage.php files.
120+ if ($ this ->is_return_new_class_extends ($ phpcsFile , $ stackPtr )) {
121+ return ;
122+ }
123+
118124 // Allow phpdoc before define() token (see CONTRIB-4150).
119125 if ($ tokens [$ nextToken ]['code ' ] == T_STRING and $ tokens [$ nextToken ]['content ' ] == 'define ' ) {
120126 return ;
@@ -515,5 +521,45 @@ public function process(File $phpcsFile, $stackPtr) {
515521
516522 }//end process()
517523
524+ /**
525+ * This looks if there is a valid "return new class extends" expression allowed to have phpdoc block.
526+ *
527+ * @param File $file The file being scanned.
528+ * @param int $pointer The position in the stack.
529+ * @return bool true if is an allowed to have phpdoc block return new class code.
530+ */
531+ protected function is_return_new_class_extends (File $ file , $ pointer ) {
532+
533+ $ ignoredtokens = Tokens::$ emptyTokens ;
534+
535+ $ tokens = $ file ->getTokens ();
536+
537+ // Detect 'return'.
538+ $ pointer = $ file ->findNext ($ ignoredtokens , $ pointer + 1 , null , true );
539+ if ($ tokens [$ pointer ]['code ' ] !== T_RETURN ) {
540+ return false ;
541+ }
542+
543+ // Detect 'new'.
544+ $ pointer = $ file ->findNext ($ ignoredtokens , $ pointer + 1 , null , true );
545+ if ($ tokens [$ pointer ]['code ' ] !== T_NEW ) {
546+ return false ;
547+ }
548+
549+ // Detect 'class'.
550+ $ pointer = $ file ->findNext ($ ignoredtokens , $ pointer + 1 , null , true );
551+ if ($ tokens [$ pointer ]['code ' ] !== T_ANON_CLASS ) {
552+ return false ;
553+ }
554+
555+ // Detect 'extends'.
556+ $ pointer = $ file ->findNext ($ ignoredtokens , $ pointer + 1 , null , true );
557+ if ($ tokens [$ pointer ]['code ' ] !== T_EXTENDS ) {
558+ return false ;
559+ }
560+
561+ // Found a valid "return new class extends" expression, phpdoc block allowed.
562+ return true ;
563+ }// end is_return_new_class_extends()
518564
519565}//end class
0 commit comments