Skip to content

Commit

Permalink
fix: Resolves phpstan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-code-labx committed Aug 11, 2024
1 parent 20a02e3 commit 1554189
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 27 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"pestphp/pest-plugin-livewire": "^2.1",
"pestphp/pest-plugin-type-coverage": "^2.8",
"phpunit/phpunit": "^10",
"rector/rector": "^1.0.4",
"rector/rector": "^1.2",
"spatie/invade": "^1.1",
"spatie/pest-plugin-snapshots": "^2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion config/rest-presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
],
'logout_revoke_all_tokens' => env('REST_PRESENTER_AUTH_LOGOUT_REVOKE_ALL_TOKENS', false),
'rate_limit' => [
'max_attempts' => (int) env('REST_PRESENTER_AUTH_RATE_LIMIT_MAX_ATTEMPTS', 5),
'max_attempts' => (int) env('REST_PRESENTER_AUTH_RATE_LIMIT_MAX_ATTEMPTS', 5), // @phpstan-ignore-line
],
],
'exporters' => [
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
includes:
- vendor/larastan/larastan/extension.neon
parameters:
excludePaths:
- src/Concerns/InteractsWithSushi.php
paths:
- src
- config
Expand Down
4 changes: 2 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

declare(strict_types=1);

use Rector\CodeQuality\Rector\Concat\JoinStringConcatRector;
use Rector\Config\RectorConfig;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector;
use RectorLaravel\Set\LaravelSetList;

Expand All @@ -13,8 +13,8 @@
__DIR__.'/tests',
])
->withSkip([
AddOverrideAttributeToOverriddenMethodsRector::class,
BooleanInBooleanNotRuleFixerRector::class,
JoinStringConcatRector::class,
])
->withAttributesSets(
symfony: true,
Expand Down
11 changes: 8 additions & 3 deletions src/Commands/Generator/MakeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,19 @@ private function promptForFilters(InputInterface $input): void
$suggestFilters = $this->generateFilterSuggestions();

if ($suggestFilters->isNotEmpty()) {
/** @var array<string> $selectedFilters */
/** @var Collection<int|string, string> $options */
$options = $suggestFilters;
/** @var Collection<int, int|string> $defaultOptions */
$defaultOptions = $suggestFilters->keys();

$selectedFilters = multiselect(
label: __('Here are some suggested filters we found in your :model model to add to your generated resource:', ['model' => class_basename($this->model)]),
options: $suggestFilters, // @phpstan-ignore-line
default: $suggestFilters->keys(), // @phpstan-ignore-line
options: $options,
default: $defaultOptions,
hint: 'Press <comment>Space</> to select the filters then <comment>Enter</> to confirm.',
);

/** @var \Illuminate\Support\Collection<string, string> $selectedFilters */
$this->filters = $suggestFilters->only($selectedFilters);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/InteractsWithCollectionExports.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function downloadExportedCollection(string $type): BinaryFileResponse
protected function getLatestExportedCollection(string $type, string $filenameSuffix): string
{
$collections = glob(resource_path("rest-presenter/$type/*_$filenameSuffix"));
if (empty($collections)) {
if (! $collections) {
throw new InvalidArgumentException('No collections found');
}

Expand Down
14 changes: 14 additions & 0 deletions src/Concerns/InteractsWithConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace XtendPackages\RESTPresenter\Concerns;

trait InteractsWithConfig
{
// @phpstan-ignore-next-line
protected function config(string $key, mixed $default = null)
{
return config($key, $default) ?? $default;
}
}
6 changes: 4 additions & 2 deletions src/Concerns/InteractsWithSushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@

use Closure;
use DateTime;
use Illuminate\Database\Connection;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Str;
use ReflectionClass;

/** @phpstan-consistent-constructor */
trait InteractsWithSushi
{
protected static mixed $sushiConnection;
protected static Connection $sushiConnection;

public static function resolveConnection($connection = null): mixed
public static function resolveConnection($connection = null): Connection
{
return static::$sushiConnection;
}
Expand Down
37 changes: 23 additions & 14 deletions src/StarterKits/Filament/FilamentPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,32 @@
use Filament\PanelProvider;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use XtendPackages\RESTPresenter\Concerns\InteractsWithConfig;

final class FilamentPanelProvider extends PanelProvider
{
use InteractsWithConfig;

public function panel(Panel $panel): Panel
{
return $panel
->id(config('rest-presenter.panel.path'))
->path(config('rest-presenter.panel.path'))
->id($this->config('rest-presenter.panel.path'))
->path($this->config('rest-presenter.panel.path'))
->font('Work Sans')
->brandName(config('rest-presenter.panel.brand_name'))
->brandName($this->config('rest-presenter.panel.brand_name'))
->brandLogo(
fn () => config('rest-presenter.panel.brand_logo')
fn () => $this->config('rest-presenter.panel.brand_logo')
? view('rest-presenter::brand-logo')
: null,
)
->maxContentWidth(config('rest-presenter.panel.max_width'))
->topNavigation(config('rest-presenter.panel.top_navigation'))
->maxContentWidth($this->config('rest-presenter.panel.max_width'))
->topNavigation($this->config('rest-presenter.panel.top_navigation'))
->globalSearch(false)
->spa()
->discoverResources(in: __DIR__.'/Resources', for: 'XtendPackages\\RESTPresenter\\StarterKits\\Filament\\Resources')
Expand All @@ -57,7 +61,7 @@ public function panel(Panel $panel): Panel
public function boot(): void
{
$panelIds = collect(Filament::getPanels())
->filter(fn (Panel $panel): bool => $panel->getId() !== config('rest-presenter.panel.path'))
->filter(fn (Panel $panel): bool => $panel->getId() !== $this->config('rest-presenter.panel.path'))
->keys();

$panelIds->each(
Expand All @@ -70,18 +74,23 @@ private function setUserMenuPanelRoute(string $panelId): void
Filament::getPanel($panelId)->userMenuItems([
MenuItem::make()
->visible(fn (): bool => $this->getAuthenticatedUser())
->label(config('rest-presenter.panel.brand_name'))
->url(fn (): string => '/'.config('rest-presenter.panel.path'))
->label($this->config('rest-presenter.panel.brand_name'))
->url(fn (): string => '/'.$this->config('rest-presenter.panel.path'))
->icon('heroicon-o-server-stack'),
]);
}

private function getAuthenticatedUser(): bool
{
return auth()
->user()
->canAccessPanel(
Filament::getPanel(config('rest-presenter.panel.path'))
);
$user = type(auth()->user())->as(Authenticatable::class);

if (! method_exists($user, 'canAccessPanel')) {
return false;
}

/** @var \Filament\Models\Contracts\FilamentUser $user */
return $user->canAccessPanel(
Filament::getPanel($this->config('rest-presenter.panel.path')),
);
}
}
5 changes: 4 additions & 1 deletion src/StarterKits/Sanctum/Actions/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace XtendPackages\RESTPresenter\StarterKits\Sanctum\Actions;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
Expand All @@ -21,7 +22,9 @@ final class Login
public function __invoke(LoginDataRequest $request): JsonResponse
{
$this->authenticate($request);
$user = type(auth()->user())->as(config('rest-presenter.resources.user.model'));

/** @var \Laravel\Sanctum\Contracts\HasApiTokens $user */
$user = type(auth()->user())->as(Authenticatable::class);

$config = [
'abilities' => type(config('rest-presenter.auth.abilities'))->asArray(),
Expand Down
5 changes: 3 additions & 2 deletions src/StarterKits/Sanctum/Actions/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace XtendPackages\RESTPresenter\StarterKits\Sanctum\Actions;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Http\JsonResponse;
use XtendPackages\RESTPresenter\Models\User;

Expand Down Expand Up @@ -32,7 +33,7 @@ public function __invoke(): JsonResponse
private function deleteAllTokens(): void
{
/** @var User $user */
$user = type(auth()->user())->as(config('rest-presenter.resources.user.model'));
$user = type(auth()->user())->as(Authenticatable::class);

/** @var \Illuminate\Database\Eloquent\Relations\MorphMany<\Laravel\Sanctum\PersonalAccessToken> $tokens */
$tokens = $user->tokens();
Expand All @@ -45,7 +46,7 @@ private function deleteAllTokens(): void
private function deleteCurrentToken(): void
{
/** @var User $user */
$user = type(auth()->user())->as(config('rest-presenter.resources.user.model'));
$user = type(auth()->user())->as(Authenticatable::class);

/** @var \Laravel\Sanctum\Contracts\HasAbilities $currentAccessToken */
$currentAccessToken = $user->currentAccessToken();
Expand Down

0 comments on commit 1554189

Please sign in to comment.