|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Facade\Ignition\Solutions; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Blade; |
|
6
|
|
|
use Facade\IgnitionContracts\RunnableSolution; |
|
7
|
|
|
|
|
8
|
|
|
class SuggestCorrectVariableNameSolution implements RunnableSolution |
|
9
|
|
|
{ |
|
10
|
|
|
private $variableName; |
|
11
|
|
|
private $viewFile; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct($variableName = null, $viewFile = null, $suggested = null) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->variableName = $variableName; |
|
16
|
|
|
$this->viewFile = $viewFile; |
|
17
|
|
|
$this->suggested = $suggested; |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function getSolutionTitle(): string |
|
21
|
|
|
{ |
|
22
|
|
|
return 'Possible typo $'.$this->variableName; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function getDocumentationLinks(): array |
|
26
|
|
|
{ |
|
27
|
|
|
return []; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getSolutionActionDescription(): string |
|
31
|
|
|
{ |
|
32
|
|
|
$path = str_replace(base_path().'/', '', $this->viewFile); |
|
|
|
|
|
|
33
|
|
|
$output = [ |
|
34
|
|
|
'Did you mean `$'.$this->suggested.'`?', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
return implode(PHP_EOL, $output); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getRunButtonText(): string |
|
41
|
|
|
{ |
|
42
|
|
|
return 'Fix typo'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getSolutionDescription(): string |
|
46
|
|
|
{ |
|
47
|
|
|
return ''; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getRunParameters(): array |
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
|
|
'variableName' => $this->variableName, |
|
54
|
|
|
'viewFile' => $this->viewFile, |
|
55
|
|
|
'suggested' => $this->suggested, |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function isRunnable(array $parameters = []) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
return $this->fixTypo($this->getRunParameters()) !== false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function run(array $parameters = []) |
|
65
|
|
|
{ |
|
66
|
|
|
$output = $this->fixTypo($parameters); |
|
67
|
|
|
if ($output !== false) { |
|
68
|
|
|
file_put_contents($parameters['viewFile'], $output); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function fixTypo(array $parameters = []) |
|
73
|
|
|
{ |
|
74
|
|
|
// Make sure suggested variable is valid alpha-numeric with underscore, or return false |
|
75
|
|
|
if (! preg_match('/^[a-zA-Z]+[a-zA-Z0-9_]+$/', $parameters['suggested'])) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$originalContents = file_get_contents($parameters['viewFile']); |
|
80
|
|
|
$newContents = str_replace('$'.$parameters['variableName'], '$'.$parameters['suggested'], $originalContents); |
|
81
|
|
|
|
|
82
|
|
|
$originalTokens = token_get_all(Blade::compileString($originalContents)); |
|
83
|
|
|
$newTokens = token_get_all(Blade::compileString($newContents)); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$expectedTokens = $this->generateExpectedTokens($originalTokens, $parameters['variableName'], $parameters['suggested']); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
return $newContents; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function generateExpectedTokens(array $originalTokens, string $variableName, string $suggested): array |
|
91
|
|
|
{ |
|
92
|
|
|
$expectedTokens = $originalTokens; |
|
93
|
|
|
foreach ($expectedTokens as $key => $token) { |
|
94
|
|
|
if ($token[0] === T_VARIABLE && $token[1] === '$'.$variableName) { |
|
95
|
|
|
$expectedTokens[$key][1] = '$'.$suggested; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return $expectedTokens; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: