Skip to content

Commit

Permalink
Merge pull request #30 from spatie/solutions-refactor
Browse files Browse the repository at this point in the history
Refactor solutions
  • Loading branch information
rubenvanassche authored Jun 12, 2024
2 parents 3368ac5 + 75b8997 commit 097040f
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^8.0",
"illuminate/pipeline": "^8.0|^9.0|^10.0|^11.0",
"spatie/backtrace": "^1.5.2",
"spatie/backtrace": "^1.6.1",
"symfony/http-foundation": "^5.2|^6.0|^7.0",
"symfony/mime": "^5.2|^6.0|^7.0",
"symfony/process": "^5.2|^6.0|^7.0",
Expand Down
45 changes: 45 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ parameters:
count: 1
path: src/Glows/Glow.php

-
message: "#^Parameter \\$solution of method Spatie\\\\FlareClient\\\\Report\\:\\:addSolution\\(\\) has invalid type Spatie\\\\ErrorSolutions\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Report.php

-
message: "#^Parameter \\$solution of method Spatie\\\\FlareClient\\\\Report\\:\\:addSolution\\(\\) has invalid type Spatie\\\\Ignition\\\\Contracts\\\\Solution\\.$#"
count: 1
Expand All @@ -20,41 +25,81 @@ parameters:
count: 1
path: src/Report.php

-
message: "#^Access to property \\$aiGenerated on an unknown class Spatie\\\\ErrorSolutions\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Access to property \\$aiGenerated on an unknown class Spatie\\\\Ignition\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Call to method getDocumentationLinks\\(\\) on an unknown class Spatie\\\\ErrorSolutions\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Call to method getDocumentationLinks\\(\\) on an unknown class Spatie\\\\Ignition\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Call to method getSolutionDescription\\(\\) on an unknown class Spatie\\\\ErrorSolutions\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Call to method getSolutionDescription\\(\\) on an unknown class Spatie\\\\Ignition\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Call to method getSolutionTitle\\(\\) on an unknown class Spatie\\\\ErrorSolutions\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Call to method getSolutionTitle\\(\\) on an unknown class Spatie\\\\Ignition\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Class Spatie\\\\ErrorSolutions\\\\Contracts\\\\RunnableSolution not found\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Class Spatie\\\\Ignition\\\\Contracts\\\\RunnableSolution not found\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Parameter \\$solution of method Spatie\\\\FlareClient\\\\Solutions\\\\ReportSolution\\:\\:__construct\\(\\) has invalid type Spatie\\\\ErrorSolutions\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Parameter \\$solution of method Spatie\\\\FlareClient\\\\Solutions\\\\ReportSolution\\:\\:__construct\\(\\) has invalid type Spatie\\\\Ignition\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Parameter \\$solution of method Spatie\\\\FlareClient\\\\Solutions\\\\ReportSolution\\:\\:fromSolution\\(\\) has invalid type Spatie\\\\ErrorSolutions\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Parameter \\$solution of method Spatie\\\\FlareClient\\\\Solutions\\\\ReportSolution\\:\\:fromSolution\\(\\) has invalid type Spatie\\\\Ignition\\\\Contracts\\\\Solution\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Property Spatie\\\\FlareClient\\\\Solutions\\\\ReportSolution\\:\\:\\$solution has unknown class Spatie\\\\ErrorSolutions\\\\Contracts\\\\Solution as its type\\.$#"
count: 1
path: src/Solutions/ReportSolution.php

-
message: "#^Property Spatie\\\\FlareClient\\\\Solutions\\\\ReportSolution\\:\\:\\$solution has unknown class Spatie\\\\Ignition\\\\Contracts\\\\Solution as its type\\.$#"
count: 1
Expand Down
11 changes: 9 additions & 2 deletions src/Flare.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Spatie\FlareClient\Glows\Glow;
use Spatie\FlareClient\Glows\GlowRecorder;
use Spatie\FlareClient\Http\Client;
use Spatie\FlareClient\Support\PhpStackFrameArgumentsFixer;
use Throwable;

class Flare
Expand Down Expand Up @@ -145,10 +146,16 @@ public function argumentReducers(null|array|ArgumentReducers $argumentReducers):
return $this;
}

