@@ -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
/**
@@ -2406,18 +2407,19 @@ protected function processAdditional()
2406
2407
if (isset ($ this ->tokens [$ x ]) === true && $ this ->tokens [$ x ]['code ' ] === T_OPEN_PARENTHESIS ) {
2407
2408
$ ignore = Util \Tokens::$ emptyTokens ;
2408
2409
$ 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 ,
2421
2423
];
2422
2424
2423
2425
$ closer = $ this ->tokens [$ x ]['parenthesis_closer ' ];
@@ -2713,9 +2715,12 @@ protected function processAdditional()
2713
2715
}//end if
2714
2716
2715
2717
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
+ ) {
2717
2721
/*
2718
2722
Convert "|" to T_TYPE_UNION or leave as T_BITWISE_OR.
2723
+ Convert "&" to T_TYPE_INTERSECTION or leave as T_BITWISE_AND.
2719
2724
*/
2720
2725
2721
2726
$ allowed = [
@@ -2780,12 +2785,12 @@ protected function processAdditional()
2780
2785
}//end for
2781
2786
2782
2787
if ($ typeTokenCount === 0 || isset ($ suspectedType ) === false ) {
2783
- // Definitely not a union type, move on.
2788
+ // Definitely not a union or intersection type, move on.
2784
2789
continue ;
2785
2790
}
2786
2791
2787
2792
$ typeTokenCount = 0 ;
2788
- $ unionOperators = [$ i ];
2793
+ $ typeOperators = [$ i ];
2789
2794
$ confirmed = false ;
2790
2795
2791
2796
for ($ x = ($ i - 1 ); $ x >= 0 ; $ x --) {
@@ -2798,13 +2803,13 @@ protected function processAdditional()
2798
2803
continue ;
2799
2804
}
2800
2805
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.
2802
2807
if ($ typeTokenCount > 0 && $ this ->tokens [$ x ]['code ' ] === T_NULLABLE ) {
2803
2808
continue ;
2804
2809
}
2805
2810
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 ;
2808
2813
continue ;
2809
2814
}
2810
2815
@@ -2870,17 +2875,27 @@ protected function processAdditional()
2870
2875
}//end if
2871
2876
2872
2877
if ($ confirmed === false ) {
2873
- // Not a union type after all, move on.
2878
+ // Not a union or intersection type after all, move on.
2874
2879
continue ;
2875
2880
}
2876
2881
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 ' ;
2880
2886
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
+ }
2884
2899
}
2885
2900
}
2886
2901
@@ -2938,6 +2953,7 @@ protected function processAdditional()
2938
2953
T_NAME_RELATIVE => T_NAME_RELATIVE ,
2939
2954
T_NAME_QUALIFIED => T_NAME_QUALIFIED ,
2940
2955
T_TYPE_UNION => T_TYPE_UNION ,
2956
+ T_TYPE_INTERSECTION => T_TYPE_INTERSECTION ,
2941
2957
T_BITWISE_OR => T_BITWISE_OR ,
2942
2958
T_BITWISE_AND => T_BITWISE_AND ,
2943
2959
T_ARRAY => T_ARRAY ,
0 commit comments