1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facade\Ignition\SolutionProviders; |
4
|
|
|
|
5
|
|
|
use Facade\Ignition\Exceptions\ViewException; |
6
|
|
|
use Facade\Ignition\Solutions\MakeViewVariableOptionalSolution; |
7
|
|
|
use Facade\Ignition\Solutions\SuggestCorrectVariableNameSolution; |
8
|
|
|
use Facade\IgnitionContracts\BaseSolution; |
9
|
|
|
use Facade\IgnitionContracts\HasSolutionsForThrowable; |
10
|
|
|
use Throwable; |
11
|
|
|
|
12
|
|
|
class UndefinedVariableSolutionProvider implements HasSolutionsForThrowable |
13
|
|
|
{ |
14
|
|
|
private $variableName; |
15
|
|
|
|
16
|
|
|
private $viewFile; |
17
|
|
|
|
18
|
|
|
public function canSolve(Throwable $throwable): bool |
19
|
|
|
{ |
20
|
|
|
if (! $throwable instanceof ViewException) { |
21
|
|
|
return false; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $this->getNameAndView($throwable) !== null; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getSolutions(Throwable $throwable): array |
28
|
|
|
{ |
29
|
|
|
$solutions = []; |
|
|
|
|
30
|
|
|
|
31
|
|
|
extract($this->getNameAndView($throwable)); |
|
|
|
|
32
|
|
|
|
33
|
|
|
if (! isset($variableName)) { |
34
|
|
|
return []; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$solutions = $this->findCorrectVariableSolutions($throwable, $variableName, $viewFile); |
38
|
|
|
$solutions[] = $this->findOptionalVariableSolution($variableName, $viewFile); |
39
|
|
|
|
40
|
|
|
return $solutions; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function findCorrectVariableSolutions(Throwable $throwable, string $variableName, string $viewFile): array |
44
|
|
|
{ |
45
|
|
|
return collect($throwable->getViewData())->map(function ($value, $key) use ($variableName) { |
46
|
|
|
similar_text($variableName, $key, $percentage); |
47
|
|
|
|
48
|
|
|
return ['match' => $percentage, 'value' => $value]; |
49
|
|
|
})->sortByDesc('match')->filter(function ($var, $key) { |
|
|
|
|
50
|
|
|
return $var['match'] > 40; |
51
|
|
|
})->keys()->map(function ($suggestion) use ($variableName, $viewFile) { |
52
|
|
|
return new SuggestCorrectVariableNameSolution($variableName, $viewFile, $suggestion); |
53
|
|
|
})->map(function ($solution) { |
54
|
|
|
return $solution->isRunnable() |
55
|
|
|
? $solution |
56
|
|
|
: BaseSolution::create($solution->getSolutionTitle()) |
57
|
|
|
->setSolutionDescription($solution->getSolutionDescription()); |
58
|
|
|
})->toArray(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function findOptionalVariableSolution(string $variableName, string $viewFile) |
62
|
|
|
{ |
63
|
|
|
$optionalSolution = new MakeViewVariableOptionalSolution($variableName, $viewFile); |
64
|
|
|
|
65
|
|
|
return $optionalSolution->isRunnable() |
66
|
|
|
? $optionalSolution |
67
|
|
|
: BaseSolution::create($optionalSolution->getSolutionTitle()) |
68
|
|
|
->setSolutionDescription($optionalSolution->getSolutionDescription()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getNameAndView(Throwable $throwable): ?array |
72
|
|
|
{ |
73
|
|
|
$pattern = '/Undefined variable: (.*?) \(View: (.*?)\)/'; |
74
|
|
|
|
75
|
|
|
preg_match($pattern, $throwable->getMessage(), $matches); |
76
|
|
|
if (count($matches) === 3) { |
77
|
|
|
[$string, $variableName, $viewFile] = $matches; |
|
|
|
|
78
|
|
|
|
79
|
|
|
return compact('variableName', 'viewFile'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.