Skip to content

Commit e88d9f4

Browse files
committed
Rector
1 parent 4428667 commit e88d9f4

16 files changed

+70
-57
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"illuminate/contracts": "^11.0|^12.0"
2424
},
2525
"require-dev": {
26-
"laravel/pint": "^1.0",
26+
"laravel/pint": "^1.21",
2727
"nunomaduro/collision": "^8.1|^9.0",
2828
"orchestra/testbench": "^9.1|^10.0",
2929
"phpunit/phpunit": "^10.5|^11.0"

rector.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\ValueObject\PhpVersion;
7+
8+
return RectorConfig::configure()
9+
->withPaths([
10+
__DIR__.'/src',
11+
__DIR__.'/tests',
12+
])
13+
->withPreparedSets(
14+
deadCode: true,
15+
codeQuality: true,
16+
typeDeclarations: true,
17+
privatization: true,
18+
earlyReturn: true,
19+
)
20+
->withAttributesSets()
21+
->withPhpSets()
22+
->withPhpVersion(PhpVersion::PHP_82);

src/Commands/Concerns/InstallsForImportmap.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ protected function publishJsFilesForImportmaps()
4242
protected function registerImportmapPins()
4343
{
4444
$dependencies = collect($this->jsPackages())
45-
->map(fn ($version, $package) => "{$package}@{$version}")
45+
->map(fn ($version, $package): string => "{$package}@{$version}")
4646
->values()
4747
->all();
4848

4949
Process::forever()->run(array_merge([
5050
$this->phpBinary(),
5151
'artisan',
5252
'importmap:pin',
53-
], $dependencies), function ($_type, $output) {
53+
], $dependencies), function ($_type, $output): void {
5454
$this->output->write($output);
5555
});
5656

@@ -61,7 +61,7 @@ protected function registerImportmapPins()
6161
'vendor:publish',
6262
'--tag',
6363
'stimulus-laravel-assets',
64-
], function ($_type, $output) {
64+
], function ($_type, $output): void {
6565
$this->output->write($output);
6666
});
6767

src/Commands/Concerns/InstallsForNode.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ protected function publishJsFilesForNode()
7474

7575
protected function updateNpmPackagesForNode()
7676
{
77-
$this->updateNodePackages(function ($packages) {
78-
return array_merge(
79-
$packages,
80-
$this->jsPackages(),
81-
);
82-
});
77+
$this->updateNodePackages(fn($packages): array => array_merge(
78+
$packages,
79+
$this->jsPackages(),
80+
));
8381
}
8482
}

src/Commands/InstallCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ protected function runCommands($commands)
6868
}
6969
}
7070

