Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPStan improvements #8

Merged
merged 34 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7afc482
wip
Baspa Aug 23, 2024
31c5774
wip
Baspa Aug 23, 2024
a05ae18
Add missing types and increase phpstan
Baspa Aug 23, 2024
8e7d08e
Fix styling
Baspa Aug 23, 2024
462854d
Set correct types
Baspa Aug 23, 2024
7dc3aab
Fix types on level 8
Baspa Aug 23, 2024
dfd8274
Merge branch 'feature/php-stan' of github.com:vormkracht10/filament-t…
Baspa Aug 23, 2024
86b47aa
Fix styling
Baspa Aug 23, 2024
a36076c
wip
Baspa Aug 23, 2024
1c0d8b5
Merge branch 'feature/php-stan' of github.com:vormkracht10/filament-t…
Baspa Aug 23, 2024
ccc620b
wip
Baspa Aug 23, 2024
3875f82
wip
Baspa Aug 23, 2024
0fdb84d
wip
Baspa Aug 23, 2024
961a441
wip
Baspa Aug 23, 2024
e1a9845
wip
Baspa Aug 23, 2024
059d96c
Fix styling
Baspa Aug 23, 2024
cac7902
wip
Baspa Aug 23, 2024
c26ddc7
Merge branch 'feature/php-stan' of github.com:vormkracht10/filament-t…
Baspa Aug 23, 2024
b031fdf
wip
Baspa Aug 23, 2024
1b55db0
Fix styling
Baspa Aug 23, 2024
e9042b5
wip
Baspa Aug 23, 2024
1c111e4
Merge branch 'feature/php-stan' of github.com:vormkracht10/filament-t…
Baspa Aug 23, 2024
092f645
wip
Baspa Aug 23, 2024
7fc5eef
Fix styling
Baspa Aug 23, 2024
9119806
wip
Baspa Aug 23, 2024
00b5ca4
Fix styling
Baspa Aug 23, 2024
df58108
wip
Baspa Aug 23, 2024
d9f347d
Fix styling
Baspa Aug 23, 2024
924d280
wip
Baspa Aug 23, 2024
9da0d73
Merge branch 'feature/php-stan' of github.com:vormkracht10/filament-t…
Baspa Aug 23, 2024
44bb025
wip
Baspa Aug 23, 2024
3832d49
Fix styling
Baspa Aug 23, 2024
6d7186a
Force refresh
Baspa Aug 23, 2024
49a7ead
Fix styling
Baspa Aug 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan

on:
push:
pull_request:
paths:
- '**.php'
- 'phpstan.neon.dist'
Expand All @@ -16,11 +16,11 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v3

- name: Run PHPStan
run: ./vendor/bin/phpstan analyse --error-format=github --no-parallel
run: ./vendor/bin/phpstan analyse --error-format=github --debug
5 changes: 3 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ includes:
- phpstan-baseline.neon

parameters:
level: 5
level: 8
paths:
- src
- config
- database
- tests/PHPStan
tmpDir: build/phpstan
parallel:
maximumNumberOfProcesses: 1
Expand All @@ -15,4 +16,4 @@ services:
-
class: Vormkracht10\TwoFactorAuth\Tests\PHPStan\UserPropertiesClassReflectionExtension
tags:
- phpstan.broker.propertiesClassReflectionExtension
- phpstan.broker.propertiesClassReflectionExtension
5 changes: 5 additions & 0 deletions src/Enums/TwoFactorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ enum TwoFactorType: string implements HasLabel
case email = 'email';
case phone = 'phone';

