Skip to content

Commit

Permalink
Merge pull request #752 from pinkary-project/feat/phpstan-and-rector-2.O
Browse files Browse the repository at this point in the history
Feat: phpstan and rector 2.o
  • Loading branch information
nunomaduro authored Dec 31, 2024
2 parents 5cb0efc + 74377d8 commit 0a2c703
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 151 deletions.
9 changes: 6 additions & 3 deletions app/Console/Commands/PerformDatabaseBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ public function handle(): void

$glob = File::glob(database_path('backups/*.sql'));

collect($glob)->sort()->reverse()->slice(4)->each(
fn (string $backup): bool => File::delete($backup),
);
collect($glob)->sort()->reverse()->slice(4)->filter(
fn (mixed $backup): bool => is_string($backup),
)
->each(
fn (string $backup): bool => File::delete($backup),
);
}
}
2 changes: 1 addition & 1 deletion app/EventActions/UpdateQuestionHashtags.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
}

/**
* @return array{attached: array<int, int>, detached: array<int, int>, updated: array<int, int>}
* @return array{attached: array<array-key, mixed>, detached: array<array-key, mixed>, updated: array<array-key, mixed>}
*/
public function handle(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected function getStats(): array
->selectRaw('COUNT(*) AS total, SUM(is_reported) AS reported, SUM(is_ignored) AS ignored')
->first();

/** @var array<string, int> $counts */
$counts = $counts !== null ? $counts->attributesToArray() : ['total' => 0, 'reported' => 0, 'ignored' => 0];

return [
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Auth/UpdatePasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __invoke(Request $request): RedirectResponse
{
$user = type($request->user())->as(User::class);

/** @var array<string, string> $validated */
$validated = $request->validateWithBag('updatePassword', [
'current_password' => ['required', 'current_password'],
'password' => ['required', Password::defaults(), 'confirmed'],
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/UserAvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function destroy(Request $request): RedirectResponse

UpdateUserAvatar::dispatchSync(
$user,
service: $user->github_username ? 'github' : 'gravatar',
null,
$user->github_username ? 'github' : 'gravatar',
);

return to_route('profile.edit')
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/UserGitHubUsernameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function update(Request $request): RedirectResponse
$user = type($request->user())->as(User::class);

try {
/** @var array<string, string> $validated */
$validated = Validator::validate([
'github_username' => $githubUser->getNickname(),
], [
Expand Down Expand Up @@ -62,8 +63,9 @@ public function update(Request $request): RedirectResponse

if (! $user->is_uploaded_avatar) {
UpdateUserAvatar::dispatch(
user: $user,
service: 'github',
$user,
null,
'github',
);
}

Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/UserTimezoneController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
public function update(Request $request): void
{
/** @var array<string, string> $validated */
$validated = $request->validate([
'timezone' => ['required', 'string', 'max:255', new ValidTimezone],
]);
Expand Down
1 change: 1 addition & 0 deletions app/Jobs/CleanUnusedUploadedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function handle(): void

collect($this->getDateRange($lastRunTime, $fiveMinutesAgo))
->flatMap(fn (string $date): array => $disk->allFiles("images/{$date}"))
->filter(fn (mixed $file): bool => is_string($file))
->filter(function (string $file) use ($disk, $lastRunTime, $fiveMinutesAgo): bool {
$lastModified = Carbon::createFromTimestamp($disk->lastModified($file));

Expand Down
1 change: 1 addition & 0 deletions app/Livewire/Links/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function store(Request $request): void
$this->url = "https://{$this->url}";
}

/** @var array<string, string> $validated */
$validated = $this->validate([
'description' => 'required|max:100',
'url' => ['required', 'max:100', 'url', 'starts_with:https'],
Expand Down
1 change: 1 addition & 0 deletions app/Livewire/Links/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function update(Request $request): void
$this->url = "https://{$this->url}";
}

/** @var array<string, string> $validated */
$validated = $this->validate([
'description' => 'required|max:100',
'url' => ['required', 'max:100', 'url', 'starts_with:https'],
Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Profile/TwoFactorAuthenticationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function enableTwoFactorAuthentication(EnableTwoFactorAuthentication $ena
*/
public function confirmTwoFactorAuthentication(ConfirmTwoFactorAuthentication $confirm): void
{
$confirm(auth()->user(), $this->pull('code'));
$confirm(auth()->user(), type($this->pull('code'))->asString());

$this->showingQrCode = false;
$this->showingConfirmation = false;
Expand Down
1 change: 1 addition & 0 deletions app/Livewire/Questions/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ static function (string $attribute, mixed $value, Closure $fail): void {
/** @var UploadedFile $value */
$dimensions = $value->dimensions();
if (is_array($dimensions)) {
/** @var array<int, int> $dimensions */
[$width, $height] = $dimensions;
$aspectRatio = $width / $height;
$maxAspectRatio = 2 / 5;
Expand Down
1 change: 1 addition & 0 deletions app/Livewire/Questions/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function update(Request $request): void
return;
}

/** @var array<string, string> $validated */
$validated = $this->validate([
'answer' => ['required', 'string', 'max:1000', new NoBlankCharacters],
]);
Expand Down
39 changes: 23 additions & 16 deletions app/Services/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function checkExistsAndSize(string $image): bool
/**
* Get the meta-data for a given URL.
*
* @return Collection<string, string>
* @return Collection<string, non-empty-string>
*/
private function getData(): Collection
{
Expand All @@ -108,14 +108,14 @@ private function getData(): Collection
// Laravel Http Client, Guzzle, and PSR-7
}

return $data;
return $data->filter(fn (mixed $value): bool => is_string($value) && $value !== '');
}

/**
* Fetch the oEmbed data for a given URL.
*
* @param array<string, string|int> $options
* @return Collection<string, string>
* @return Collection<string, non-empty-string>
*/
private function fetchOEmbed(string $service, array $options): Collection
{
Expand All @@ -127,20 +127,19 @@ private function fetchOEmbed(string $service, array $options): Collection
);

if ($response->ok()) {
/** @var Collection<string, string|null> $data */
$data = $response->collect();
}
} catch (ConnectionException) {
// Catch but not capture the exception
}

return $data;
return $data->filter(fn (mixed $value): bool => is_string($value) && $value !== '');
}

/**
* Parse the response body for MetaData.
*
* @return Collection<string, string>
* @return Collection<string, non-empty-string>
*/
private function parseContent(string $content): Collection
{
Expand All @@ -149,7 +148,8 @@ private function parseContent(string $content): Collection

$interested_in = ['og', 'twitter'];
$allowed = ['title', 'description', 'keywords', 'image', 'site_name', 'url', 'type'];
$data = collect();
/** @var Collection<string, string> $data */
$data = new Collection();
$metas = $doc->getElementsByTagName('meta');

if ($metas->count() > 0) {
Expand Down Expand Up @@ -178,14 +178,13 @@ private function parseContent(string $content): Collection
}
}

return $data->filter(fn (?string $value): bool => (string) $value !== '');

return $data->filter(fn (string $value): bool => $value !== '');
}

/**
* Parse the response body for MetaData.
*
* @return Collection<string, string>
* @return Collection<string, non-empty-string>
*
* @throws ConnectionException
*/
Expand Down Expand Up @@ -225,9 +224,13 @@ private function parse(string $html): Collection
);
if ($vimeo->isNotEmpty()) {
foreach ($vimeo as $key => $value) {
$key === 'html'
? $data->put($key, $this->ensureCorrectSize((string) $value))
: $data->put($key, $value);
$value = $key === 'html'
? $this->ensureCorrectSize((string) $value)
: $value;

if ($value !== '') {
$data->put($key, $value);
}
}
}
}
Expand All @@ -242,9 +245,13 @@ private function parse(string $html): Collection

if ($youtube->isNotEmpty()) {
foreach ($youtube as $key => $value) {
$key === 'html'
? $data->put($key, $this->ensureCorrectSize((string) $value))
: $data->put($key, $value);
$value = $key === 'html'
? $this->ensureCorrectSize((string) $value)
: $value;

if ($value !== '') {
$data->put($key, $value);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ function (array $matches): string {
$highlighted = $highlighter->highlight('plaintext', $code);
} // @codeCoverageIgnoreEnd

$highlightedCode = $highlighted->value;
$highlightedLanguage = $highlighted->language;
$highlightedCode = type($highlighted->value)->asString();
$highlightedLanguage = type($highlighted->language)->asString();

return '<pre><code class="p-4 rounded-lg hljs '.$highlightedLanguage.' text-xs" style="background-color: #23262E">'.$highlightedCode.'</code></pre>';
},
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
"require-dev": {
"barryvdh/laravel-debugbar": "^3.14.10",
"fakerphp/faker": "^1.24.1",
"larastan/larastan": "^2.9.12",
"larastan/larastan": "^3.0.0",
"laravel/pint": "^1.18.3",
"laravel/sail": "^1.39.1",
"mockery/mockery": "^1.6.12",
"nunomaduro/collision": "^8.5",
"pestphp/pest": "^3.7.1",
"pestphp/pest-plugin-laravel": "^3.0.0",
"pestphp/pest-plugin-type-coverage": "^3.2.1",
"rector/rector": "^1.2.10"
"rector/rector": "^2.0.0"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit 0a2c703

Please sign in to comment.