88use PhpParser \Node \Arg ;
99use PhpParser \Node \Expr ;
1010use PhpParser \Node \Expr \ArrayDimFetch ;
11+ use PhpParser \Node \Expr \Assign ;
12+ use PhpParser \Node \Expr \AssignOp ;
1113use PhpParser \Node \Expr \BinaryOp \Coalesce ;
14+ use PhpParser \Node \Expr \Empty_ ;
15+ use PhpParser \Node \Expr \Isset_ ;
1216use PhpParser \Node \Expr \StaticCall ;
1317use PhpParser \Node \Expr \Throw_ ;
1418use PhpParser \Node \Name \FullyQualified ;
@@ -31,21 +35,27 @@ final class ArrayToArrGetRector extends AbstractRector
3135 public function getRuleDefinition (): RuleDefinition
3236 {
3337 return new RuleDefinition (
34- 'Convert array access to Arr::get() method call, skips null coalesce with throw expressions ' ,
38+ 'Convert array access to Arr::get() method call, skips isset/empty checks, assignments, and null coalesce with throw expressions ' ,
3539 [new CodeSample (
3640 <<<'CODE_SAMPLE'
3741$array['key'];
3842$array['nested']['key'];
3943$array['key'] ?? 'default';
4044$array['nested']['key'] ?? 'default';
4145$array['key'] ?? throw new Exception('Required');
46+ isset($array['key']);
47+ empty($array['key']);
48+ $array['key'] = 'value';
4249CODE_SAMPLE,
4350 <<<'CODE_SAMPLE'
4451\Illuminate\Support\Arr::get($array, 'key');
4552\Illuminate\Support\Arr::get($array, 'nested.key');
4653\Illuminate\Support\Arr::get($array, 'key', 'default');
4754\Illuminate\Support\Arr::get($array, 'nested.key', 'default');
4855$array['key'] ?? throw new Exception('Required');
56+ isset($array['key']);
57+ empty($array['key']);
58+ $array['key'] = 'value';
4959CODE_SAMPLE
5060 )]
5161 );
@@ -56,14 +66,33 @@ public function getRuleDefinition(): RuleDefinition
5666 */
5767 public function getNodeTypes (): array
5868 {
59- return [ArrayDimFetch::class, Coalesce::class];
69+ return [ArrayDimFetch::class, Coalesce::class, Isset_::class, Empty_::class, Assign::class, AssignOp::class ];
6070 }
6171
6272 /**
63- * @param ArrayDimFetch|Coalesce $node
73+ * @param ArrayDimFetch|Coalesce|Isset_|Empty_|Assign|AssignOp $node
6474 */
6575 public function refactor (Node $ node ): ?StaticCall
6676 {
77+ if ($ node instanceof Isset_) {
78+ $ this ->markIssetArrayDimFetchesAsProcessed ($ node );
79+ return null ;
80+ }
81+
82+ if ($ node instanceof Empty_) {
83+ if ($ node ->expr instanceof ArrayDimFetch) {
84+ $ this ->markArrayDimFetchAsProcessed ($ node ->expr );
85+ }
86+ return null ;
87+ }
88+
89+ if ($ node instanceof Assign || $ node instanceof AssignOp) {
90+ if ($ node ->var instanceof ArrayDimFetch) {
91+ $ this ->markArrayDimFetchAsProcessed ($ node ->var );
92+ }
93+ return null ;
94+ }
95+
6796 if ($ node instanceof Coalesce) {
6897 $ result = $ this ->refactorCoalesce ($ node );
6998 if ($ result instanceof StaticCall && $ node ->left instanceof ArrayDimFetch) {
@@ -213,4 +242,13 @@ private function markArrayDimFetchAsProcessed(ArrayDimFetch $arrayDimFetch): voi
213242 $ current = $ current ->var ;
214243 }
215244 }
245+
246+ private function markIssetArrayDimFetchesAsProcessed (Isset_ $ isset ): void
247+ {
248+ foreach ($ isset ->vars as $ var ) {
249+ if ($ var instanceof ArrayDimFetch) {
250+ $ this ->markArrayDimFetchAsProcessed ($ var );
251+ }
252+ }
253+ }
216254}
0 commit comments