@@ -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 /**
@@ -2406,18 +2407,19 @@ protected function processAdditional()
24062407 if (isset ($ this ->tokens [$ x ]) === true && $ this ->tokens [$ x ]['code ' ] === T_OPEN_PARENTHESIS ) {
24072408 $ ignore = Util \Tokens::$ emptyTokens ;
24082409 $ ignore += [
2409- T_ARRAY => T_ARRAY ,
2410- T_CALLABLE => T_CALLABLE ,
2411- T_COLON => T_COLON ,
2412- T_NAMESPACE => T_NAMESPACE ,
2413- T_NS_SEPARATOR => T_NS_SEPARATOR ,
2414- T_NULL => T_NULL ,
2415- T_NULLABLE => T_NULLABLE ,
2416- T_PARENT => T_PARENT ,
2417- T_SELF => T_SELF ,
2418- T_STATIC => T_STATIC ,
2419- T_STRING => T_STRING ,
2420- T_TYPE_UNION => T_TYPE_UNION ,
2410+ T_ARRAY => T_ARRAY ,
2411+ T_CALLABLE => T_CALLABLE ,
2412+ T_COLON => T_COLON ,
2413+ T_NAMESPACE => T_NAMESPACE ,
2414+ T_NS_SEPARATOR => T_NS_SEPARATOR ,
2415+ T_NULL => T_NULL ,
2416+ T_NULLABLE => T_NULLABLE ,
2417+ T_PARENT => T_PARENT ,
2418+ T_SELF => T_SELF ,
2419+ T_STATIC => T_STATIC ,
2420+ T_STRING => T_STRING ,
2421+ T_TYPE_UNION => T_TYPE_UNION ,
2422+ T_TYPE_INTERSECTION => T_TYPE_INTERSECTION ,
24212423 ];
24222424
24232425 $ closer = $ this ->tokens [$ x ]['parenthesis_closer ' ];
@@ -2713,9 +2715,12 @@ protected function processAdditional()
27132715 }//end if
27142716
27152717 continue ;
2716- } else if ($ this ->tokens [$ i ]['code ' ] === T_BITWISE_OR ) {
2718+ } else if ($ this ->tokens [$ i ]['code ' ] === T_BITWISE_OR
2719+ || $ this ->tokens [$ i ]['code ' ] === T_BITWISE_AND
2720+ ) {
27172721 /*
27182722 Convert "|" to T_TYPE_UNION or leave as T_BITWISE_OR.
2723+ Convert "&" to T_TYPE_INTERSECTION or leave as T_BITWISE_AND.
27192724 */
27202725
27212726 $ allowed = [
@@ -2780,12 +2785,12 @@ protected function processAdditional()
27802785 }//end for
27812786
27822787 if ($ typeTokenCount === 0 || isset ($ suspectedType ) === false ) {
2783- // Definitely not a union type, move on.
2788+ // Definitely not a union or intersection type, move on.
27842789 continue ;
27852790 }
27862791
27872792 $ typeTokenCount = 0 ;
2788- $ unionOperators = [$ i ];
2793+ $ typeOperators = [$ i ];
27892794 $ confirmed = false ;
27902795
27912796 for ($ x = ($ i - 1 ); $ x >= 0 ; $ x --) {
@@ -2798,13 +2803,13 @@ protected function processAdditional()
27982803 continue ;
27992804 }
28002805
2801- // Union types can't use the nullable operator, but be tolerant to parse errors.
2806+ // Union and intersection types can't use the nullable operator, but be tolerant to parse errors.
28022807 if ($ typeTokenCount > 0 && $ this ->tokens [$ x ]['code ' ] === T_NULLABLE ) {
28032808 continue ;
28042809 }
28052810
2806- if ($ this ->tokens [$ x ]['code ' ] === T_BITWISE_OR ) {
2807- $ unionOperators [] = $ x ;
2811+ if ($ this ->tokens [$ x ]['code ' ] === T_BITWISE_OR || $ this -> tokens [ $ x ][ ' code ' ] === T_BITWISE_AND ) {
2812+ $ typeOperators [] = $ x ;
28082813 continue ;
28092814 }
28102815
@@ -2870,17 +2875,27 @@ protected function processAdditional()
28702875 }//end if
28712876
28722877 if ($ confirmed === false ) {
2873- // Not a union type after all, move on.
2878+ // Not a union or intersection type after all, move on.
28742879 continue ;
28752880 }
28762881
2877- foreach ($ unionOperators as $ x ) {
2878- $ this ->tokens [$ x ]['code ' ] = T_TYPE_UNION ;
2879- $ this ->tokens [$ x ]['type ' ] = 'T_TYPE_UNION ' ;
2882+ foreach ($ typeOperators as $ x ) {
2883+ if ($ this ->tokens [$ x ]['code ' ] === T_BITWISE_OR ) {
2884+ $ this ->tokens [$ x ]['code ' ] = T_TYPE_UNION ;
2885+ $ this ->tokens [$ x ]['type ' ] = 'T_TYPE_UNION ' ;
28802886
2881- if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
2882- $ line = $ this ->tokens [$ x ]['line ' ];
2883- echo "\t* token $ x on line $ line changed from T_BITWISE_OR to T_TYPE_UNION " .PHP_EOL ;
2887+ if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
2888+ $ line = $ this ->tokens [$ x ]['line ' ];
2889+ echo "\t* token $ x on line $ line changed from T_BITWISE_OR to T_TYPE_UNION " .PHP_EOL ;
2890+ }
2891+ } else {
2892+ $ this ->tokens [$ x ]['code ' ] = T_TYPE_INTERSECTION ;
2893+ $ this ->tokens [$ x ]['type ' ] = 'T_TYPE_INTERSECTION ' ;
2894+
2895+ if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
2896+ $ line = $ this ->tokens [$ x ]['line ' ];
2897+ echo "\t* token $ x on line $ line changed from T_BITWISE_AND to T_TYPE_INTERSECTION " .PHP_EOL ;
2898+ }
28842899 }
28852900 }
28862901
@@ -2938,6 +2953,7 @@ protected function processAdditional()
29382953 T_NAME_RELATIVE => T_NAME_RELATIVE ,
29392954 T_NAME_QUALIFIED => T_NAME_QUALIFIED ,
29402955 T_TYPE_UNION => T_TYPE_UNION ,
2956+ T_TYPE_INTERSECTION => T_TYPE_INTERSECTION ,
29412957 T_BITWISE_OR => T_BITWISE_OR ,
29422958 T_BITWISE_AND => T_BITWISE_AND ,
29432959 T_ARRAY => T_ARRAY ,
0 commit comments