From a5f86d6e38a0705aab1d4283c5c3fc61a626cbe3 Mon Sep 17 00:00:00 2001 From: Tony Lea Date: Sun, 19 May 2024 18:57:03 -0400 Subject: [PATCH] Adding updates to php stan and setting level to 1 --- composer.json | 1 + phpstan.neon | 13 +--- src/AuthServiceProvider.php | 7 +- src/Http/Controllers/SocialController.php | 20 ++--- src/Livewire/Setup/Logo.php | 4 +- src/Models/SocialProvider.php | 6 ++ src/Models/SocialProviderUser.php | 17 ++++- src/Models/User.php | 1 + src/PHPStan/Extensions/ConfigExtension.php | 30 -------- src/Traits/HasSocialProviders.php | 20 +++-- src/Traits/HasTwoFactorAuth.php | 86 ---------------------- 11 files changed, 54 insertions(+), 151 deletions(-) delete mode 100644 src/PHPStan/Extensions/ConfigExtension.php delete mode 100644 src/Traits/HasTwoFactorAuth.php diff --git a/composer.json b/composer.json index c91755d..8c3f283 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "orchestra/testbench": "^9.0", "pestphp/pest": "^2.34", "pestphp/pest-plugin-laravel": "^2.4", + "larastan/larastan": "^2.0", "phpstan/phpstan": "^1.11" }, "autoload": { diff --git a/phpstan.neon b/phpstan.neon index b849c62..fcb4688 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,15 +1,10 @@ includes: - - vendor/phpstan/phpstan/conf/bleedingEdge.neon - -services: - - - class: Devdojo\Auth\PHPStan\Extensions\ConfigExtension - tags: - - phpstan.broker.dynamicMethodReturnTypeExtension + - vendor/larastan/larastan/extension.neon parameters: - level: max paths: - - src + - packages/devdojo/auth/src + - vendor/devdojo/auth/src + level: 1 excludePaths: - /Users/tonylea/Sites/auth/vendor diff --git a/src/AuthServiceProvider.php b/src/AuthServiceProvider.php index 0c2440c..54b17fd 100644 --- a/src/AuthServiceProvider.php +++ b/src/AuthServiceProvider.php @@ -16,8 +16,9 @@ class AuthServiceProvider extends ServiceProvider { /** * Bootstrap the application services. + * @return void */ - public function boot() + public function boot() : void { Route::middlewareGroup('two-factor-challenged', [TwoFactorChallenged::class]); @@ -74,7 +75,7 @@ public function boot() //app()->register(\October\Rain\Config\ServiceProvider::class); } - private function registerAuthFolioDirectory() + private function registerAuthFolioDirectory() : void { $pagesDirectory = __DIR__.'/../resources/views/pages'; if (File::exists($pagesDirectory)) { @@ -86,7 +87,7 @@ private function registerAuthFolioDirectory() } } - private function registerVoltDirectory() + private function registerVoltDirectory() : void { Volt::mount([ __DIR__.'/../resources/views/pages', diff --git a/src/Http/Controllers/SocialController.php b/src/Http/Controllers/SocialController.php index 5bb2094..6eddbb6 100644 --- a/src/Http/Controllers/SocialController.php +++ b/src/Http/Controllers/SocialController.php @@ -2,7 +2,7 @@ namespace Devdojo\Auth\Http\Controllers; -use App\Models\User; +use Devdojo\Auth\Models\User; use Devdojo\Auth\Models\SocialProvider; use Devdojo\Auth\Models\SocialProviderUser; use Illuminate\Support\Facades\Auth; @@ -35,7 +35,7 @@ public function callback(Request $request, $driver) DB::transaction(function () use ($socialiteUser, $driver) { // Attempt to find the user based on the social provider's ID and slug $socialProviderUser = SocialProviderUser::where('provider_slug', $driver) - ->where('provider_user_id', $socialiteUser->id) + ->where('provider_user_id', $socialiteUser->getId()) ->first(); if ($socialProviderUser) { @@ -46,7 +46,7 @@ public function callback(Request $request, $driver) } // Check if the email from the social provider already exists in the User table - $user = User::where('email', $socialiteUser->email)->first(); + $user = User::where('email', $socialiteUser->getEmail())->first(); if ($user) { // Inform the user that an account with this email already exists @@ -55,8 +55,8 @@ public function callback(Request $request, $driver) // No user exists, register a new user $newUser = User::create([ - 'name' => $socialiteUser->name, - 'email' => $socialiteUser->email, + 'name' => $socialiteUser->getName(), + 'email' => $socialiteUser->getEmail(), // Add other fields as necessary ]); @@ -65,11 +65,11 @@ public function callback(Request $request, $driver) // Now add the social provider info for this new user $newUser->addOrUpdateSocialProviderUser($driver, [ - 'provider_user_id' => $socialiteUser->id, - 'nickname' => $socialiteUser->nickname, - 'name' => $socialiteUser->name, - 'email' => $socialiteUser->email, - 'avatar' => $socialiteUser->avatar, + 'provider_user_id' => $socialiteUser->getId(), + 'nickname' => $socialiteUser->getNickname(), + 'name' => $socialiteUser->getName(), + 'email' => $socialiteUser->getEmail(), + 'avatar' => $socialiteUser->getAvatar(), 'provider_data' => json_encode($socialiteUser->user), 'token' => $socialiteUser->token, 'refresh_token' => $socialiteUser->refreshToken, diff --git a/src/Livewire/Setup/Logo.php b/src/Livewire/Setup/Logo.php index 934fc28..dba83dd 100644 --- a/src/Livewire/Setup/Logo.php +++ b/src/Livewire/Setup/Logo.php @@ -87,8 +87,8 @@ public function logoValue() $logo = match ($this->logo_type) { 'image' => $this->logo_image, - 'svg' => $this->logo_svg, - 'text' => $this->logo_text + 'svg' => $this->logo_svg_string, + 'text' => $this->logo_image_src }; return $logo; diff --git a/src/Models/SocialProvider.php b/src/Models/SocialProvider.php index bcc3ea2..82c3545 100644 --- a/src/Models/SocialProvider.php +++ b/src/Models/SocialProvider.php @@ -5,6 +5,12 @@ use Illuminate\Database\Eloquent\Model; use Sushi\Sushi; +/** + * Class User + * + * @property string|null $client_id + * @property string|null $client_secret + */ class SocialProvider extends Model { use Sushi; diff --git a/src/Models/SocialProviderUser.php b/src/Models/SocialProviderUser.php index 50f9b43..76c53fd 100644 --- a/src/Models/SocialProviderUser.php +++ b/src/Models/SocialProviderUser.php @@ -4,6 +4,7 @@ use App\Models\User; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; class SocialProviderUser extends Model { @@ -33,14 +34,22 @@ class SocialProviderUser extends Model 'provider_data' => 'array', ]; - // Define a relationship to the User model - public function user() + /** + * Get the user that belongs to this SocialProvderUser + * + * @return BelongsTo + */ + public function user(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); } - // Define a relationship to the SocialProvider model via Sushi - public function socialProvider() + /** + * Get the social provider for the social provider user. + * + * @return BelongsTo + */ + public function socialProvider(): BelongsTo { return $this->belongsTo(SocialProvider::class, 'provider_slug', 'slug'); } diff --git a/src/Models/User.php b/src/Models/User.php index 8438053..13b2dc9 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -14,6 +14,7 @@ * @property string|null $two_factor_secret * @property string|null $two_factor_recovery_codes * @property \Illuminate\Support\Carbon|null $two_factor_confirmed_at + * @property \Illuminate\Database\Eloquent\Relations\HasMany $socialProviders */ class User extends Authenticatable implements MustVerifyEmail { diff --git a/src/PHPStan/Extensions/ConfigExtension.php b/src/PHPStan/Extensions/ConfigExtension.php deleted file mode 100644 index ccfd5e8..0000000 --- a/src/PHPStan/Extensions/ConfigExtension.php +++ /dev/null @@ -1,30 +0,0 @@ -getName() === 'write'; - } - - public function getTypeFromMethodCall( - MethodReflection $methodReflection, - \PhpParser\Node\Expr\MethodCall $methodCall, - Scope $scope - ): Type { - return new ObjectType(\Devdojo\ConfigWriter\Repository::class); - } -} diff --git a/src/Traits/HasSocialProviders.php b/src/Traits/HasSocialProviders.php index 9e7c2e8..c8246c7 100644 --- a/src/Traits/HasSocialProviders.php +++ b/src/Traits/HasSocialProviders.php @@ -3,23 +3,29 @@ namespace Devdojo\Auth\Traits; use Devdojo\Auth\Models\SocialProviderUser; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Collection; trait HasSocialProviders { /** * Relationship with SocialProviderUser. + * + * @return HasMany */ - public function socialProviders() + public function socialProviders(): HasMany { return $this->hasMany(SocialProviderUser::class); } /** * Retrieve a list of social providers linked to the user. + * + * @return Collection */ - public function getLinkedSocialProvidersAttribute() + public function getLinkedSocialProvidersAttribute(): Collection { - return $this->socialProviders->map(function ($providerUser) { + return collect($this->socialProviders->get())->map(function (SocialProviderUser $providerUser) { return $providerUser->socialProvider; }); } @@ -30,7 +36,7 @@ public function getLinkedSocialProvidersAttribute() * @param string $providerSlug The slug of the social provider. * @return SocialProviderUser|null */ - public function getSocialProviderUser($providerSlug) + public function getSocialProviderUser(string $providerSlug): ?SocialProviderUser { return $this->socialProviders->firstWhere('provider_slug', $providerSlug); } @@ -41,7 +47,7 @@ public function getSocialProviderUser($providerSlug) * @param string $providerSlug The slug of the social provider. * @return bool */ - public function hasSocialProvider($providerSlug) + public function hasSocialProvider(string $providerSlug): bool { return $this->getSocialProviderUser($providerSlug) !== null; } @@ -50,10 +56,10 @@ public function hasSocialProvider($providerSlug) * Add or update social provider user information for a given provider. * * @param string $providerSlug The slug of the social provider. - * @param array $data Data to store/update for the provider. + * @param array $data Data to store/update for the provider. * @return SocialProviderUser */ - public function addOrUpdateSocialProviderUser($providerSlug, array $data) + public function addOrUpdateSocialProviderUser(string $providerSlug, array $data): SocialProviderUser { $providerUser = $this->getSocialProviderUser($providerSlug); diff --git a/src/Traits/HasTwoFactorAuth.php b/src/Traits/HasTwoFactorAuth.php deleted file mode 100644 index 6a24f03..0000000 --- a/src/Traits/HasTwoFactorAuth.php +++ /dev/null @@ -1,86 +0,0 @@ -two_factor_secret) && - ! is_null($this->two_factor_confirmed_at); - } - - return ! is_null($this->two_factor_secret); - } - - /** - * Get the user's two factor authentication recovery codes. - * - * @return array - */ - public function recoveryCodes() - { - return json_decode(decrypt($this->two_factor_recovery_codes), true); - } - - /** - * Replace the given recovery code with a new one in the user's stored codes. - * - * @param string $code - * @return void - */ - public function replaceRecoveryCode($code) - { - $this->forceFill([ - 'two_factor_recovery_codes' => encrypt(str_replace( - $code, - RecoveryCode::generate(), - decrypt($this->two_factor_recovery_codes) - )), - ])->save(); - } - - /** - * Get the QR code SVG of the user's two factor authentication QR code URL. - * - * @return string - */ - public function twoFactorQrCodeSvg() - { - $svg = (new Writer( - new ImageRenderer( - new RendererStyle(192, 0, null, null, Fill::uniformColor(new Rgb(255, 255, 255), new Rgb(45, 55, 72))), - new SvgImageBackEnd - ) - ))->writeString($this->twoFactorQrCodeUrl()); - - return trim(substr($svg, strpos($svg, "\n") + 1)); - } - - /** - * Get the two factor authentication QR code URL. - * - * @return string - */ - public function twoFactorQrCodeUrl() - { - return app(TwoFactorAuthenticationProvider::class)->qrCodeUrl( - config('app.name'), - $this->{Fortify::username()}, - decrypt($this->two_factor_secret) - ); - } -}