@@ -462,6 +462,7 @@ class PHP extends Tokenizer
462
462
T_OPEN_SHORT_ARRAY => 1 ,
463
463
T_CLOSE_SHORT_ARRAY => 1 ,
464
464
T_TYPE_UNION => 1 ,
465
+ T_TYPE_INTERSECTION => 1 ,
465
466
];
466
467
467
468
/**
@@ -2438,18 +2439,19 @@ protected function processAdditional()
2438
2439
if (isset ($ this ->tokens [$ x ]) === true && $ this ->tokens [$ x ]['code ' ] === T_OPEN_PARENTHESIS ) {
2439
2440
$ ignore = Util \Tokens::$ emptyTokens ;
2440
2441
$ 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 ,
2453
2455
];
2454
2456
2455
2457
$ closer = $ this ->tokens [$ x ]['parenthesis_closer ' ];
@@ -2745,9 +2747,12 @@ protected function processAdditional()
2745
2747
}//end if
2746
2748
2747
2749
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
+ ) {
2749
2753
/*
2750
2754
Convert "|" to T_TYPE_UNION or leave as T_BITWISE_OR.
2755
+ Convert "&" to T_TYPE_INTERSECTION or leave as T_BITWISE_AND.
2751
2756
*/
2752
2757
2753
2758
$ allowed = [
@@ -2812,12 +2817,12 @@ protected function processAdditional()
2812
2817
}//end for
2813
2818
2814
2819
if ($ typeTokenCount === 0 || isset ($ suspectedType ) === false ) {
2815
- // Definitely not a union type, move on.
2820
+ // Definitely not a union or intersection type, move on.
2816
2821
continue ;
2817
2822
}
2818
2823
2819
2824
$ typeTokenCount = 0 ;
2820
- $ unionOperators = [$ i ];
2825
+ $ typeOperators = [$ i ];
2821
2826
$ confirmed = false ;
2822
2827
2823
2828
for ($ x = ($ i - 1 ); $ x >= 0 ; $ x --) {
@@ -2830,13 +2835,13 @@ protected function processAdditional()
2830
2835
continue ;
2831
2836
}
2832
2837
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.
2834
2839
if ($ typeTokenCount > 0 && $ this ->tokens [$ x ]['code ' ] === T_NULLABLE ) {
2835
2840
continue ;
2836
2841
}
2837
2842
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 ;
2840
2845
continue ;
2841
2846
}
2842
2847
@@ -2902,17 +2907,27 @@ protected function processAdditional()
2902
2907
}//end if
2903
2908
2904
2909
if ($ confirmed === false ) {
2905
- // Not a union type after all, move on.
2910
+ // Not a union or intersection type after all, move on.
2906
2911
continue ;
2907
2912
}
2908
2913
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 ' ;
2912
2918
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
+ }
2916
2931
}
2917
2932
}
2918
2933
0 commit comments