public function withStackFrameArguments(bool $withStackFrameArguments = true): self
{
public function withStackFrameArguments(
bool $withStackFrameArguments = true,
bool $forcePHPIniSetting = false,
): self {
$this->withStackFrameArguments = $withStackFrameArguments;

if ($forcePHPIniSetting) {
(new PhpStackFrameArgumentsFixer())->enable();
}

return $this;
}

Expand Down
7 changes: 4 additions & 3 deletions src/FlareMiddleware/AddSolutions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace Spatie\FlareClient\FlareMiddleware;

use Closure;
use Spatie\ErrorSolutions\Contracts\SolutionProviderRepository;
use Spatie\FlareClient\Report;
use Spatie\Ignition\Contracts\SolutionProviderRepository;
use Spatie\Ignition\Contracts\SolutionProviderRepository as IgnitionSolutionProviderRepository;

class AddSolutions implements FlareMiddleware
{
protected SolutionProviderRepository $solutionProviderRepository;
protected SolutionProviderRepository|IgnitionSolutionProviderRepository $solutionProviderRepository;

public function __construct(SolutionProviderRepository $solutionProviderRepository)
public function __construct(SolutionProviderRepository|IgnitionSolutionProviderRepository $solutionProviderRepository)
{
$this->solutionProviderRepository = $solutionProviderRepository;
}
Expand Down
10 changes: 6 additions & 4 deletions src/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
use Spatie\Backtrace\Arguments\Reducers\ArgumentReducer;
use Spatie\Backtrace\Backtrace;
use Spatie\Backtrace\Frame as SpatieFrame;
use Spatie\ErrorSolutions\Contracts\Solution;
use Spatie\FlareClient\Concerns\HasContext;
use Spatie\FlareClient\Concerns\UsesTime;
use Spatie\FlareClient\Context\ContextProvider;
use Spatie\FlareClient\Contracts\ProvidesFlareContext;
use Spatie\FlareClient\Glows\Glow;
use Spatie\FlareClient\Solutions\ReportSolution;
use Spatie\Ignition\Contracts\Solution;
use Spatie\LaravelIgnition\Exceptions\ViewException;
use Spatie\Ignition\Contracts\Solution as IgnitionSolution;
use Spatie\LaravelFlare\Exceptions\ViewException;
use Spatie\LaravelIgnition\Exceptions\ViewException as IgnitionViewException;
use Throwable;

class Report
Expand Down Expand Up @@ -95,7 +97,7 @@ public static function createForThrowable(
protected static function getClassForThrowable(Throwable $throwable): string
{
/** @phpstan-ignore-next-line */
if ($throwable::class === ViewException::class) {
if ($throwable::class === IgnitionViewException::class || $throwable::class === ViewException::class) {
/** @phpstan-ignore-next-line */
if ($previous = $throwable->getPrevious()) {
return get_class($previous);
Expand Down Expand Up @@ -259,7 +261,7 @@ public function addGlow(Glow $glow): self
return $this;
}

public function addSolution(Solution $solution): self
public function addSolution(Solution|IgnitionSolution $solution): self
{
$this->solutions[] = ReportSolution::fromSolution($solution)->toArray();

Expand Down
14 changes: 8 additions & 6 deletions src/Solutions/ReportSolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

namespace Spatie\FlareClient\Solutions;

use Spatie\Ignition\Contracts\RunnableSolution;
use Spatie\Ignition\Contracts\Solution as SolutionContract;
use Spatie\ErrorSolutions\Contracts\RunnableSolution;
use Spatie\ErrorSolutions\Contracts\Solution;
use Spatie\Ignition\Contracts\RunnableSolution as IgnitionRunnableSolution;
use Spatie\Ignition\Contracts\Solution as IgnitionSolution;

class ReportSolution
{
protected SolutionContract $solution;
protected Solution|IgnitionSolution $solution;

public function __construct(SolutionContract $solution)
public function __construct(Solution|IgnitionSolution $solution)
{
$this->solution = $solution;
}

public static function fromSolution(SolutionContract $solution): self
public static function fromSolution(Solution|IgnitionSolution $solution): self
{
return new self($solution);
}
Expand All @@ -24,7 +26,7 @@ public static function fromSolution(SolutionContract $solution): self
*/
public function toArray(): array
{
$isRunnable = ($this->solution instanceof RunnableSolution);
$isRunnable = ($this->solution instanceof RunnableSolution || $this->solution instanceof IgnitionRunnableSolution);

return [
'class' => get_class($this->solution),
Expand Down
24 changes: 24 additions & 0 deletions src/Support/PhpStackFrameArgumentsFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Spatie\FlareClient\Support;

class PhpStackFrameArgumentsFixer
{
public function enable(): void
{
if (! $this->isCurrentlyIgnoringStackFrameArguments()) {
return;
}

ini_set('zend.exception_ignore_args', '0');
}

protected function isCurrentlyIgnoringStackFrameArguments(): bool
{
return match (ini_get('zend.exception_ignore_args')) {
'1' => true,
'0' => false,
default => false,
};
}
}
24 changes: 24 additions & 0 deletions tests/Support/PhpStackFrameArgumentsFixerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Spatie\FlareClient\Flare;
use Spatie\FlareClient\Tests\TestClasses\TraceArguments;

it('can enable stack trace arguments on a PHP level', function () {
ini_set('zend.exception_ignore_args', 1);

$report = Flare::make()
->withStackFrameArguments(true, forcePHPIniSetting: false)
->createReport(TraceArguments::create()->exception('string', new DateTime()))
->toArray();

expect($report['stacktrace'][1]['arguments'])->toBeNull();

$report = Flare::make()
->withStackFrameArguments(true, forcePHPIniSetting: true)
->createReport(TraceArguments::create()->exception('string', new DateTime()))
->toArray();

expect($report['stacktrace'][1]['arguments'])
->toBeArray()
->toHaveCount(2);
});

0 comments on commit 097040f

Please sign in to comment.