1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facade\Ignition\Solutions; |
4
|
|
|
|
5
|
|
|
use Facade\IgnitionContracts\RunnableSolution; |
6
|
|
|
use Illuminate\Support\Facades\Blade; |
7
|
|
|
|
8
|
|
|
class MakeViewVariableOptionalSolution implements RunnableSolution |
9
|
|
|
{ |
10
|
|
|
/** @var string */ |
11
|
|
|
private $variableName; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $viewFile; |
15
|
|
|
|
16
|
|
|
public function __construct($variableName = null, $viewFile = null) |
17
|
|
|
{ |
18
|
|
|
$this->variableName = $variableName; |
19
|
|
|
$this->viewFile = $viewFile; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getSolutionTitle(): string |
23
|
|
|
{ |
24
|
|
|
return "$$this->variableName is undefined"; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getDocumentationLinks(): array |
28
|
|
|
{ |
29
|
|
|
return []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getSolutionActionDescription(): string |
33
|
|
|
{ |
34
|
|
|
$path = str_replace(base_path().'/', '', $this->viewFile); |
|
|
|
|
35
|
|
|
$output = [ |
36
|
|
|
'Make the variable optional in the blade template.', |
37
|
|
|
"Replace `{{ $$this->variableName }}` with `{{ $$this->variableName ?? '' }}`", |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
return implode(PHP_EOL, $output); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getRunButtonText(): string |
44
|
|
|
{ |
45
|
|
|
return 'Make variable optional'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getSolutionDescription(): string |
49
|
|
|
{ |
50
|
|
|
return ''; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getRunParameters(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'variableName' => $this->variableName, |
57
|
|
|
'viewFile' => $this->viewFile, |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function isRunnable(array $parameters = []) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return $this->makeOptional($this->getRunParameters()) !== false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function run(array $parameters = []) |
67
|
|
|
{ |
68
|
|
|
$output = $this->makeOptional($parameters); |
69
|
|
|
if ($output !== false) { |
70
|
|
|
file_put_contents($parameters['viewFile'], $output); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function makeOptional(array $parameters = []) |
75
|
|
|
{ |
76
|
|
|
$originalContents = file_get_contents($parameters['viewFile']); |
77
|
|
|
$newContents = str_replace('$'.$parameters['variableName'], '$'.$parameters['variableName']." ?? ''", $originalContents); |
78
|
|
|
|
79
|
|
|
$originalTokens = token_get_all(Blade::compileString($originalContents)); |
80
|
|
|
$newTokens = token_get_all(Blade::compileString($newContents)); |
81
|
|
|
|
82
|
|
|
$expectedTokens = $this->generateExpectedTokens($originalTokens, $parameters['variableName']); |
83
|
|
|
|
84
|
|
|
if ($expectedTokens !== $newTokens) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $newContents; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function generateExpectedTokens(array $originalTokens, string $variableName): array |
92
|
|
|
{ |
93
|
|
|
$expectedTokens = []; |
94
|
|
|
foreach ($originalTokens as $key => $token) { |
95
|
|
|
$expectedTokens[] = $token; |
96
|
|
|
if ($token[0] === T_VARIABLE && $token[1] === '$'.$variableName) { |
97
|
|
|
$expectedTokens[] = [T_WHITESPACE, ' ', $token[2]]; |
98
|
|
|
$expectedTokens[] = [T_COALESCE, '??', $token[2]]; |
99
|
|
|
$expectedTokens[] = [T_WHITESPACE, ' ', $token[2]]; |
100
|
|
|
$expectedTokens[] = [T_CONSTANT_ENCAPSED_STRING, "''", $token[2]]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $expectedTokens; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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.