Skip to content

Commit afd24a2

Browse files
committed
Rector
1 parent 23f714c commit afd24a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+697
-838
lines changed

rector.php

Lines changed: 22 additions & 0 deletions
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/Broadcasting/Factory.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function withoutBroadcasts(callable $callback)
4545
}
4646
}
4747

48-
public function fake()
48+
public function fake(): static
4949
{
5050
$this->recording = true;
5151

@@ -93,7 +93,7 @@ public function broadcastRefresh(Channel|Model|Collection|array|string|null $cha
9393
action: 'refresh',
9494
channel: $channel,
9595
attributes: array_filter(['request-id' => $requestId = Turbo::currentRequestId()]),
96-
)->lazyCancelIf(fn (PendingBroadcast $broadcast) => (
96+
)->lazyCancelIf(fn (PendingBroadcast $broadcast): bool => (
9797
$this->shouldLimitPageRefreshesOn($broadcast->channels, $requestId)
9898
));
9999
}
@@ -116,7 +116,7 @@ public function broadcastAction(string $action, $content = null, Model|string|nu
116116
return $broadcast->cancelIf(! $this->isBroadcasting);
117117
}
118118

119-
public function record(PendingBroadcast $broadcast)
119+
public function record(PendingBroadcast $broadcast): static
120120
{
121121
$this->recordedStreams[] = $broadcast;
122122

@@ -151,9 +151,7 @@ protected function resolveRendering($content)
151151
protected function resolveChannels(Channel|Model|Collection|array|string $channel)
152152
{
153153
if (is_array($channel) || $channel instanceof Collection) {
154-
return collect($channel)->flatMap(function ($channel) {
155-
return $this->resolveChannels($channel);
156-
})->values()->filter()->all();
154+
return collect($channel)->flatMap(fn ($channel) => $this->resolveChannels($channel))->values()->filter()->all();
157155
}
158156

159157
if (is_string($channel)) {
@@ -184,7 +182,7 @@ public function clearRecordedBroadcasts(): self
184182
return $this;
185183
}
186184

187-
public function assertBroadcasted($callback)
185+
public function assertBroadcasted(?callable $callback): static
188186
{
189187
$result = collect($this->recordedStreams)->filter($callback);
190188

@@ -193,7 +191,7 @@ public function assertBroadcasted($callback)
193191
return $this;
194192
}
195193

196-
public function assertBroadcastedTimes($callback, $times = 1, $message = null)
194+
public function assertBroadcastedTimes(?callable $callback, $times = 1, $message = null): static
197195
{
198196
$result = collect($this->recordedStreams)->filter($callback);
199197

@@ -210,8 +208,6 @@ public function assertBroadcastedTimes($callback, $times = 1, $message = null)
210208

211209
public function assertNothingWasBroadcasted()
212210
{
213-
return $this->assertBroadcastedTimes(function () {
214-
return true;
215-
}, 0, sprintf('Expected to not have broadcasted any Turbo Stream, but broadcasted %d instead.', count($this->recordedStreams)));
211+
return $this->assertBroadcastedTimes(fn (): true => true, 0, sprintf('Expected to not have broadcasted any Turbo Stream, but broadcasted %d instead.', count($this->recordedStreams)));
216212
}
217213
}

src/Broadcasting/PendingBroadcast.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ class PendingBroadcast
1818
/** @var Channel[] */
1919
public array $channels;
2020

21-
public string $action;
22-
23-
public ?string $target = null;
24-
25-
public ?string $targets = null;
26-
2721
public ?string $partialView = null;
2822

2923
public ?array $partialData = [];
@@ -32,8 +26,6 @@ class PendingBroadcast
3226

3327
public bool $escapeInlineContent = true;
3428

35-
public array $attributes = [];
36-
3729
/**
3830
* Whether we should broadcast only to other users and
3931
* ignore the current user's broadcasting socket.
@@ -61,7 +53,7 @@ class PendingBroadcast
6153
*
6254
* @var ?\HotwiredLaravel\TurboLaravel\Broadcasting\Factory = null
6355
*/
64-
protected $recorder = null;
56+
protected $recorder;
6557

6658
/**
6759
* These cancel callbacks will run right before the broadcasting is fired on __destruct.
@@ -70,13 +62,8 @@ class PendingBroadcast
7062
*/
7163
protected array $deferredCancelCallbacks = [];
7264

73-
public function __construct(array $channels, string $action, Rendering $rendering, ?string $target = null, ?string $targets = null, array $attributes = [])
65+
public function __construct(array $channels, public string $action, Rendering $rendering, public ?string $target = null, public ?string $targets = null, public array $attributes = [])
7466
{
75-
$this->action = $action;
76-
$this->target = $target;
77-
$this->targets = $targets;
78-
$this->attributes = $attributes;
79-
8067
$this->to($channels);
8168
$this->rendering($rendering);
8269
}
@@ -142,12 +129,12 @@ public function view(?string $view, array $data = []): self
142129
return $this->rendering(new Rendering($view, $data));
143130
}
144131

145-
public function content($content)
132+
public function content(\Illuminate\Contracts\View\View|\Illuminate\Support\HtmlString|string $content)
146133
{
147134
return $this->rendering(Rendering::forContent($content));
148135
}
149136

150-
public function attributes(array $attributes)
137+
public function attributes(array $attributes): static
151138
{
152139
$this->attributes = $attributes;
153140

@@ -170,7 +157,7 @@ public function method(?string $method = null): self
170157
return $this->attributes(Arr::except($this->attributes, 'method'));
171158
}
172159

173-
public function rendering(Rendering $rendering)
160+
public function rendering(Rendering $rendering): static
174161
{
175162
$this->partialView = $rendering->partial;
176163
$this->partialData = $rendering->data;
@@ -187,28 +174,28 @@ public function later(bool $later = true): self
187174
return $this;
188175
}
189176

190-
public function cancel()
177+
public function cancel(): static
191178
{
192179
$this->wasCancelled = true;
193180

194181
return $this;
195182
}
196183

197-
public function cancelIf($condition)
184+
public function cancelIf($condition): static
198185
{
199186
$this->wasCancelled = $this->wasCancelled || boolval(value($condition, $this));
200187

201188
return $this;
202189
}
203190

204-
public function lazyCancelIf(callable $condition)
191+
public function lazyCancelIf(callable $condition): static
205192
{
206193
$this->deferredCancelCallbacks[] = $condition;
207194

208195
return $this;
209196
}
210197

211-
public function fake($recorder = null)
198+
public function fake($recorder = null): static
212199
{
213200
$this->isRecording = true;
214201
$this->recorder = $recorder;

src/Broadcasting/Rendering.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,9 @@
99

1010
class Rendering
1111
{
12-
public ?string $partial = null;
12+
public function __construct(public ?string $partial = null, public ?array $data = [], public ?string $inlineContent = null, public bool $escapeInlineContent = true) {}
1313

14-
public ?array $data = [];
15-
16-
public ?string $inlineContent = null;
17-
18-
public bool $escapeInlineContent = true;
19-
20-
public function __construct(?string $partial = null, ?array $data = [], ?string $inlineContent = null, ?bool $escapeInlineContent = true)
21-
{
22-
$this->partial = $partial;
23-
$this->data = $data;
24-
$this->inlineContent = $inlineContent;
25-
$this->escapeInlineContent = $escapeInlineContent;
26-
}
27-
28-
public static function forContent(View|HtmlString|string $content)
14+
public static function forContent(View|HtmlString|string $content): static
2915
{
3016
if ($content instanceof View) {
3117
return new static(partial: $content->name(), data: $content->getData());

src/Commands/TurboInstallCommand.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TurboInstallCommand extends Command
1616

1717
public $description = 'Installs Turbo.';
1818

19-
public function handle()
19+
public function handle(): void
2020
{
2121
$this->updateLayouts();
2222
$this->publishJsFiles();
@@ -26,7 +26,7 @@ public function handle()
2626
$this->components->info('Turbo Laravel was installed successfully.');
2727
}
2828

29-
private function publishJsFiles()
29+
private function publishJsFiles(): void
3030
{
3131
File::ensureDirectoryExists(resource_path('js/elements'));
3232
File::ensureDirectoryExists(resource_path('js/libs'));
@@ -38,7 +38,7 @@ private function publishJsFiles()
3838
File::put(resource_path('js/libs/index.js'), $this->libsIndexJsImportLines());
3939
}
4040

41-
private function appJsImportLines()
41+
private function appJsImportLines(): string
4242
{
4343
$prefix = $this->usingImportmaps() ? '' : './';
4444

@@ -51,7 +51,7 @@ private function appJsImportLines()
5151
return implode("\n", $imports);
5252
}
5353

54-
private function libsIndexJsImportLines()
54+
private function libsIndexJsImportLines(): string
5555
{
5656
$imports = [];
5757

@@ -62,7 +62,7 @@ private function libsIndexJsImportLines()
6262
return implode("\n", $imports);
6363
}
6464

65-
private function installJsDependencies()
65+
private function installJsDependencies(): void
6666
{
6767
if ($this->usingImportmaps()) {
6868
$this->updateImportmapsDependencies();
@@ -74,9 +74,7 @@ private function installJsDependencies()
7474

7575
private function updateNpmDependencies(): void
7676
{
77-
$this->updateNodePackages(function ($packages) {
78-
return $this->jsDependencies() + $packages;
79-
});
77+
static::updateNodePackages(fn ($packages): array => $this->jsDependencies() + $packages);
8078
}
8179

8280
private function runInstallAndBuildCommand(): void
@@ -92,7 +90,7 @@ private function runInstallAndBuildCommand(): void
9290
}
9391
}
9492

95-
private function runCommands($commands): void
93+
private function runCommands(array $commands): void
9694
{
9795
$process = Process::fromShellCommandline(implode(' && ', $commands), null, null, null, null);
9896

@@ -104,7 +102,7 @@ private function runCommands($commands): void
104102
}
105103
}
106104

107-
$process->run(function ($type, $line) {
105+
$process->run(function ($type, string $line): void {
108106
$this->output->write(' '.$line);
109107
});
110108
}
@@ -174,7 +172,7 @@ private function existingLayoutFiles()
174172
->filter(fn ($file) => File::exists($file));
175173
}
176174

177-
private function phpBinary()
175+
private function phpBinary(): string
178176
{
179177
return (new PhpExecutableFinder)->find(false) ?: 'php';
180178
}

src/Events/TurboStreamBroadcast.php

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,25 @@ class TurboStreamBroadcast implements ShouldBroadcastNow
1212
{
1313
use InteractsWithSockets;
1414

15-
/** @var Channel[] */
16-
public array $channels;
17-
18-
public string $action;
19-
20-
public ?string $target = null;
21-
22-
public ?string $targets = null;
23-
24-
public ?string $partial = null;
25-
26-
public ?array $partialData = [];
27-
28-
public ?string $inlineContent = null;
29-
30-
public array $attrs = [];
31-
32-
public bool $escapeInlineContent = true;
33-
34-
public function __construct(array $channels, string $action, ?string $target = null, ?string $targets = null, ?string $partial = null, ?array $partialData = [], ?string $inlineContent = null, bool $escapeInlineContent = true, array $attributes = [])
35-
{
36-
$this->channels = $channels;
37-
$this->action = $action;
38-
$this->target = $target;
39-
$this->targets = $targets;
40-
$this->partial = $partial;
41-
$this->partialData = $partialData;
42-
$this->inlineContent = $inlineContent;
43-
$this->escapeInlineContent = $escapeInlineContent;
44-
$this->attrs = $attributes;
45-
}
15+
public function __construct(
16+
/** @var Channel[] */
17+
public array $channels,
18+
public string $action,
19+
public ?string $target = null,
20+
public ?string $targets = null,
21+
public ?string $partial = null,
22+
public ?array $partialData = [],
23+
public ?string $inlineContent = null,
24+
public bool $escapeInlineContent = true,
25+
public array $attrs = []
26+
) {}
4627

4728
public function broadcastOn()
4829
{
4930
return $this->channels;
5031
}
5132

52-
public function broadcastWith()
33+
public function broadcastWith(): array
5334
{
5435
return [
5536
'message' => $this->render(),

src/Exceptions/TurboStreamTargetException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
class TurboStreamTargetException extends InvalidArgumentException
88
{
9-
public static function targetMissing()
9+
public static function targetMissing(): static
1010
{
1111
return new static('No target was specified');
1212
}
1313

14-
public static function multipleTargets()
14+
public static function multipleTargets(): static
1515
{
1616
return new static('Must specify either target or targets attributes, but never both.');
1717
}

src/Facades/Turbo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
*/
2626
class Turbo extends Facade
2727
{
28-
public static function usePartialsSubfolderPattern()
28+
public static function usePartialsSubfolderPattern(): void
2929
{
3030
static::resolvePartialsPathUsing('{plural}.partials.{singular}');
3131
}
3232

33-
public static function resolvePartialsPathUsing(string|Closure $pattern)
33+
public static function resolvePartialsPathUsing(string|Closure $pattern): void
3434
{
3535
NamesResolver::resolvePartialsPathUsing($pattern);
3636
}

src/Facades/TurboStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected static function getFacadeAccessor()
3232

3333
public static function fake($callback = null)
3434
{
35-
return tap(static::getFacadeRoot(), function ($fake) use ($callback) {
35+
return tap(static::getFacadeRoot(), function ($fake) use ($callback): void {
3636
static::swap($fake->fake($callback));
3737
});
3838
}

0 commit comments

Comments
 (0)