RouteNotDefinedSolutionProvider::canSolve()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.1111
c 0
b 0
f 0
cc 6
nc 7
nop 1
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Facade\Ignition\Exceptions\ViewException;
6
use Facade\Ignition\Support\StringComparator;
7
use Facade\IgnitionContracts\BaseSolution;
8
use Facade\IgnitionContracts\HasSolutionsForThrowable;
9
use Illuminate\Foundation\Application;
10
use Illuminate\Support\Facades\Route;
11
use InvalidArgumentException;
12
use Symfony\Component\Routing\Exception\RouteNotFoundException;
13
use Throwable;
14
15
class RouteNotDefinedSolutionProvider implements HasSolutionsForThrowable
16
{
17
    protected const REGEX = '/Route \[(.*)\] not defined/m';
18
19
    public function canSolve(Throwable $throwable): bool
20
    {
21
        if (version_compare(Application::VERSION, '6.0.0', '>=')) {
22
            if (! $throwable instanceof RouteNotFoundException) {
23
                return false;
24
            }
25
        }
26
27
        if (version_compare(Application::VERSION, '6.0.0', '<')) {
28
            if (! $throwable instanceof InvalidArgumentException && ! $throwable instanceof ViewException) {
29
                return false;
30
            }
31
        }
32
33
        return preg_match(self::REGEX, $throwable->getMessage(), $matches);
0 ignored issues
show
Bug introduced by
The method getMessage does only exist in Throwable, but not in Facade\Ignition\Exceptions\ViewException.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
34
    }
35
36
    public function getSolutions(Throwable $throwable): array
37
    {
38
        preg_match(self::REGEX, $throwable->getMessage(), $matches);
39
40
        $missingRoute = $matches[1] ?? null;
41
42
        $suggestedRoute = $this->findRelatedRoute($missingRoute);
43
44
        if ($suggestedRoute) {
45
            return [
46
                BaseSolution::create("{$missingRoute} was not defined.")
47
                    ->setSolutionDescription("Did you mean `{$suggestedRoute}`?"),
48
            ];
49
        }
50
51
        return [
52
            BaseSolution::create("{$missingRoute} was not defined.")
53
                ->setSolutionDescription('Are you sure that the route is defined'),
54
        ];
55
    }
56
57
    protected function findRelatedRoute(string $missingRoute): ?string
58
    {
59
        Route::getRoutes()->refreshNameLookups();
60
61
        return StringComparator::findClosestMatch(array_keys(Route::getRoutes()->getRoutesByName()), $missingRoute);
62
    }
63
}
64