Skip to content

Commit 50dd0cf

Browse files
authored
Adds the ArrayAccess to Method Call set (#218)
* Adds the ArrayAccess set * fixes * More fixes
1 parent ea914dc commit 50dd0cf

File tree

6 files changed

+99
-1
lines changed

6 files changed

+99
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ return RectorConfig::configure()
4545
| [LaravelSetList::LARAVEL_IF_HELPERS](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-if-helpers.php) | Replaces `abort()`, `report()`, `throw` statements inside conditions with `abort_if()`, `report_if()`, `throw_if()` function calls.<br/>https://laravel.com/docs/11.x/helpers#method-abort-if |
4646
| [LaravelSetList::LARAVEL_LEGACY_FACTORIES_TO_CLASSES](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-legacy-factories-to-classes.php) | Migrates Eloquent legacy model factories (with closures) into class based factories.<br/>https://laravel.com/docs/8.x/releases#model-factory-classes |
4747
| [LaravelSetList::LARAVEL_STATIC_TO_INJECTION](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-static-to-injection.php) | Replaces Laravel's Facades with Dependency Injection.<br/>https://tomasvotruba.com/blog/2019/03/04/how-to-turn-laravel-from-static-to-dependency-injection-in-one-day/<br/>https://laravel.com/docs/11.x/facades#facades-vs-dependency-injection |
48-
48+
| [LaravelSetList::LARAVEL_ARRAYACCESS_TO_METHOD_CALL](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-arrayaccess-to-method-call.php) | Converts uses of things like `$app['config']` to `$app->make('config')`. |
4949

5050
## Contributors
5151

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
use PHPStan\Type\ObjectType;
5+
use Rector\Config\RectorConfig;
6+
use Rector\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector;
7+
use Rector\Transform\ValueObject\ArrayDimFetchToMethodCall;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->import(__DIR__ . '/../config.php');
11+
12+
$rectorConfig
13+
->ruleWithConfiguration(
14+
ArrayDimFetchToMethodCallRector::class,
15+
[
16+
new ArrayDimFetchToMethodCall(
17+
new ObjectType('Illuminate\Foundation\Application'),
18+
'make',
19+
),
20+
new ArrayDimFetchToMethodCall(
21+
new ObjectType('Illuminate\Config\Repository'),
22+
'get',
23+
),
24+
],
25+
);
26+
};

src/Set/LaravelSetList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,9 @@ final class LaravelSetList implements SetListInterface
127127
* @var string
128128
*/
129129
final public const LARAVEL_IF_HELPERS = __DIR__ . '/../../config/sets/laravel-if-helpers.php';
130+
131+
/**
132+
* @var string
133+
*/
134+
final public const LARAVEL_ARRAYACCESS_TO_METHOD_CALL = __DIR__ . '/../../config/sets/laravel-arrayaccess-to-method-call.php';
130135
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RectorLaravel\Tests\Sets\ArrayAccessToMethodCall;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ArrayAccessToMethodCallTest extends AbstractRectorTestCase
12+
{
13+
public static function provideData(): Iterator
14+
{
15+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
16+
}
17+
18+
/**
19+
* @test
20+
*/
21+
#[DataProvider('provideData')]
22+
public function test(string $filePath): void
23+
{
24+
$this->doTestFile($filePath);
25+
}
26+
27+
public function provideConfigFilePath(): string
28+
{
29+
return __DIR__ . '/config/configured_rule.php';
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Sets\ArrayAccessToMethodCall;
4+
5+
$app = new \Illuminate\Foundation\Application();
6+
7+
$service = $app[\Illuminate\Contracts\Events\Dispatcher::class];
8+
9+
$config = new \Illuminate\Config\Repository();
10+
11+
$name = $config['app.name'];
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace RectorLaravel\Tests\Sets\ArrayAccessToMethodCall;
18+
19+
$app = new \Illuminate\Foundation\Application();
20+
21+
$service = $app->make(\Illuminate\Contracts\Events\Dispatcher::class);
22+
23+
$config = new \Illuminate\Config\Repository();
24+
25+
$name = $config->get('app.name');
26+
27+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return static function (RectorConfig $rectorConfig): void {
8+
$rectorConfig->import(__DIR__ . '/../../../../config/sets/laravel-arrayaccess-to-method-call.php');
9+
};

0 commit comments

Comments
 (0)