Skip to content

Commit c644034

Browse files
committed
[code-quality] Improve InlineArrayReturnAssignRector to include implicit variable init and nested keys
1 parent 87b1e0d commit c644034

File tree

15 files changed

+260
-193
lines changed

15 files changed

+260
-193
lines changed

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,7 @@ parameters:
338338
-
339339
message: '#Offset float\|int\|string might not exist on string#'
340340
path: rules/Php70/EregToPcreTransformer.php
341+
342+
-
343+
identifier: symplify.noReference
344+
message: '#Use explicit return value over magic &reference#'
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
44

5-
final class KeepComments
5+
final class IncludeEmptyAssign
66
{
7-
public function getPerson()
7+
public function run($person2)
88
{
99
$person = [];
10-
// name
11-
$person['name'] = 'Timmy';
12-
// surname
10+
11+
$person[] = 'Timmy';
1312
$person['surname'] = 'Back';
1413

1514
return $person;
@@ -22,16 +21,11 @@ final class KeepComments
2221

2322
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
2423

25-
final class KeepComments
24+
final class IncludeEmptyAssign
2625
{
27-
public function getPerson()
26+
public function run($person2)
2827
{
29-
return [
30-
// name
31-
'name' => 'Timmy',
32-
// surname
33-
'surname' => 'Back',
34-
];
28+
return ['Timmy', 'surname' => 'Back'];
3529
}
3630
}
3731

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
4+
5+
final class IncludeNestedVariable
6+
{
7+
public function create()
8+
{
9+
$items[0]['name'] = 'John';
10+
$items[0]['surname'] = 'Doe';
11+
$items[0]['age'] = 50;
12+
return $items;
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
21+
22+
final class IncludeNestedVariable
23+
{
24+
public function create()
25+
{
26+
return [['name' => 'John', 'surname' => 'Doe', 'age' => 50]];
27+
}
28+
}
29+
30+
?>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
4+
5+
final class NestedArrayWithMultipleKeys
6+
{
7+
public function run()
8+
{
9+
$arr = [];
10+
$arr['foo']['bar']['baz'] = ['a' => 1, 'b' => 2];
11+
12+
return $arr;
13+
}
14+
}
15+
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
22+
23+
final class NestedArrayWithMultipleKeys
24+
{
25+
public function run()
26+
{
27+
return ['foo' => ['bar' => ['baz' => ['a' => 1, 'b' => 2]]]];
28+
}
29+
}
30+
31+
32+
?>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
4+
5+
final class SkipDifferentVariable
6+
{
7+
public function create()
8+
{
9+
$item['sub_key'] = 500;
10+
11+
$items['name'] = 'John';
12+
$items['surname'] = 'Doe';
13+
$items['age'] = $item;
14+
15+
return $items;
16+
}
17+
}

rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/skip_empty_assign.php.inc

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
4+
5+
final class SkipMultipleReturns
6+
{
7+
public function create()
8+
{
9+
$items['name'] = 'John';
10+
$items['surname'] = 'Doe';
11+
$items['age'] = 50;
12+
13+
if (mt_rand(1, 2)) {
14+
return [];
15+
}
16+
17+
return $items;
18+
}
19+
}

rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/skip_nested_array.php.inc

Lines changed: 0 additions & 15 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/skip_reuse_variable_in_assign_expr.php.inc

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
4+
5+
class SomeClass
6+
{
7+
public function create()
8+
{
9+
$items['name'] = 'John';
10+
$items['surname'] = 'Doe';
11+
$items['age'] = 50;
12+
return $items;
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
21+
22+
class SomeClass
23+
{
24+
public function create()
25+
{
26+
return ['name' => 'John', 'surname' => 'Doe', 'age' => 50];
27+
}
28+
}
29+
30+
?>

0 commit comments

Comments
 (0)