71-
$process->run(function ($type, $line) {
71+
$process->run(function ($type, string $line): void {
7272
$this->output->write(' '.$line);
7373
});
7474
}
7575

76-
protected function phpBinary()
76+
protected function phpBinary(): string
7777
{
7878
return (new PhpExecutableFinder)->find(false) ?: 'php';
7979
}

src/Commands/MakeCommand.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@ public function handle(StimulusGenerator $generator): int
1717
{
1818
$this->components->info($this->option('bridge') ? 'Making a Stimulus Bridge Component' : 'Making Stimulus Controller');
1919

20-
$this->components->task('creating file', function () use ($generator) {
20+
$this->components->task('creating file', function () use ($generator): true {
2121
$generator->create($this->argument('name'), bridge: $this->option('bridge'));
2222

2323
return true;
2424
});
2525

2626
if (! File::exists(base_path('routes/importmap.php'))) {
27-
$this->components->task('regenerating manifest', function () {
28-
return $this->callSilently(ManifestCommand::class);
29-
});
27+
$this->components->task('regenerating manifest', fn() => $this->callSilently(ManifestCommand::class));
3028

3129
if (file_exists(base_path('pnpm-lock.yaml'))) {
3230
Process::forever()->path(base_path())->run(['pnpm', 'run', 'build']);

src/Commands/ManifestCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class ManifestCommand extends Command
1212

1313
public $description = 'Updates the manifest based on the existing Stimulus controllers.';
1414

15-
public function handle(Manifest $generator)
15+
public function handle(Manifest $generator): int
1616
{
1717
$this->components->info('Regenerating Manifest');
1818

19-
$this->components->task('regenerating manifest', function () use ($generator) {
19+
$this->components->task('regenerating manifest', function () use ($generator): true {
2020
$manifest = $generator->generateFrom(config('stimulus-laravel.controllers_path'))->join(PHP_EOL);
2121
$manifestFile = resource_path('js/controllers/index.js');
2222

src/Features.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Features
66
{
7-
public static function directives()
7+
public static function directives(): string
88
{
99
return 'directives';
1010
}

src/Manifest.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class Manifest
1212
public function generateFrom(string $controllersPath): Collection
1313
{
1414
return collect(File::allFiles($controllersPath))
15-
->filter(fn (SplFileInfo $file) => str_contains($file->getFilename(), '_controller'))
15+
->filter(fn (SplFileInfo $file): bool => str_contains($file->getFilename(), '_controller'))
1616
->values()
17-
->map(function (SplFileInfo $file) use ($controllersPath) {
17+
->map(function (SplFileInfo $file) use ($controllersPath): string {
1818
$controllerPath = $this->relativePathFrom($file->getRealPath(), $controllersPath);
1919
$modulePath = Str::of($controllerPath)->before('.')->replace(DIRECTORY_SEPARATOR, '/')->toString();
2020
$controllerClassName = Str::of($modulePath)
@@ -23,9 +23,7 @@ public function generateFrom(string $controllersPath): Collection
2323
->join('__');
2424
$tagName = Str::of($modulePath)->before('_controller')->replace('_', '-')->replace('/', '--')->toString();
2525

26-
$join = function ($paths) {
27-
return implode('/', $paths);
28-
};
26+
$join = (fn($paths): string => implode('/', $paths));
2927

3028
return <<<JS
3129
@@ -35,7 +33,7 @@ public function generateFrom(string $controllersPath): Collection
3533
});
3634
}
3735

38-
private function relativePathFrom(string $controllerPath, string $basePath)
36+
private function relativePathFrom(string $controllerPath, string $basePath): string
3937
{
4038
return trim(str_replace($basePath, '', $controllerPath), DIRECTORY_SEPARATOR);
4139
}

src/StimulusLaravel.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace HotwiredLaravel\StimulusLaravel;
44

55
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Collection;
67

78
class StimulusLaravel
89
{
910
public function controller($controllers)
1011
{
1112
$controllers = collect(Arr::wrap($controllers));
1213

13-
return $controllers->map(function ($configs, $controller) {
14+
return $controllers->map(function ($configs, $controller): array {
1415
if (is_numeric($controller)) {
1516
$controller = $configs;
1617
$configs = [];
@@ -22,19 +23,19 @@ public function controller($controllers)
2223
'value' => $configs['value'] ?? null,
2324
'class' => $configs['class'] ?? null,
2425
];
25-
})->reduce(function ($acc, $configs) {
26+
})->reduce(function ($acc, array $configs) {
2627
$acc['data-controller'] = array_merge($acc['data-controller'] ?? [], [$configs['controller']]);
2728

2829
foreach (['target', 'value', 'class'] as $attribute) {
2930
if ($configs[$attribute]) {
3031
foreach ($configs[$attribute] as $key => $val) {
31-
$acc['data-'.$configs['controller'].'-'.$key.'-'.$attribute] = $val;
32+
$acc['data-' . $configs['controller'] . '-' . $key . '-' . $attribute] = $val;
3233
}
3334
}
3435
}
3536

3637
return $acc;
37-
}, collect())->map(function ($value, $attr) {
38+
}, collect())->map(function ($value, $attr): string {
3839
$attr = e($attr);
3940
$controllers = e(is_array($value) ? implode(' ', $value) : $value);
4041

@@ -46,11 +47,11 @@ public function target($targets)
4647
{
4748
$targets = collect(Arr::wrap($targets));
4849

49-
return $targets->reduce(function ($acc, $targetName, $controller) {
50-
$acc['data-'.$controller.'-target'] = $targetName;
50+
return $targets->reduce(function (Collection $acc, $targetName, string $controller) {
51+
$acc['data-' . $controller . '-target'] = $targetName;
5152

5253
return $acc;
53-
}, collect())->map(function ($value, $attr) {
54+
}, collect())->map(function ($value, $attr): string {
5455
$attr = e($attr);
5556
$value = e($value);
5657

src/StimulusLaravelServiceProvider.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,19 @@ public function configurePackage(Package $package): void
2828
]);
2929
}
3030

31-
public function packageBooted()
31+
public function packageBooted(): void
3232
{
3333
$this->bindDirectivesIfEnabled();
3434
}
3535

36-
private function bindDirectivesIfEnabled()
36+
private function bindDirectivesIfEnabled(): void
3737
{
3838
if (! Features::enabled(Features::directives())) {
3939
return;
4040
}
4141

42-
Blade::directive('controller', function ($expression) {
43-
return "<?php echo \HotwiredLaravel\StimulusLaravel\Facades\StimulusLaravel::controller({$expression}); ?>";
44-
});
42+
Blade::directive('controller', fn($expression): string => "<?php echo \HotwiredLaravel\StimulusLaravel\Facades\StimulusLaravel::controller({$expression}); ?>");
4543

46-
Blade::directive('target', function ($expression) {
47-
return "<?php echo \HotwiredLaravel\StimulusLaravel\Facades\StimulusLaravel::target({$expression}); ?>";
48-
});
44+
Blade::directive('target', fn($expression): string => "<?php echo \HotwiredLaravel\StimulusLaravel\Facades\StimulusLaravel::target({$expression}); ?>");
4945
}
5046
}

tests/BladeControllerDirectiveTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class BladeControllerDirectiveTest extends TestCase
88
{
99
use InteractsWithViews;
1010

11-
/** @test */
12-
public function binds_controller()
11+
#[\PHPUnit\Framework\Attributes\Test]
12+
public function binds_controller(): void
1313
{
1414
$this->blade('<div @controller(["hello"])></div>')
1515
->assertSee('<div data-controller="hello"></div>', false);
@@ -21,8 +21,8 @@ public function binds_controller()
2121
->assertSee('<div data-controller="hello something"></div>', false);
2222
}
2323

24-
/** @test */
25-
public function binds_values()
24+
#[\PHPUnit\Framework\Attributes\Test]
25+
public function binds_values(): void
2626
{
2727
$this->blade(trim(<<<'BLADE'
2828
<div @controller(['hello' => ['value' => ['name' => 'Tony']]])></div>
@@ -38,8 +38,8 @@ public function binds_values()
3838
->assertSee('<div data-controller="hello other" data-hello-name-value="Tony" data-other-name-value="Tester"></div>', false);
3939
}
4040

41-
/** @test */
42-
public function binds_css_classes()
41+
#[\PHPUnit\Framework\Attributes\Test]
42+
public function binds_css_classes(): void
4343
{
4444
$this->blade(trim(<<<'BLADE'
4545
<div @controller(['hello' => ['class' => ['loading' => 'bg-gray-100 text-gray-900']]])></div>
@@ -56,8 +56,8 @@ public function binds_css_classes()
5656
' data-other-loading-class="bg-blue-100 text-blue-900"></div>', false);
5757
}
5858

59-
/** @test */
60-
public function handles_mixed_bindings()
59+
#[\PHPUnit\Framework\Attributes\Test]
60+
public function handles_mixed_bindings(): void
6161
{
6262
$this->blade(<<<'BLADE'
6363
<div @controller([

tests/BladeTargetDirectiveTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class BladeTargetDirectiveTest extends TestCase
88
{
99
use InteractsWithViews;
1010

11-
/** @test */
12-
public function binds_targets()
11+
#[\PHPUnit\Framework\Attributes\Test]
12+
public function binds_targets(): void
1313
{
1414
$this->blade(<<<'BLADE'
1515
<form data-controller="search checkbox">

tests/ManifestTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
class ManifestTest extends TestCase
88
{
9-
/** @test */
10-
public function generates_controllers_imports_given_a_path()
9+
#[\PHPUnit\Framework\Attributes\Test]
10+
public function generates_controllers_imports_given_a_path(): void
1111
{
1212
$manifest = (new Manifest)->generateFrom(implode(DIRECTORY_SEPARATOR, [
1313
__DIR__,

tests/StimulusGeneratorTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function setUp(): void
2121
}
2222

2323
#[Test]
24-
public function creates_stimulus_controller_with_regular_name()
24+
public function creates_stimulus_controller_with_regular_name(): void
2525
{
2626
(new StimulusGenerator($this->tmpFolder))
2727
->create('hello');
@@ -31,7 +31,7 @@ public function creates_stimulus_controller_with_regular_name()
3131
}
3232

3333
#[Test]
34-
public function removes_controller_suffix_when_used()
34+
public function removes_controller_suffix_when_used(): void
3535
{
3636
(new StimulusGenerator($this->tmpFolder))
3737
->create('hello_controller');
@@ -41,7 +41,7 @@ public function removes_controller_suffix_when_used()
4141
}
4242

4343
#[Test]
44-
public function generates_controller_with_subfolders()
44+
public function generates_controller_with_subfolders(): void
4545
{
4646
$file = (new StimulusGenerator($this->tmpFolder))
4747
->create('nested/hello_controller');

tests/TestCase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ protected function setUp(): void
1313
parent::setUp();
1414

1515
Factory::guessFactoryNamesUsing(
16-
fn (string $modelName) => 'Tonysm\\StimulusLaravel\\Database\\Factories\\'.class_basename($modelName).'Factory'
16+
fn (string $modelName): string => 'Tonysm\\StimulusLaravel\\Database\\Factories\\'.class_basename($modelName).'Factory'
1717
);
1818
}
1919

20-
public function getEnvironmentSetUp($app)
20+
public function getEnvironmentSetUp($app): void
2121
{
2222
config()->set('database.default', 'testing');
2323

0 commit comments

Comments
 (0)