/**
* Get the values of the enum.
*
* @return array<int, string|null>
*/
public static function values(): array
{
return array_map(fn ($type) => $type->getLabel(), self::cases());
Expand Down
35 changes: 22 additions & 13 deletions src/Http/Livewire/Auth/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Laravel\Fortify\Actions\PrepareAuthenticatedSession;
use Laravel\Fortify\Features;
use Laravel\Fortify\Fortify;
use Livewire\Features\SupportRedirects\Redirector;
use Vormkracht10\TwoFactorAuth\Http\Middleware\RedirectIfTwoFactorAuthenticatable;
use Vormkracht10\TwoFactorAuth\Http\Responses\LoginResponse;

Expand All @@ -30,13 +31,13 @@ class Login extends BaseLogin

protected static string $view = 'filament-two-factor-auth::auth.login';

public $email = '';
public string $email = '';

public $password = '';
public string $password = '';

public $resetPasswordEnabled = false;
public bool $resetPasswordEnabled = false;

public $registrationEnabled = false;
public bool $registrationEnabled = false;

public function mount(): void
{
Expand Down Expand Up @@ -73,22 +74,24 @@ protected function getFormSchema(): array
];
}

public function loginWithFortify()
public function loginWithFortify(): LoginResponse | Redirector | null
{
session()->put('panel', Filament::getCurrentPanel()->getId());
session()->put('panel', Filament::getCurrentPanel()?->getId() ?? null);

try {
$this->rateLimit(5);
} catch (TooManyRequestsException $exception) {
$notificationBody = __('filament-panels::pages/auth/login.notifications.throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]);

Notification::make()
->title(__('filament-panels::pages/auth/login.notifications.throttled.title', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]))
->body(array_key_exists('body', __('filament-panels::pages/auth/login.notifications.throttled') ?: []) ? __('filament-panels::pages/auth/login.notifications.throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]) : null)
->body(is_array($notificationBody) ? null : $notificationBody)
->danger()
->send();

Expand All @@ -107,6 +110,12 @@ public function loginWithFortify()

$user = Filament::auth()->user();

if (! Filament::getCurrentPanel()) {
Filament::auth()->logout();

throw new \Exception('Current panel is not set.');
}

if (
($user instanceof FilamentUser) &&
(! $user->canAccessPanel(Filament::getCurrentPanel()))
Expand All @@ -122,7 +131,7 @@ public function loginWithFortify()
});
}

protected function loginPipeline(Request $request)
protected function loginPipeline(Request $request): Pipeline
{
if (Fortify::$authenticateThroughCallback) {
return (new Pipeline(app()))->send($request)->through(array_filter(
Expand All @@ -149,9 +158,9 @@ protected function getPasswordFormComponent(): Component
{
return TextInput::make('password')
->label(__('filament-panels::pages/auth/login.form.password.label'))
->hint(filament()->hasPasswordReset() ? new HtmlString(Blade::render('<x-filament::link href="/forgot-password"> {{ __(\'filament-panels::pages/auth/login.actions.request_password_reset.label\') }}</x-filament::link>')) : null)
->hint(Filament::hasPasswordReset() ? new HtmlString(Blade::render('<x-filament::link href="/forgot-password"> {{ __(\'filament-panels::pages/auth/login.actions.request_password_reset.label\') }}</x-filament::link>')) : null)
->password()
->revealable(filament()->arePasswordsRevealable())
->revealable(Filament::arePasswordsRevealable())
->autocomplete('current-password')
->required()
->extraInputAttributes(['tabindex' => 2]);
Expand Down
14 changes: 9 additions & 5 deletions src/Http/Livewire/Auth/LoginTwoFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class LoginTwoFactor extends Page implements HasActions, HasForms

protected static bool $shouldRegisterNavigation = false;

public ?object $challengedUser = null;
public mixed $challengedUser = null;

public function mount(TwoFactorLoginRequest $request): void
{
Expand Down Expand Up @@ -68,15 +68,19 @@ private function throttle(): bool

return true;
} catch (TooManyRequestsException $exception) {
$translation = __('filament-panels::pages/auth/email-verification/email-verification-prompt.notifications.notification_resend_throttled');
$translationArray = is_array($translation) ? $translation : [];

Notification::make()
->title(__('filament-panels::pages/auth/email-verification/email-verification-prompt.notifications.notification_resend_throttled.title', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => $exception->minutesUntilAvailable,
]))
->body(array_key_exists('body', __('filament-panels::pages/auth/email-verification/email-verification-prompt.notifications.notification_resend_throttled') ?: []) ? __('filament-panels::pages/auth/email-verification/email-verification-prompt.notifications.notification_resend_throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => $exception->minutesUntilAvailable,
]) : null)
->body(array_key_exists('body', $translationArray) ?
__('filament-panels::pages/auth/email-verification/email-verification-prompt.notifications.notification_resend_throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => $exception->minutesUntilAvailable,
]) : [])
->danger()
->send();

Expand Down
8 changes: 6 additions & 2 deletions src/Http/Livewire/Auth/PasswordConfirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vormkracht10\TwoFactorAuth\Http\Livewire\Auth;

use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
Expand All @@ -23,10 +22,15 @@ public function mount(): void
}
}

/**
* Get the form schema.
*
* @return array<int, \Filament\Forms\Components\TextInput>
*/
protected function getFormSchema(): array
{
return [
TextInput::make('password')
\Filament\Forms\Components\TextInput::make('password')
->extraInputAttributes(['name' => 'password'])
->label(__('Password'))
->password()
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Livewire/Auth/PasswordReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PasswordReset extends Page implements HasForms
public function mount(): void
{
if (Filament::auth()->check()) {
redirect()->intended(Filament::getCurrentPanel()->getUrl());
redirect()->intended(Filament::getCurrentPanel()?->getUrl() ?? config('fortify.home'));
}

if (session('status')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Livewire/Auth/RequestPasswordReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RequestPasswordReset extends Page implements HasForms
public function mount(): void
{
if (Filament::auth()->check()) {
redirect()->intended(Filament::getCurrentPanel()->getUrl());
redirect()->intended(Filament::getCurrentPanel()?->getUrl() ?? config('fortify.home'));
}

if (session('status')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Responses/LoginResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function toResponse($request)
{
return $request->wantsJson()
? response()->json(['two_factor' => false])
: redirect()->intended(Filament::getCurrentPanel()->getUrl());
: redirect()->intended(Filament::getCurrentPanel()?->getUrl() ?? config('fortify.home'));
}
}
2 changes: 1 addition & 1 deletion src/Http/Responses/TwoFactorChallengeViewResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class TwoFactorChallengeViewResponse implements LoginResponseContract
*/
public function toResponse($request)
{
return redirect()->intended(Filament::getCurrentPanel()->getUrl());
return redirect()->intended(Filament::getCurrentPanel()?->getUrl() ?? config('fortify.home'));
}
}
2 changes: 1 addition & 1 deletion src/Listeners/SendTwoFactorCodeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct()
*/
public function handle(TwoFactorAuthenticationChallenged | TwoFactorAuthenticationEnabled $event): void
{
/** @var object $user */
/** @var mixed $user */
$user = $event->user;
$user->notify(app(SendOTP::class));
}
Expand Down
16 changes: 8 additions & 8 deletions src/Notifications/SendOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct()
*
* @return array<int, string>
*/
public function via(object $notifiable): array
public function via(mixed $notifiable): array
{
if ($notifiable->two_factor_type === TwoFactorType::email) {
return ['mail'];
Expand All @@ -49,8 +49,12 @@ public function via(object $notifiable): array
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): Mailable
public function toMail(mixed $notifiable): Mailable
{
if (! $notifiable->two_factor_secret) {
throw new \Exception('User does not have a two factor secret.');
}

return (new TwoFactorCodeMail($this->getTwoFactorCode($notifiable)))
->to($notifiable->email);
}
Expand All @@ -60,7 +64,7 @@ public function toMail(object $notifiable): Mailable
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
public function toArray(mixed $notifiable): array
{
return [
//
Expand All @@ -72,12 +76,8 @@ public function toArray(object $notifiable): array
* @throws SecretKeyTooShortException
* @throws InvalidCharactersException
*/
public function getTwoFactorCode(object $notifiable): ?string
public function getTwoFactorCode(mixed $notifiable): string
{
if (! $notifiable->two_factor_secret) {
return null;
}

return GenerateOTP::for(
decrypt($notifiable->two_factor_secret)
);
Expand Down
Loading