@@ -462,6 +462,7 @@ class PHP extends Tokenizer
462462 T_OPEN_SHORT_ARRAY => 1 ,
463463 T_CLOSE_SHORT_ARRAY => 1 ,
464464 T_TYPE_UNION => 1 ,
465+ T_TYPE_INTERSECTION => 1 ,
465466 ];
466467
467468 /**
@@ -2438,18 +2439,19 @@ protected function processAdditional()
24382439 if (isset ($ this ->tokens [$ x ]) === true && $ this ->tokens [$ x ]['code ' ] === T_OPEN_PARENTHESIS ) {
24392440 $ ignore = Util \Tokens::$ emptyTokens ;
24402441 $ ignore += [
2441- T_ARRAY => T_ARRAY ,
2442- T_CALLABLE => T_CALLABLE ,
2443- T_COLON => T_COLON ,
2444- T_NAMESPACE => T_NAMESPACE ,
2445- T_NS_SEPARATOR => T_NS_SEPARATOR ,
2446- T_NULL => T_NULL ,
2447- T_NULLABLE => T_NULLABLE ,
2448- T_PARENT => T_PARENT ,
2449- T_SELF => T_SELF ,
2450- T_STATIC => T_STATIC ,
2451- T_STRING => T_STRING ,
2452- T_TYPE_UNION => T_TYPE_UNION ,
2442+ T_ARRAY => T_ARRAY ,
2443+ T_CALLABLE => T_CALLABLE ,
2444+ T_COLON => T_COLON ,
2445+ T_NAMESPACE => T_NAMESPACE ,
2446+ T_NS_SEPARATOR => T_NS_SEPARATOR ,
2447+ T_NULL => T_NULL ,
2448+ T_NULLABLE => T_NULLABLE ,
2449+ T_PARENT => T_PARENT ,
2450+ T_SELF => T_SELF ,
2451+ T_STATIC => T_STATIC ,
2452+ T_STRING => T_STRING ,
2453+ T_TYPE_UNION => T_TYPE_UNION ,
2454+ T_TYPE_INTERSECTION => T_TYPE_INTERSECTION ,
24532455 ];
24542456
24552457 $ closer = $ this ->tokens [$ x ]['parenthesis_closer ' ];
@@ -2745,9 +2747,12 @@ protected function processAdditional()
27452747 }//end if
27462748
27472749 continue ;
2748- } else if ($ this ->tokens [$ i ]['code ' ] === T_BITWISE_OR ) {
2750+ } else if ($ this ->tokens [$ i ]['code ' ] === T_BITWISE_OR
2751+ || $ this ->tokens [$ i ]['code ' ] === T_BITWISE_AND
2752+ ) {
27492753 /*
27502754 Convert "|" to T_TYPE_UNION or leave as T_BITWISE_OR.
2755+ Convert "&" to T_TYPE_INTERSECTION or leave as T_BITWISE_AND.
27512756 */
27522757
27532758 $ allowed = [
@@ -2812,12 +2817,12 @@ protected function processAdditional()
28122817 }//end for
28132818
28142819 if ($ typeTokenCount === 0 || isset ($ suspectedType ) === false ) {
2815- // Definitely not a union type, move on.
2820+ // Definitely not a union or intersection type, move on.
28162821 continue ;
28172822 }
28182823
28192824 $ typeTokenCount = 0 ;
2820- $ unionOperators = [$ i ];
2825+ $ typeOperators = [$ i ];
28212826 $ confirmed = false ;
28222827
28232828 for ($ x = ($ i - 1 ); $ x >= 0 ; $ x --) {
@@ -2830,13 +2835,13 @@ protected function processAdditional()
28302835 continue ;
28312836 }
28322837
2833- // Union types can't use the nullable operator, but be tolerant to parse errors.
2838+ // Union and intersection types can't use the nullable operator, but be tolerant to parse errors.
28342839 if ($ typeTokenCount > 0 && $ this ->tokens [$ x ]['code ' ] === T_NULLABLE ) {
28352840 continue ;
28362841 }
28372842
2838- if ($ this ->tokens [$ x ]['code ' ] === T_BITWISE_OR ) {
2839- $ unionOperators [] = $ x ;
2843+ if ($ this ->tokens [$ x ]['code ' ] === T_BITWISE_OR || $ this -> tokens [ $ x ][ ' code ' ] === T_BITWISE_AND ) {
2844+ $ typeOperators [] = $ x ;
28402845 continue ;
28412846 }
28422847
@@ -2902,17 +2907,27 @@ protected function processAdditional()
29022907 }//end if
29032908
29042909 if ($ confirmed === false ) {
2905- // Not a union type after all, move on.
2910+ // Not a union or intersection type after all, move on.
29062911 continue ;
29072912 }
29082913
2909- foreach ($ unionOperators as $ x ) {
2910- $ this ->tokens [$ x ]['code ' ] = T_TYPE_UNION ;
2911- $ this ->tokens [$ x ]['type ' ] = 'T_TYPE_UNION ' ;
2914+ foreach ($ typeOperators as $ x ) {
2915+ if ($ this ->tokens [$ x ]['code ' ] === T_BITWISE_OR ) {
2916+ $ this ->tokens [$ x ]['code ' ] = T_TYPE_UNION ;
2917+ $ this ->tokens [$ x ]['type ' ] = 'T_TYPE_UNION ' ;
29122918
2913- if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
2914- $ line = $ this ->tokens [$ x ]['line ' ];
2915- echo "\t* token $ x on line $ line changed from T_BITWISE_OR to T_TYPE_UNION " .PHP_EOL ;
2919+ if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
2920+ $ line = $ this ->tokens [$ x ]['line ' ];
2921+ echo "\t* token $ x on line $ line changed from T_BITWISE_OR to T_TYPE_UNION " .PHP_EOL ;
2922+ }
2923+ } else {
2924+ $ this ->tokens [$ x ]['code ' ] = T_TYPE_INTERSECTION ;
2925+ $ this ->tokens [$ x ]['type ' ] = 'T_TYPE_INTERSECTION ' ;
2926+
2927+ if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
2928+ $ line = $ this ->tokens [$ x ]['line ' ];
2929+ echo "\t* token $ x on line $ line changed from T_BITWISE_AND to T_TYPE_INTERSECTION " .PHP_EOL ;
2930+ }
29162931 }
29172932 }
29182933
0 commit comments