|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Facade\Ignition\Solutions; |
|
4
|
|
|
|
|
5
|
|
|
use Facade\IgnitionContracts\RunnableSolution; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
class FixMissingSemicolonSolution implements RunnableSolution |
|
9
|
|
|
{ |
|
10
|
|
|
private $filePath; |
|
11
|
|
|
private $lineNumber; |
|
12
|
|
|
private $unexpected; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct($filePath = null, $lineNumber = null, $unexpected = null) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->filePath = $filePath; |
|
17
|
|
|
$this->lineNumber = $lineNumber; |
|
18
|
|
|
$this->unexpected = $unexpected; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function getSolutionTitle(): string |
|
22
|
|
|
{ |
|
23
|
|
|
return 'You are missing a semicolon.'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getDocumentationLinks(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return []; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getSolutionActionDescription(): string |
|
32
|
|
|
{ |
|
33
|
|
|
return 'Each instruction requires termination with a semicolon.'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getRunButtonText(): string |
|
37
|
|
|
{ |
|
38
|
|
|
return 'Insert missing semicolon'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getSolutionDescription(): string |
|
42
|
|
|
{ |
|
43
|
|
|
return ''; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getRunParameters(): array |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
'filePath' => $this->filePath, |
|
50
|
|
|
'lineNumber' => $this->lineNumber, |
|
51
|
|
|
'unexpected' => $this->unexpected, |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function isRunnable() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->insertSemicolon($this->getRunParameters()) !== false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function run(array $parameters = []) |
|
61
|
|
|
{ |
|
62
|
|
|
$output = $this->insertSemicolon($parameters); |
|
63
|
|
|
if ($output !== false) { |
|
64
|
|
|
file_put_contents(app_path().$parameters['filePath'], $output); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function insertSemicolon(array $parameters = []) |
|
69
|
|
|
{ |
|
70
|
|
|
if (strpos($parameters['filePath'], 'ignition/tests/stubs') !== false) { |
|
71
|
|
|
$file = $parameters['filePath']; |
|
72
|
|
|
} else { |
|
73
|
|
|
$file = app_path().$parameters['filePath']; |
|
74
|
|
|
} |
|
75
|
|
|
if (! is_file($file)) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
$contents = file_get_contents($file); |
|
79
|
|
|
$tokens = token_get_all($contents); |
|
80
|
|
|
|
|
81
|
|
|
$reverseTokens = array_reverse($tokens); |
|
82
|
|
|
|
|
83
|
|
|
$output = []; |
|
84
|
|
|
$insertSemicolon = false; |
|
85
|
|
|
$line = 0; |
|
86
|
|
|
foreach ($reverseTokens as $token) { |
|
87
|
|
|
$char = isset($token[1]) ? $token[1] : $token; |
|
88
|
|
|
|
|
89
|
|
|
$output[] = $char; |
|
90
|
|
|
|
|
91
|
|
|
if (isset($token[2])) { |
|
92
|
|
|
$line = $token[2]; |
|
93
|
|
|
} |
|
94
|
|
|
if (is_string($token) && $line == $parameters['lineNumber'] && $char == $parameters['unexpected']) { |
|
95
|
|
|
$insertSemicolon = true; |
|
96
|
|
|
} |
|
97
|
|
|
if ($insertSemicolon && isset($token[0]) && $token[0] == T_WHITESPACE) { |
|
98
|
|
|
$insertSemicolon = false; |
|
99
|
|
|
$output[] = ';'; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
$proposedFix = implode('', array_reverse($output)); |
|
103
|
|
|
|
|
104
|
|
|
$result = exec(sprintf('echo %s | php -l', escapeshellarg($proposedFix)), $output, $exit); |
|
105
|
|
|
|
|
106
|
|
|
if (Str::contains($result, 'No syntax errors')) { |
|
107
|
|
|
return $proposedFix; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|