1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facade\Ignition\SolutionProviders; |
4
|
|
|
|
5
|
|
|
use Throwable; |
6
|
|
|
use Facade\IgnitionContracts\Solution; |
7
|
|
|
use Facade\IgnitionContracts\BaseSolution; |
8
|
|
|
use Facade\Ignition\Exceptions\ViewException; |
9
|
|
|
use Facade\IgnitionContracts\HasSolutionsForThrowable; |
10
|
|
|
use Facade\Ignition\Solutions\MakeViewVariableOptionalSolution; |
11
|
|
|
use Facade\Ignition\Solutions\SuggestCorrectVariableNameSolution; |
12
|
|
|
|
13
|
|
|
class UndefinedVariableSolutionProvider implements HasSolutionsForThrowable |
14
|
|
|
{ |
15
|
|
|
private $variableName; |
16
|
|
|
|
17
|
|
|
private $viewFile; |
18
|
|
|
|
19
|
|
|
public function canSolve(Throwable $throwable): bool |
20
|
|
|
{ |
21
|
|
|
if (! $throwable instanceof ViewException) { |
22
|
|
|
return false; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $this->getNameAndView($throwable) !== null; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getSolutions(Throwable $throwable): array |
29
|
|
|
{ |
30
|
|
|
$solutions = []; |
|
|
|
|
31
|
|
|
|
32
|
|
|
extract($this->getNameAndView($throwable)); |
|
|
|
|
33
|
|
|
$solutions = collect($throwable->getViewData())->map(function ($value, $key) use ($variableName) { |
34
|
|
|
similar_text($variableName, $key, $percentage); |
35
|
|
|
|
36
|
|
|
return ['match' => $percentage, 'value' => $value]; |
37
|
|
|
})->sortByDesc('match')->filter(function ($var, $key) { |
|
|
|
|
38
|
|
|
return $var['match'] > 40; |
39
|
|
|
})->keys()->map(function ($suggestion) use ($variableName, $viewFile) { |
40
|
|
|
return new SuggestCorrectVariableNameSolution($variableName, $viewFile, $suggestion); |
41
|
|
|
})->map(function ($solution) { |
42
|
|
|
// If the solution isn't runnable, then just return the suggestions without the fix |
43
|
|
|
if ($solution->isRunnable()) { |
44
|
|
|
return $solution; |
45
|
|
|
} else { |
46
|
|
|
return BaseSolution::create($solution->getSolutionTitle()) |
47
|
|
|
->setSolutionDescription($solution->getSolutionActionDescription()); |
48
|
|
|
} |
49
|
|
|
})->toArray(); |
50
|
|
|
|
51
|
|
|
$optionalSolution = new MakeViewVariableOptionalSolution($variableName, $viewFile); |
52
|
|
|
if ($optionalSolution->isRunnable()) { |
53
|
|
|
$solutions[] = $optionalSolution; |
54
|
|
|
} else { |
55
|
|
|
$solutions[] = BaseSolution::create($optionalSolution->getSolutionTitle()) |
56
|
|
|
->setSolutionDescription($optionalSolution->getSolutionActionDescription()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $solutions; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function getNameAndView(Throwable $throwable) |
63
|
|
|
{ |
64
|
|
|
$pattern = '/Undefined variable: (.*?) \(View: (.*?)\)/'; |
65
|
|
|
|
66
|
|
|
preg_match($pattern, $throwable->getMessage(), $matches); |
67
|
|
|
if (count($matches) === 3) { |
68
|
|
|
[$string, $variableName, $viewFile] = $matches; |
|
|
|
|
69
|
|
|
|
70
|
|
|
return compact('variableName', 'viewFile'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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.