1515use PhpParser \Node \Stmt ;
1616use PhpParser \Node \Stmt \Expression ;
1717use PhpParser \Node \Stmt \Unset_ ;
18- use PhpParser \NodeVisitor ;
1918use PHPStan \Type \ObjectType ;
2019use Rector \Contract \Rector \ConfigurableRectorInterface ;
2120use Rector \NodeTypeResolver \Node \AttributeKey ;
2221use Rector \Rector \AbstractRector ;
22+ use Rector \Transform \Enum \MagicPropertyHandler ;
2323use Rector \Transform \ValueObject \ArrayDimFetchToMethodCall ;
2424use Symplify \RuleDocGenerator \ValueObject \CodeSample \ConfiguredCodeSample ;
2525use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
@@ -65,9 +65,9 @@ public function getNodeTypes(): array
6565
6666 /**
6767 * @param ArrayDimFetch|Assign|Isset_|Unset_ $node
68- * @return ($node is Unset_ ? Stmt[]|int : ($node is Isset_ ? Expr|int : MethodCall|int |null))
68+ * @return ($node is Unset_ ? Stmt[] : ($node is Isset_ ? Expr : MethodCall|null))
6969 */
70- public function refactor (Node $ node ): array |Expr |null | int
70+ public function refactor (Node $ node ): array |Expr |null
7171 {
7272 if ($ node instanceof Unset_) {
7373 return $ this ->handleUnset ($ node );
@@ -82,7 +82,7 @@ public function refactor(Node $node): array|Expr|null|int
8282 return null ;
8383 }
8484
85- return $ this ->createExplicitMethodCall ($ node ->var , ' set ' , $ node ->expr );
85+ return $ this ->createExplicitMethodCall ($ node ->var , MagicPropertyHandler:: SET , $ node ->expr );
8686 }
8787
8888 // is part of assign, skip
@@ -94,7 +94,16 @@ public function refactor(Node $node): array|Expr|null|int
9494 return null ;
9595 }
9696
97- return $ this ->createExplicitMethodCall ($ node , 'get ' );
97+ // should be skipped as handled above
98+ if ($ node ->getAttribute (AttributeKey::IS_UNSET_VAR )) {
99+ return null ;
100+ }
101+
102+ if ($ node ->getAttribute (AttributeKey::IS_ISSET_VAR )) {
103+ return null ;
104+ }
105+
106+ return $ this ->createExplicitMethodCall ($ node , MagicPropertyHandler::GET );
98107 }
99108
100109 public function configure (array $ configuration ): void
@@ -104,7 +113,7 @@ public function configure(array $configuration): void
104113 $ this ->arrayDimFetchToMethodCalls = $ configuration ;
105114 }
106115
107- private function handleIsset (Isset_ $ isset ): Expr |int | null
116+ private function handleIsset (Isset_ $ isset ): Expr |null
108117 {
109118 $ issets = [];
110119 $ exprs = [];
@@ -123,7 +132,8 @@ private function handleIsset(Isset_ $isset): Expr|int|null
123132 }
124133
125134 if ($ exprs === []) {
126- return NodeVisitor::DONT_TRAVERSE_CHILDREN ;
135+ // nothing to handle
136+ return null ;
127137 }
128138
129139 if ($ issets !== []) {
@@ -141,9 +151,9 @@ private function handleIsset(Isset_ $isset): Expr|int|null
141151 }
142152
143153 /**
144- * @return Stmt[]|int
154+ * @return Stmt[]|null
145155 */
146- private function handleUnset (Unset_ $ unset ): array | int
156+ private function handleUnset (Unset_ $ unset ): ? array
147157 {
148158 $ unsets = [];
149159 $ stmts = [];
@@ -161,8 +171,9 @@ private function handleUnset(Unset_ $unset): array|int
161171 $ unsets [] = $ var ;
162172 }
163173
174+ // nothing to change
164175 if ($ stmts === []) {
165- return NodeVisitor:: DONT_TRAVERSE_CHILDREN ;
176+ return null ;
166177 }
167178
168179 if ($ unsets !== []) {
@@ -174,11 +185,11 @@ private function handleUnset(Unset_ $unset): array|int
174185 }
175186
176187 /**
177- * @param 'get'|'set'|'exists'|'unset' $action
188+ * @param MagicPropertyHandler::* $magicPropertyHandler
178189 */
179190 private function createExplicitMethodCall (
180191 ArrayDimFetch $ arrayDimFetch ,
181- string $ action ,
192+ string $ magicPropertyHandler ,
182193 ?Expr $ expr = null
183194 ): ?MethodCall {
184195 if (! $ arrayDimFetch ->dim instanceof Node) {
@@ -190,11 +201,11 @@ private function createExplicitMethodCall(
190201 continue ;
191202 }
192203
193- $ method = match ($ action ) {
194- ' get ' => $ arrayDimFetchToMethodCall ->getMethod (),
195- ' set ' => $ arrayDimFetchToMethodCall ->getSetMethod (),
196- ' exists ' => $ arrayDimFetchToMethodCall ->getExistsMethod (),
197- ' unset ' => $ arrayDimFetchToMethodCall ->getUnsetMethod (),
204+ $ method = match ($ magicPropertyHandler ) {
205+ MagicPropertyHandler:: GET => $ arrayDimFetchToMethodCall ->getMethod (),
206+ MagicPropertyHandler:: SET => $ arrayDimFetchToMethodCall ->getSetMethod (),
207+ MagicPropertyHandler:: ISSET_ => $ arrayDimFetchToMethodCall ->getExistsMethod (),
208+ MagicPropertyHandler:: UNSET => $ arrayDimFetchToMethodCall ->getUnsetMethod (),
198209 };
199210
200211 if ($ method === null ) {
0 commit comments