Skip to content

Commit

Permalink
Rector
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Mar 2, 2025
1 parent 23f714c commit afd24a2
Show file tree
Hide file tree
Showing 46 changed files with 697 additions and 838 deletions.
22 changes: 22 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\ValueObject\PhpVersion;

return RectorConfig::configure()
->withPaths([
__DIR__.'/src',
__DIR__.'/tests',
])
->withPreparedSets(
deadCode: true,
codeQuality: true,
typeDeclarations: true,
privatization: true,
earlyReturn: true,
)
->withAttributesSets()
->withPhpSets()
->withPhpVersion(PhpVersion::PHP_82);
18 changes: 7 additions & 11 deletions src/Broadcasting/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function withoutBroadcasts(callable $callback)
}
}

public function fake()
public function fake(): static
{
$this->recording = true;

Expand Down Expand Up @@ -93,7 +93,7 @@ public function broadcastRefresh(Channel|Model|Collection|array|string|null $cha
action: 'refresh',
channel: $channel,
attributes: array_filter(['request-id' => $requestId = Turbo::currentRequestId()]),
)->lazyCancelIf(fn (PendingBroadcast $broadcast) => (
)->lazyCancelIf(fn (PendingBroadcast $broadcast): bool => (
$this->shouldLimitPageRefreshesOn($broadcast->channels, $requestId)
));
}
Expand All @@ -116,7 +116,7 @@ public function broadcastAction(string $action, $content = null, Model|string|nu
return $broadcast->cancelIf(! $this->isBroadcasting);
}

public function record(PendingBroadcast $broadcast)
public function record(PendingBroadcast $broadcast): static
{
$this->recordedStreams[] = $broadcast;

Expand Down Expand Up @@ -151,9 +151,7 @@ protected function resolveRendering($content)
protected function resolveChannels(Channel|Model|Collection|array|string $channel)
{
if (is_array($channel) || $channel instanceof Collection) {
return collect($channel)->flatMap(function ($channel) {
return $this->resolveChannels($channel);
})->values()->filter()->all();
return collect($channel)->flatMap(fn ($channel) => $this->resolveChannels($channel))->values()->filter()->all();
}

if (is_string($channel)) {
Expand Down Expand Up @@ -184,7 +182,7 @@ public function clearRecordedBroadcasts(): self
return $this;
}

public function assertBroadcasted($callback)
public function assertBroadcasted(?callable $callback): static
{
$result = collect($this->recordedStreams)->filter($callback);

Expand All @@ -193,7 +191,7 @@ public function assertBroadcasted($callback)
return $this;
}

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

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

public function assertNothingWasBroadcasted()
{
return $this->assertBroadcastedTimes(function () {
return true;
}, 0, sprintf('Expected to not have broadcasted any Turbo Stream, but broadcasted %d instead.', count($this->recordedStreams)));
return $this->assertBroadcastedTimes(fn (): true => true, 0, sprintf('Expected to not have broadcasted any Turbo Stream, but broadcasted %d instead.', count($this->recordedStreams)));
}
}
31 changes: 9 additions & 22 deletions src/Broadcasting/PendingBroadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ class PendingBroadcast
/** @var Channel[] */
public array $channels;

public string $action;

public ?string $target = null;

public ?string $targets = null;

public ?string $partialView = null;

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

public bool $escapeInlineContent = true;

public array $attributes = [];

/**
* Whether we should broadcast only to other users and
* ignore the current user's broadcasting socket.
Expand Down Expand Up @@ -61,7 +53,7 @@ class PendingBroadcast
*
* @var ?\HotwiredLaravel\TurboLaravel\Broadcasting\Factory = null
*/
protected $recorder = null;
protected $recorder;

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

public function __construct(array $channels, string $action, Rendering $rendering, ?string $target = null, ?string $targets = null, array $attributes = [])
public function __construct(array $channels, public string $action, Rendering $rendering, public ?string $target = null, public ?string $targets = null, public array $attributes = [])
{
$this->action = $action;
$this->target = $target;
$this->targets = $targets;
$this->attributes = $attributes;

$this->to($channels);
$this->rendering($rendering);
}
Expand Down Expand Up @@ -142,12 +129,12 @@ public function view(?string $view, array $data = []): self
return $this->rendering(new Rendering($view, $data));
}

public function content($content)
public function content(\Illuminate\Contracts\View\View|\Illuminate\Support\HtmlString|string $content)
{
return $this->rendering(Rendering::forContent($content));
}

public function attributes(array $attributes)
public function attributes(array $attributes): static
{
$this->attributes = $attributes;

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

public function rendering(Rendering $rendering)
public function rendering(Rendering $rendering): static
{
$this->partialView = $rendering->partial;
$this->partialData = $rendering->data;
Expand All @@ -187,28 +174,28 @@ public function later(bool $later = true): self
return $this;
}

public function cancel()
public function cancel(): static
{
$this->wasCancelled = true;

return $this;
}

public function cancelIf($condition)
public function cancelIf($condition): static
{
$this->wasCancelled = $this->wasCancelled || boolval(value($condition, $this));

return $this;
}

public function lazyCancelIf(callable $condition)
public function lazyCancelIf(callable $condition): static
{
$this->deferredCancelCallbacks[] = $condition;

return $this;
}

public function fake($recorder = null)
public function fake($recorder = null): static
{
$this->isRecording = true;
$this->recorder = $recorder;
Expand Down
18 changes: 2 additions & 16 deletions src/Broadcasting/Rendering.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,9 @@

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

public ?array $data = [];

public ?string $inlineContent = null;

public bool $escapeInlineContent = true;

public function __construct(?string $partial = null, ?array $data = [], ?string $inlineContent = null, ?bool $escapeInlineContent = true)
{
$this->partial = $partial;
$this->data = $data;
$this->inlineContent = $inlineContent;
$this->escapeInlineContent = $escapeInlineContent;
}

public static function forContent(View|HtmlString|string $content)
public static function forContent(View|HtmlString|string $content): static
{
if ($content instanceof View) {
return new static(partial: $content->name(), data: $content->getData());
Expand Down
20 changes: 9 additions & 11 deletions src/Commands/TurboInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TurboInstallCommand extends Command

public $description = 'Installs Turbo.';

public function handle()
public function handle(): void
{
$this->updateLayouts();
$this->publishJsFiles();
Expand All @@ -26,7 +26,7 @@ public function handle()
$this->components->info('Turbo Laravel was installed successfully.');
}

private function publishJsFiles()
private function publishJsFiles(): void
{
File::ensureDirectoryExists(resource_path('js/elements'));
File::ensureDirectoryExists(resource_path('js/libs'));
Expand All @@ -38,7 +38,7 @@ private function publishJsFiles()
File::put(resource_path('js/libs/index.js'), $this->libsIndexJsImportLines());
}

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

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

private function libsIndexJsImportLines()
private function libsIndexJsImportLines(): string
{
$imports = [];

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

private function installJsDependencies()
private function installJsDependencies(): void
{
if ($this->usingImportmaps()) {
$this->updateImportmapsDependencies();
Expand All @@ -74,9 +74,7 @@ private function installJsDependencies()

private function updateNpmDependencies(): void
{
$this->updateNodePackages(function ($packages) {
return $this->jsDependencies() + $packages;
});
static::updateNodePackages(fn ($packages): array => $this->jsDependencies() + $packages);
}

private function runInstallAndBuildCommand(): void
Expand All @@ -92,7 +90,7 @@ private function runInstallAndBuildCommand(): void
}
}

private function runCommands($commands): void
private function runCommands(array $commands): void
{
$process = Process::fromShellCommandline(implode(' && ', $commands), null, null, null, null);

Expand All @@ -104,7 +102,7 @@ private function runCommands($commands): void
}
}

$process->run(function ($type, $line) {
$process->run(function ($type, string $line): void {
$this->output->write(' '.$line);
});
}
Expand Down Expand Up @@ -174,7 +172,7 @@ private function existingLayoutFiles()
->filter(fn ($file) => File::exists($file));
}

private function phpBinary()
private function phpBinary(): string
{
return (new PhpExecutableFinder)->find(false) ?: 'php';
}
Expand Down
45 changes: 13 additions & 32 deletions src/Events/TurboStreamBroadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,25 @@ class TurboStreamBroadcast implements ShouldBroadcastNow
{
use InteractsWithSockets;

/** @var Channel[] */
public array $channels;

public string $action;

public ?string $target = null;

public ?string $targets = null;

public ?string $partial = null;

public ?array $partialData = [];

public ?string $inlineContent = null;

public array $attrs = [];

public bool $escapeInlineContent = true;

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 = [])
{
$this->channels = $channels;
$this->action = $action;
$this->target = $target;
$this->targets = $targets;
$this->partial = $partial;
$this->partialData = $partialData;
$this->inlineContent = $inlineContent;
$this->escapeInlineContent = $escapeInlineContent;
$this->attrs = $attributes;
}
public function __construct(
/** @var Channel[] */
public array $channels,
public string $action,
public ?string $target = null,
public ?string $targets = null,
public ?string $partial = null,
public ?array $partialData = [],
public ?string $inlineContent = null,
public bool $escapeInlineContent = true,
public array $attrs = []
) {}

public function broadcastOn()
{
return $this->channels;
}

public function broadcastWith()
public function broadcastWith(): array
{
return [
'message' => $this->render(),
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/TurboStreamTargetException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

class TurboStreamTargetException extends InvalidArgumentException
{
public static function targetMissing()
public static function targetMissing(): static
{
return new static('No target was specified');
}

public static function multipleTargets()
public static function multipleTargets(): static
{
return new static('Must specify either target or targets attributes, but never both.');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Facades/Turbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
*/
class Turbo extends Facade
{
public static function usePartialsSubfolderPattern()
public static function usePartialsSubfolderPattern(): void
{
static::resolvePartialsPathUsing('{plural}.partials.{singular}');
}

public static function resolvePartialsPathUsing(string|Closure $pattern)
public static function resolvePartialsPathUsing(string|Closure $pattern): void
{
NamesResolver::resolvePartialsPathUsing($pattern);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/TurboStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected static function getFacadeAccessor()

public static function fake($callback = null)
{
return tap(static::getFacadeRoot(), function ($fake) use ($callback) {
return tap(static::getFacadeRoot(), function ($fake) use ($callback): void {
static::swap($fake->fake($callback));
});
}
Expand Down
Loading

0 comments on commit afd24a2

Please sign in to comment.