|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Facade\Ignition\Solutions; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Blade; |
|
6
|
|
|
use Facade\IgnitionContracts\RunnableSolution; |
|
7
|
|
|
|
|
8
|
|
|
class MakeViewVariableOptionalSolution implements RunnableSolution |
|
9
|
|
|
{ |
|
10
|
|
|
private $variableName; |
|
11
|
|
|
private $viewFile; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct($variableName = null, $viewFile = null) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->variableName = $variableName; |
|
16
|
|
|
$this->viewFile = $viewFile; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function getSolutionTitle(): string |
|
20
|
|
|
{ |
|
21
|
|
|
return '$'.$this->variableName.' is undefined'; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getDocumentationLinks(): array |
|
25
|
|
|
{ |
|
26
|
|
|
return []; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getSolutionActionDescription(): string |
|
30
|
|
|
{ |
|
31
|
|
|
$path = str_replace(base_path().'/', '', $this->viewFile); |
|
|
|
|
|
|
32
|
|
|
$output = [ |
|
33
|
|
|
'Make the variable optional in the blade template.', |
|
34
|
|
|
'Replace `{{ $'.$this->variableName.' }}` with `{{ $'.$this->variableName.' ?? \'\' }}`', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
return implode(PHP_EOL, $output); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getRunButtonText(): string |
|
41
|
|
|
{ |
|
42
|
|
|
return 'Make variable optional'; |
|
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
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function isRunnable(array $parameters = []) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
return $this->makeOptional($this->getRunParameters()) !== false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function run(array $parameters = []) |
|
64
|
|
|
{ |
|
65
|
|
|
$output = $this->makeOptional($parameters); |
|
66
|
|
|
if ($output !== false) { |
|
67
|
|
|
file_put_contents($parameters['viewFile'], $output); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function makeOptional(array $parameters = []) |
|
72
|
|
|
{ |
|
73
|
|
|
$originalContents = file_get_contents($parameters['viewFile']); |
|
74
|
|
|
$newContents = str_replace('$'.$parameters['variableName'], '$'.$parameters['variableName']." ?? ''", $originalContents); |
|
75
|
|
|
|
|
76
|
|
|
$originalTokens = token_get_all(Blade::compileString($originalContents)); |
|
77
|
|
|
$newTokens = token_get_all(Blade::compileString($newContents)); |
|
78
|
|
|
|
|
79
|
|
|
$expectedTokens = $this->generateExpectedTokens($originalTokens, $parameters['variableName']); |
|
80
|
|
|
|
|
81
|
|
|
if ($expectedTokens !== $newTokens) { |
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $newContents; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function generateExpectedTokens(array $originalTokens, string $variableName): array |
|
89
|
|
|
{ |
|
90
|
|
|
$expectedTokens = []; |
|
91
|
|
|
foreach ($originalTokens as $key => $token) { |
|
92
|
|
|
$expectedTokens[] = $token; |
|
93
|
|
|
if ($token[0] === T_VARIABLE && $token[1] === '$'.$variableName) { |
|
94
|
|
|
$expectedTokens[] = [T_WHITESPACE, ' ', $token[2]]; |
|
95
|
|
|
$expectedTokens[] = [T_COALESCE, '??', $token[2]]; |
|
96
|
|
|
$expectedTokens[] = [T_WHITESPACE, ' ', $token[2]]; |
|
97
|
|
|
$expectedTokens[] = [T_CONSTANT_ENCAPSED_STRING, "''", $token[2]]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $expectedTokens; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.