From 2fde331a66c0d27cb93785b9ac05991da7316cf3 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Tue, 30 Apr 2024 15:51:47 +0200 Subject: [PATCH 1/6] Add support for handling errors --- composer.json | 3 +- config/flare.php | 4 +- src/FlareMiddleware/AddExceptionHandled.php | 48 +++++++++++++++++++ .../AddExceptionHandledTest.php | 35 ++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/FlareMiddleware/AddExceptionHandled.php create mode 100644 tests/FlareMiddleware/AddExceptionHandledTest.php diff --git a/composer.json b/composer.json index f0e9c7c..ec3f3c1 100644 --- a/composer.json +++ b/composer.json @@ -16,13 +16,14 @@ ], "homepage": "https://flareapp.io/ignition", "license": "MIT", + "repositories" : [{"type" : "path", "url": "../flare-client-php"}], "require": { "php": "^8.1", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", "illuminate/support": "^10.0|^11.0", - "spatie/flare-client-php": "^1.3.5", + "spatie/flare-client-php": "dev-main as 1.4.4", "spatie/ignition": "^1.14", "symfony/console": "^6.2.3|^7.0", "symfony/var-dumper": "^6.2.3|^7.0" diff --git a/config/flare.php b/config/flare.php index 7c37687..111dd9d 100644 --- a/config/flare.php +++ b/config/flare.php @@ -6,6 +6,7 @@ use Spatie\FlareClient\FlareMiddleware\CensorRequestHeaders; use Spatie\LaravelIgnition\FlareMiddleware\AddDumps; use Spatie\LaravelIgnition\FlareMiddleware\AddEnvironmentInformation; +use Spatie\LaravelIgnition\FlareMiddleware\AddExceptionHandled; use Spatie\LaravelIgnition\FlareMiddleware\AddExceptionInformation; use Spatie\LaravelIgnition\FlareMiddleware\AddJobs; use Spatie\LaravelIgnition\FlareMiddleware\AddLogs; @@ -55,6 +56,7 @@ 'max_chained_job_reporting_depth' => 5, ], AddContext::class, + AddExceptionHandled::class, CensorRequestBodyFields::class => [ 'censor_fields' => [ 'password', @@ -70,7 +72,7 @@ 'X-CSRF-TOKEN', 'X-XSRF-TOKEN', ] - ] + ], ], /* diff --git a/src/FlareMiddleware/AddExceptionHandled.php b/src/FlareMiddleware/AddExceptionHandled.php new file mode 100644 index 0000000..18ce2b7 --- /dev/null +++ b/src/FlareMiddleware/AddExceptionHandled.php @@ -0,0 +1,48 @@ +limit(40)->frames(); + $frameCount = count($frames); + + foreach ($frames as $i => $frame) { + // Check first frame, probably Illuminate\Foundation\Exceptions\Handler::report() + // Next frame should be: Illuminate/Foundation/helpers.php::report() + + if ($frame->method !== 'report') { + continue; + } + + if ($frame->class === null) { + continue; + } + + if ($i === $frameCount - 1) { + continue; + } + + if ($frames[$i + 1]->class !== null) { + continue; + } + + if ($frames[$i + 1]->method !== 'report') { + continue; + } + + $report->handled(); + + break; + } + + return $next($report); + } +} diff --git a/tests/FlareMiddleware/AddExceptionHandledTest.php b/tests/FlareMiddleware/AddExceptionHandledTest.php new file mode 100644 index 0000000..d6490d8 --- /dev/null +++ b/tests/FlareMiddleware/AddExceptionHandledTest.php @@ -0,0 +1,35 @@ +bind(ExceptionHandler::class, fn() => $handler); + + $someTriggeredException = new Exception('This is a test exception'); + + report($someTriggeredException); + + expect($handler::$report)->toBeInstanceOf(Report::class); + expect($handler::$report->toArray()) + ->toHaveKey('handled', true); +}); + +it('will not mark an exception handled when it is not', function (){ + $someTriggeredException = new Exception('This is a test exception'); + + $report = Flare::createReport($someTriggeredException); + + expect($report->toArray())->toHaveKey('handled', null); +}); From 169d2aa9a952238dc94c4015156ece69644e3624 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Thu, 2 May 2024 15:21:10 +0200 Subject: [PATCH 2/6] Use correct flare client dependency --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index ec3f3c1..8a18017 100644 --- a/composer.json +++ b/composer.json @@ -16,14 +16,13 @@ ], "homepage": "https://flareapp.io/ignition", "license": "MIT", - "repositories" : [{"type" : "path", "url": "../flare-client-php"}], "require": { "php": "^8.1", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", "illuminate/support": "^10.0|^11.0", - "spatie/flare-client-php": "dev-main as 1.4.4", + "spatie/flare-client-php": "^1.5", "spatie/ignition": "^1.14", "symfony/console": "^6.2.3|^7.0", "symfony/var-dumper": "^6.2.3|^7.0" From 0245635aa17fa298c9b4e8b9a6c758d9db2b4304 Mon Sep 17 00:00:00 2001 From: rubenvanassche Date: Thu, 2 May 2024 13:21:37 +0000 Subject: [PATCH 3/6] Fix styling --- tests/FlareMiddleware/AddExceptionHandledTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/FlareMiddleware/AddExceptionHandledTest.php b/tests/FlareMiddleware/AddExceptionHandledTest.php index d6490d8..8acdadc 100644 --- a/tests/FlareMiddleware/AddExceptionHandledTest.php +++ b/tests/FlareMiddleware/AddExceptionHandledTest.php @@ -5,7 +5,7 @@ use Spatie\FlareClient\Report; use Spatie\LaravelIgnition\Facades\Flare; -it('can see when an exception is handled, meaning it is reported', function (){ +it('can see when an exception is handled, meaning it is reported', function () { $handler = new class(app()) extends Handler { public static Report $report; @@ -15,7 +15,7 @@ public function report(Throwable $e) } }; - app()->bind(ExceptionHandler::class, fn() => $handler); + app()->bind(ExceptionHandler::class, fn () => $handler); $someTriggeredException = new Exception('This is a test exception'); @@ -26,7 +26,7 @@ public function report(Throwable $e) ->toHaveKey('handled', true); }); -it('will not mark an exception handled when it is not', function (){ +it('will not mark an exception handled when it is not', function () { $someTriggeredException = new Exception('This is a test exception'); $report = Flare::createReport($someTriggeredException); From 20117ed829ec26fa71598ad5e240598b78e6a14b Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Thu, 2 May 2024 15:24:22 +0200 Subject: [PATCH 4/6] Handle errors when checking for handled exceptions --- config/flare.php | 4 +- src/FlareMiddleware/AddExceptionHandled.php | 48 ----------------- .../AddExceptionHandledStatus.php | 53 +++++++++++++++++++ 3 files changed, 55 insertions(+), 50 deletions(-) delete mode 100644 src/FlareMiddleware/AddExceptionHandled.php create mode 100644 src/FlareMiddleware/AddExceptionHandledStatus.php diff --git a/config/flare.php b/config/flare.php index 111dd9d..c811478 100644 --- a/config/flare.php +++ b/config/flare.php @@ -6,7 +6,7 @@ use Spatie\FlareClient\FlareMiddleware\CensorRequestHeaders; use Spatie\LaravelIgnition\FlareMiddleware\AddDumps; use Spatie\LaravelIgnition\FlareMiddleware\AddEnvironmentInformation; -use Spatie\LaravelIgnition\FlareMiddleware\AddExceptionHandled; +use Spatie\LaravelIgnition\FlareMiddleware\AddExceptionHandledStatus; use Spatie\LaravelIgnition\FlareMiddleware\AddExceptionInformation; use Spatie\LaravelIgnition\FlareMiddleware\AddJobs; use Spatie\LaravelIgnition\FlareMiddleware\AddLogs; @@ -56,7 +56,7 @@ 'max_chained_job_reporting_depth' => 5, ], AddContext::class, - AddExceptionHandled::class, + AddExceptionHandledStatus::class, CensorRequestBodyFields::class => [ 'censor_fields' => [ 'password', diff --git a/src/FlareMiddleware/AddExceptionHandled.php b/src/FlareMiddleware/AddExceptionHandled.php deleted file mode 100644 index 18ce2b7..0000000 --- a/src/FlareMiddleware/AddExceptionHandled.php +++ /dev/null @@ -1,48 +0,0 @@ -limit(40)->frames(); - $frameCount = count($frames); - - foreach ($frames as $i => $frame) { - // Check first frame, probably Illuminate\Foundation\Exceptions\Handler::report() - // Next frame should be: Illuminate/Foundation/helpers.php::report() - - if ($frame->method !== 'report') { - continue; - } - - if ($frame->class === null) { - continue; - } - - if ($i === $frameCount - 1) { - continue; - } - - if ($frames[$i + 1]->class !== null) { - continue; - } - - if ($frames[$i + 1]->method !== 'report') { - continue; - } - - $report->handled(); - - break; - } - - return $next($report); - } -} diff --git a/src/FlareMiddleware/AddExceptionHandledStatus.php b/src/FlareMiddleware/AddExceptionHandledStatus.php new file mode 100644 index 0000000..9e9f20b --- /dev/null +++ b/src/FlareMiddleware/AddExceptionHandledStatus.php @@ -0,0 +1,53 @@ +limit(40)->frames(); + $frameCount = count($frames); + + try { + foreach ($frames as $i => $frame) { + // Check first frame, probably Illuminate\Foundation\Exceptions\Handler::report() + // Next frame should be: Illuminate/Foundation/helpers.php::report() + + if ($frame->method !== 'report') { + continue; + } + + if ($frame->class === null) { + continue; + } + + if ($i === $frameCount - 1) { + continue; + } + + if ($frames[$i + 1]->class !== null) { + continue; + } + + if ($frames[$i + 1]->method !== 'report') { + continue; + } + + $report->handled(); + + break; + } + } catch (Throwable) { + // Do nothing + } + + return $next($report); + } +} From f1753105842d9141e23036b7de214a0a291bd703 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Thu, 2 May 2024 15:31:01 +0200 Subject: [PATCH 5/6] Remove type --- src/FlareMiddleware/AddExceptionHandledStatus.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FlareMiddleware/AddExceptionHandledStatus.php b/src/FlareMiddleware/AddExceptionHandledStatus.php index 9e9f20b..2311cb3 100644 --- a/src/FlareMiddleware/AddExceptionHandledStatus.php +++ b/src/FlareMiddleware/AddExceptionHandledStatus.php @@ -10,7 +10,7 @@ class AddExceptionHandledStatus implements FlareMiddleware { - public function handle(Report $report, Closure $next): Closure + public function handle(Report $report, Closure $next) { $frames = Backtrace::create()->limit(40)->frames(); $frameCount = count($frames); From 97193a0de1377e3d77500a4ad70a415399f052d7 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Thu, 2 May 2024 15:36:44 +0200 Subject: [PATCH 6/6] Fix PHPStan --- .github/workflows/phpstan.yml | 31 +++++++++++++++++++ phpstan-baseline.neon | 27 +++++++++++++++- .../LaravelLivewireRequestContextProvider.php | 2 +- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/phpstan.yml diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..d8d3f12 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,31 @@ +name: PHPStan + +on: + push: + paths: + - '**.php' + - 'phpstan.neon.dist' + pull_request: + paths: + - '**.php' + - 'phpstan.neon.dist' + + +jobs: + phpstan: + name: phpstan + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + coverage: none + + - name: Install composer dependencies + uses: ramsey/composer-install@v1 + + - name: Run PHPStan + run: ./vendor/bin/phpstan --error-format=github diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 278fddc..d1dfb40 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,5 +1,25 @@ parameters: ignoreErrors: + - + message: "#^Call to an undefined method Livewire\\\\LivewireManager\\:\\:getClass\\(\\)\\.$#" + count: 1 + path: src/ContextProviders/LaravelLivewireRequestContextProvider.php + + - + message: "#^Cannot call method get\\(\\) on Symfony\\\\Component\\\\HttpFoundation\\\\Request\\|null\\.$#" + count: 1 + path: src/ContextProviders/LaravelLivewireRequestContextProvider.php + + - + message: "#^Cannot call method has\\(\\) on Symfony\\\\Component\\\\HttpFoundation\\\\Request\\|null\\.$#" + count: 1 + path: src/ContextProviders/LaravelLivewireRequestContextProvider.php + + - + message: "#^Method Spatie\\\\LaravelIgnition\\\\ContextProviders\\\\LaravelLivewireRequestContextProvider\\:\\:resolveUpdates\\(\\) has parameter \\$updates with no value type specified in iterable type array\\.$#" + count: 1 + path: src/ContextProviders/LaravelLivewireRequestContextProvider.php + - message: "#^Cannot call method toFlare\\(\\) on class\\-string\\|object\\.$#" count: 1 @@ -25,6 +45,11 @@ parameters: count: 1 path: src/Exceptions/InvalidConfig.php + - + message: "#^Class Livewire\\\\LivewireComponentsFinder not found\\.$#" + count: 1 + path: src/Solutions/LivewireDiscoverSolution.php + - message: "#^Parameter \\#1 \\$invalidController of method Spatie\\\\LaravelIgnition\\\\Solutions\\\\SolutionProviders\\\\InvalidRouteActionSolutionProvider\\:\\:findRelatedController\\(\\) expects string, string\\|null given\\.$#" count: 1 @@ -46,7 +71,7 @@ parameters: path: src/Solutions/SolutionProviders/UnknownValidationSolutionProvider.php - - message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\\\:\\:filter\\(\\) expects \\(callable\\(ReflectionMethod, int\\)\\: bool\\)\\|null, Closure\\(ReflectionMethod\\)\\: 0\\|1\\|false given\\.$#" + message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\\\:\\:filter\\(\\) expects \\(callable\\(ReflectionMethod, int\\)\\: bool\\)\\|null, Closure\\(ReflectionMethod\\)\\: \\(0\\|1\\|false\\) given\\.$#" count: 1 path: src/Solutions/SolutionProviders/UnknownValidationSolutionProvider.php diff --git a/src/ContextProviders/LaravelLivewireRequestContextProvider.php b/src/ContextProviders/LaravelLivewireRequestContextProvider.php index cffaef9..ecaa8cb 100644 --- a/src/ContextProviders/LaravelLivewireRequestContextProvider.php +++ b/src/ContextProviders/LaravelLivewireRequestContextProvider.php @@ -38,7 +38,7 @@ public function toArray(): array return $properties; } - /** @return array */ + /** @return array */ protected function getLivewireInformation(): array { if ($this->request->has('components')) {