diff --git a/.github/workflows/lint_php.yml b/.github/workflows/lint_php.yml new file mode 100644 index 0000000..fd92b01 --- /dev/null +++ b/.github/workflows/lint_php.yml @@ -0,0 +1,42 @@ +name: PHP linting (Pint) + +on: + pull_request: + types: [opened, synchronize, reopened] + +concurrency: + group: Lint php ${{ github.ref }} + cancel-in-progress: true + + +jobs: + phplint: + runs-on: ubuntu-latest + name: PHP linting (Pint) + + strategy: + fail-fast: false + + steps: + - name: Checkout sources + uses: actions/checkout@v4 + + - name: Get pint version + id: pint + run: | + echo "version=$(composer show laravel/pint --locked --format=json | jq -r '.versions[0]' | sed 's/v//')" >> $GITHUB_OUTPUT + + - name: laravel-pint + uses: aglipanci/laravel-pint-action@2.3.0 + with: + configPath: pint.json + pintVersion: ${{ steps.pint.outputs.version }} + + - name: Check if there is any file update needed + run: | + status=$(git status --porcelain) + if [ -n "$status" ]; then + echo -e "Waiting modifications:\n$status" + echo "::error::Laravel pint found fixes. Please run 'vendor/bin/pint' prior to your next commit." + exit -1 + fi diff --git a/.gitignore b/.gitignore index 745f250..a79a3b3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ .phpunit.result.cache .phpunit.cache composer.lock +cghooks.lock + diff --git a/composer.json b/composer.json index 7bb6ad7..6d2f79e 100644 --- a/composer.json +++ b/composer.json @@ -34,13 +34,15 @@ "ext-sqlite3": "*", "guzzlehttp/psr7": "^2.1", "laravel/legacy-factories": "^1.0", + "laravel/pint": "^1.13", "nunomaduro/larastan": "^2.0", "ocramius/package-versions": "^2.0", "orchestra/testbench": "^7.0 || ^8.0", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-phpunit": "^1.0", "phpstan/phpstan-strict-rules": "^1.0", - "phpunit/phpunit": "^9.5 || ^10.0" + "phpunit/phpunit": "^9.5 || ^10.0", + "xheaven/composer-git-hooks": "^3.1" }, "suggest": { "guzzlehttp/psr7": "To provide a psr/http-factory-implementation implementation", @@ -67,8 +69,23 @@ "providers": [ "LaravelWebauthn\\WebauthnServiceProvider" ] + }, + "hooks": { + "config": { + "stop-on-failure": [ + "pre-commit" + ] + }, + "pre-commit": [ + "files=$(git diff --staged --name-only);\"$(dirname \"$0\")/../../vendor/bin/pint\" $files; git add $files" + ] } }, + "scripts": { + "cghooks": "vendor/bin/cghooks", + "post-install-cmd": "cghooks add --ignore-lock", + "post-update-cmd": "cghooks update" + }, "config": { "sort-packages": true, "allow-plugins": { diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..eb556fd --- /dev/null +++ b/pint.json @@ -0,0 +1,8 @@ +{ + "preset": "laravel", + "rules": { + "single_import_per_statement": { + "group_to_single_imports": false + } + } +} diff --git a/src/Actions/AttemptToAuthenticate.php b/src/Actions/AttemptToAuthenticate.php index 147884c..f87fdaf 100644 --- a/src/Actions/AttemptToAuthenticate.php +++ b/src/Actions/AttemptToAuthenticate.php @@ -113,7 +113,7 @@ protected function throwFailedAuthenticationException(Request $request): void /** * Fire the failed authentication attempt event with the given arguments. */ - protected function fireFailedEvent(Request $request, ?User $user = null): void + protected function fireFailedEvent(Request $request, User $user = null): void { event(new Failed(config('webauthn.guard'), $user, [ Webauthn::username() => $user !== null diff --git a/src/Actions/PrepareCreationData.php b/src/Actions/PrepareCreationData.php index c20b0d0..a5e006a 100644 --- a/src/Actions/PrepareCreationData.php +++ b/src/Actions/PrepareCreationData.php @@ -28,7 +28,7 @@ public function __invoke(User $user): PublicKeyCredentialCreationOptions * * @throws \Illuminate\Validation\ValidationException */ - protected function throwFailedRegisterException(User $user, ?Exception $e = null): void + protected function throwFailedRegisterException(User $user, Exception $e = null): void { WebauthnRegisterFailed::dispatch($user, $e); diff --git a/src/Actions/ValidateKeyCreation.php b/src/Actions/ValidateKeyCreation.php index 8d0423b..9dc4baf 100644 --- a/src/Actions/ValidateKeyCreation.php +++ b/src/Actions/ValidateKeyCreation.php @@ -45,7 +45,7 @@ protected function validateAttestation(User $user, array $data, string $keyName) * * @throws \Illuminate\Validation\ValidationException */ - protected function throwFailedRegisterException(User $user, ?Exception $e = null): void + protected function throwFailedRegisterException(User $user, Exception $e = null): void { WebauthnRegisterFailed::dispatch($user, $e); diff --git a/src/Events/WebauthnLogin.php b/src/Events/WebauthnLogin.php index 9076488..fbfd5c3 100644 --- a/src/Events/WebauthnLogin.php +++ b/src/Events/WebauthnLogin.php @@ -8,7 +8,7 @@ class WebauthnLogin { - use SerializesModels, Dispatchable; + use Dispatchable, SerializesModels; /** * Create a new event instance. diff --git a/src/Events/WebauthnLoginData.php b/src/Events/WebauthnLoginData.php index 94e79dc..3199092 100644 --- a/src/Events/WebauthnLoginData.php +++ b/src/Events/WebauthnLoginData.php @@ -9,7 +9,7 @@ class WebauthnLoginData { - use SerializesModels, Dispatchable; + use Dispatchable, SerializesModels; /** * Create a new event instance. diff --git a/src/Events/WebauthnRegister.php b/src/Events/WebauthnRegister.php index 6de9761..5742072 100644 --- a/src/Events/WebauthnRegister.php +++ b/src/Events/WebauthnRegister.php @@ -8,7 +8,7 @@ class WebauthnRegister { - use SerializesModels, Dispatchable; + use Dispatchable, SerializesModels; /** * Create a new event instance. diff --git a/src/Events/WebauthnRegisterData.php b/src/Events/WebauthnRegisterData.php index a7552ea..47309b6 100644 --- a/src/Events/WebauthnRegisterData.php +++ b/src/Events/WebauthnRegisterData.php @@ -9,7 +9,7 @@ class WebauthnRegisterData { - use SerializesModels, Dispatchable; + use Dispatchable, SerializesModels; /** * Create a new event instance. diff --git a/src/Events/WebauthnRegisterFailed.php b/src/Events/WebauthnRegisterFailed.php index 6ab5165..ca69af2 100644 --- a/src/Events/WebauthnRegisterFailed.php +++ b/src/Events/WebauthnRegisterFailed.php @@ -9,7 +9,7 @@ class WebauthnRegisterFailed { - use SerializesModels, Dispatchable; + use Dispatchable, SerializesModels; /** * Create a new event instance. diff --git a/src/Http/Middleware/WebauthnMiddleware.php b/src/Http/Middleware/WebauthnMiddleware.php index cb8d97d..146a8fc 100644 --- a/src/Http/Middleware/WebauthnMiddleware.php +++ b/src/Http/Middleware/WebauthnMiddleware.php @@ -21,7 +21,7 @@ public function __construct( /** * Handle an incoming request. */ - public function handle(Request $request, Closure $next, ?string $guard = null): mixed + public function handle(Request $request, Closure $next, string $guard = null): mixed { if (Webauthn::webauthnEnabled() && ! Webauthn::check()) { abort_if($this->auth->guard($guard)->guest(), 401, /** @var string $m */ $m = trans('webauthn::errors.user_unauthenticated')); diff --git a/src/Http/Responses/LoginSuccessResponse.php b/src/Http/Responses/LoginSuccessResponse.php index 76fb1ba..21709f8 100644 --- a/src/Http/Responses/LoginSuccessResponse.php +++ b/src/Http/Responses/LoginSuccessResponse.php @@ -25,9 +25,6 @@ public function toResponse($request) /** * Create an HTTP response that represents the object. - * - * @param \Illuminate\Http\Request $request - * @return \Symfony\Component\HttpFoundation\Response */ protected function jsonResponse(Request $request): \Symfony\Component\HttpFoundation\Response { diff --git a/src/Models/WebauthnKey.php b/src/Models/WebauthnKey.php index 682d661..b0f3c05 100644 --- a/src/Models/WebauthnKey.php +++ b/src/Models/WebauthnKey.php @@ -89,7 +89,7 @@ public function publicKeyCredentialSource(): Attribute (string) $this->user_id, $this->counter ), - set: function (PublicKeyCredentialSource $value, ?array $attributes = null): array { + set: function (PublicKeyCredentialSource $value, array $attributes = null): array { if (((string) Arr::get($attributes, 'user_id')) !== $value->getUserHandle()) { throw new WrongUserHandleException(); } diff --git a/src/Services/Webauthn/CredentialAssertionValidator.php b/src/Services/Webauthn/CredentialAssertionValidator.php index f3088cc..9129571 100644 --- a/src/Services/Webauthn/CredentialAssertionValidator.php +++ b/src/Services/Webauthn/CredentialAssertionValidator.php @@ -84,7 +84,7 @@ protected function getCredentialSource(User $user, PublicKeyCredential $publicKe return (Webauthn::model())::where('user_id', $user->getAuthIdentifier()) ->where(fn ($query) => $query->where('credentialId', Base64UrlSafe::encode($credentialId)) - ->orWhere('credentialId', Base64UrlSafe::encodeUnpadded($credentialId)) + ->orWhere('credentialId', Base64UrlSafe::encodeUnpadded($credentialId)) ) ->firstOrFail() ->publicKeyCredentialSource; diff --git a/src/Services/Webauthn/RequestOptionsFactory.php b/src/Services/Webauthn/RequestOptionsFactory.php index 2da447a..55e1d64 100644 --- a/src/Services/Webauthn/RequestOptionsFactory.php +++ b/src/Services/Webauthn/RequestOptionsFactory.php @@ -14,8 +14,6 @@ final class RequestOptionsFactory extends OptionsFactory { /** * User verification preference. - * - * @var string|null */ protected ?string $userVerification; @@ -68,8 +66,6 @@ private function getAllowedCredentials(User $user): array /** * Get the rpEntity Id. - * - * @return string|null */ private function getRpId(): ?string { diff --git a/src/WebauthnServiceProvider.php b/src/WebauthnServiceProvider.php index 9d5faf1..c6f3f37 100644 --- a/src/WebauthnServiceProvider.php +++ b/src/WebauthnServiceProvider.php @@ -305,7 +305,7 @@ protected function bindPsrInterfaces(): void $this->app->bind(ServerRequestInterface::class, function ($app) { if (class_exists(PsrHttpFactory::class)) { return $app[PsrHttpFactory::class] - ->createRequest($app->make('request')); + ->createRequest($app->make('request')); } if (class_exists(\GuzzleHttp\Psr7\ServerRequest::class)) { diff --git a/tests/FeatureTestCase.php b/tests/FeatureTestCase.php index 4b626c2..e98fbd9 100644 --- a/tests/FeatureTestCase.php +++ b/tests/FeatureTestCase.php @@ -42,9 +42,9 @@ protected function getEnvironmentSetUp($app) // Setup default database to use sqlite :memory: $app['config']->set('database.default', 'testbench'); $app['config']->set('database.connections.testbench', [ - 'driver' => 'sqlite', + 'driver' => 'sqlite', 'database' => ':memory:', - 'prefix' => '', + 'prefix' => '', ]); } @@ -79,7 +79,6 @@ protected function resolveApplicationHttpKernel($app) * Create a user and sign in as that user. If a user * object is passed, then sign in as that user. * - * @param null $user * @return User */ public function signIn($user = null)