File tree Expand file tree Collapse file tree
rules/CodeQuality/Rector/ClassMethod Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55namespace Rector \CodeQuality \Rector \ClassMethod ;
66
77use PhpParser \Node ;
8+ use PhpParser \Node \ComplexType ;
89use PhpParser \Node \Expr ;
910use PhpParser \Node \Expr \ConstFetch ;
11+ use PhpParser \Node \Identifier ;
12+ use PhpParser \Node \IntersectionType ;
1013use PhpParser \Node \Name ;
1114use PhpParser \Node \NullableType ;
1215use PhpParser \Node \Param ;
@@ -77,7 +80,7 @@ public function refactor(Node $node): ClassMethod|Function_|null
7780
7881 $ previousParam = $ node ->params [$ key - 1 ] ?? null ;
7982 if ($ previousParam instanceof Param && $ previousParam ->default instanceof Expr) {
80- $ hasChanged = false ;
83+ $ hasChanged = true ;
8184
8285 $ param ->default = new ConstFetch (new Name ('null ' ));
8386 $ paramType = $ param ->type ;
@@ -90,10 +93,18 @@ public function refactor(Node $node): ClassMethod|Function_|null
9093 continue ;
9194 }
9295
93- if ($ paramType instanceof UnionType) {
94- $ paramType ->types [] = new ConstFetch (new Name ('null ' ));
95- $ paramType ->types = array_unique ($ paramType ->types , SORT_REGULAR );
96+ if ($ paramType instanceof UnionType || $ paramType instanceof IntersectionType) {
97+ foreach ($ paramType ->types as $ unionedType ) {
98+ if ($ unionedType instanceof Identifier && $ this ->isName ($ unionedType , 'null ' )) {
99+ continue 2 ;
100+ }
101+ }
96102
103+ $ paramType ->types [] = new Identifier ('null ' );
104+ continue ;
105+ }
106+
107+ if ($ paramType instanceof ComplexType) {
97108 continue ;
98109 }
99110
You can’t perform that action at this time.
0 commit comments