Completed
Pull Request — master (#113)
by Ahmed Mohamed Abd El
01:26
created

findRelatedRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Throwable;
6
use InvalidArgumentException;
7
use Illuminate\Support\Facades\Route;
8
use Facade\IgnitionContracts\BaseSolution;
9
use Facade\Ignition\Support\StringComparator;
10
use Facade\Ignition\Exceptions\ViewException;
11
use Facade\IgnitionContracts\HasSolutionsForThrowable;
12
13
class RouteNotDefinedSolutionProvider implements HasSolutionsForThrowable
14
{
15
    protected const REGEX = '/Route \[(.*)\] not defined/m';
16
17
    public function canSolve(Throwable $throwable): bool
18
    {
19
        if (! $throwable instanceof InvalidArgumentException && ! $throwable instanceof ViewException) {
20
            return false;
21
        }
22
23
        return preg_match(self::REGEX, $throwable->getMessage(), $matches);
0 ignored issues
show
Bug introduced by
The method getMessage does only exist in InvalidArgumentException, 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...
24
    }
25
26
    public function getSolutions(Throwable $throwable): array
27
    {
28
        preg_match(self::REGEX, $throwable->getMessage(), $matches);
29
30
        $missingRoute = $matches[1] ?? null;
31
32
        $suggestedRoute = $this->findRelatedRoute($missingRoute);
33
34
        if ($suggestedRoute) {
35
            return [
36
                BaseSolution::create("{$missingRoute} was not defined.")
37
                    ->setSolutionDescription("Did you mean `{$suggestedRoute}`?"),
38
            ];
39
        }
40
41
        return [
42
            BaseSolution::create("{$missingRoute} was not defined.")
43
                ->setSolutionDescription('Are you sure that the route is defined'),
44
        ];
45
    }
46
47
    protected function findRelatedRoute(string $missingRoute): ?string
48
    {
49
        Route::getRoutes()->refreshNameLookups();
50
51
        return StringComparator::findClosestMatch(array_keys(Route::getRoutes()->getRoutesByName()), $missingRoute);
52
    }
53
54
}
55