@@ -712,6 +712,71 @@ public function testParseErrorVariableLabel()
712
712
}//end testParseErrorVariableLabel()
713
713
714
714
715
+ /**
716
+ * Verify whether the colons are tokenized correctly when a return type is used for an inline
717
+ * closure/arrow function declaration in a ternary.
718
+ *
719
+ * @param string $testMarker The comment prefacing the target token.
720
+ *
721
+ * @dataProvider dataOtherColonsInTernary
722
+ * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
723
+ *
724
+ * @return void
725
+ */
726
+ public function testOtherColonsInTernary ($ testMarker )
727
+ {
728
+ $ tokens = self ::$ phpcsFile ->getTokens ();
729
+
730
+ $ startOfStatement = $ this ->getTargetToken ($ testMarker , T_VARIABLE );
731
+
732
+ // Walk the statement and check the tokenization.
733
+ // There should be no T_PARAM_NAME tokens.
734
+ // First colon should be T_COLON for the return type.
735
+ // Second colon should be T_INLINE_ELSE for the ternary.
736
+ // Third colon should be T_COLON for the return type.
737
+ $ colonCount = 0 ;
738
+ for ($ i = ($ startOfStatement + 1 ); $ tokens [$ i ]['line ' ] === $ tokens [$ startOfStatement ]['line ' ]; $ i ++) {
739
+ $ this ->assertNotEquals (T_PARAM_NAME , $ tokens [$ i ]['code ' ], "Token $ i is tokenized as parameter label " );
740
+
741
+ if ($ tokens [$ i ]['content ' ] === ': ' ) {
742
+ ++$ colonCount ;
743
+
744
+ if ($ colonCount === 1 ) {
745
+ $ this ->assertSame (T_COLON , $ tokens [$ i ]['code ' ], 'First colon is not tokenized as T_COLON ' );
746
+ } else if ($ colonCount === 2 ) {
747
+ $ this ->assertSame (T_INLINE_ELSE , $ tokens [$ i ]['code ' ], 'Second colon is not tokenized as T_INLINE_ELSE ' );
748
+ } else if ($ colonCount === 3 ) {
749
+ $ this ->assertSame (T_COLON , $ tokens [$ i ]['code ' ], 'Third colon is not tokenized as T_COLON ' );
750
+ } else {
751
+ $ this ->fail ('Unexpected colon encountered in statement ' );
752
+ }
753
+ }
754
+ }
755
+
756
+ }//end testOtherColonsInTernary()
757
+
758
+
759
+ /**
760
+ * Data provider.
761
+ *
762
+ * @see testOtherColonsInTernary()
763
+ *
764
+ * @return array<string, array<string, string>>
765
+ */
766
+ public static function dataOtherColonsInTernary ()
767
+ {
768
+ return [
769
+ 'closures with return types in ternary ' => [
770
+ 'testMarker ' => '/* testTernaryWithClosuresAndReturnTypes */ ' ,
771
+ ],
772
+ 'arrow functions with return types in ternary ' => [
773
+ 'testMarker ' => '/* testTernaryWithArrowFunctionsAndReturnTypes */ ' ,
774
+ ],
775
+ ];
776
+
777
+ }//end dataOtherColonsInTernary()
778
+
779
+
715
780
/**
716
781
* Verify that reserved keywords used as a parameter label are tokenized as T_PARAM_NAME
717
782
* and that the colon after it is tokenized as a T_COLON.
0 commit comments