Completed
Push — master ( e2aab6...02a6e4 )
by Marcel
01:24
created

getRunParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    /** @var string */
11
    private $variableName;
12
13
    /** @var string */
14
    private $viewFile;
15
16
    public function __construct($variableName = null, $viewFile = null, $suggested = null)
17
    {
18
        $this->variableName = $variableName;
19
        $this->viewFile = $viewFile;
20
        $this->suggested = $suggested;
0 ignored issues
show
Bug introduced by
The property suggested does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
23
    public function getSolutionTitle(): string
24
    {
25
        return 'Possible typo $'.$this->variableName;
26
    }
27
28
    public function getDocumentationLinks(): array
29
    {
30
        return [];
31
    }
32
33
    public function getSolutionActionDescription(): string
34
    {
35
        $path = str_replace(base_path().'/', '', $this->viewFile);
0 ignored issues
show
Unused Code introduced by
$path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
36
37
        return "Did you mean `$$this->suggested`?";
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 = []): bool
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        return $this->fixTypo($this->getRunParameters()) !== false;
62
    }
63
64
    public function run(array $parameters = []): void
65
    {
66
        $output = $this->fixTypo($parameters);
67
        if ($output === false) {
68
            return;
69
        }
70
71
        file_put_contents($parameters['viewFile'], $output);
72
    }
73
74
    protected function fixTypo(array $parameters = [])
75
    {
76
        if (! $this->isAlphaNumericWithUnderscore($parameters['suggested'])) {
77
            return false;
78
        }
79
80
        $originalContents = file_get_contents($parameters['viewFile']);
81
        $newContents = str_replace('$'.$parameters['variableName'], '$'.$parameters['suggested'], $originalContents);
82
83
        $originalTokens = token_get_all(Blade::compileString($originalContents));
84
        $newTokens = token_get_all(Blade::compileString($newContents));
85
86
        $expectedTokens = $this->generateExpectedTokens($originalTokens, $parameters['variableName'], $parameters['suggested']);
87
88
        if ($expectedTokens !== $newTokens) {
89
            return false;
90
        }
91
92
        return $newContents;
93
    }
94
95
    protected function isAlphaNumericWithUnderscore(string $input): bool
96
    {
97
        return preg_match('/^[a-zA-Z]+[a-zA-Z0-9_]+$/', $input);
98
    }
99
100
    protected function generateExpectedTokens(array $originalTokens, string $variableName, string $suggested): array
101
    {
102
        $expectedTokens = $originalTokens;
103
        foreach ($expectedTokens as $key => $token) {
104
            if ($token[0] === T_VARIABLE && $token[1] === '$'.$variableName) {
105
                $expectedTokens[$key][1] = "$$suggested";
106
            }
107
        }
108
109
        return $expectedTokens;
110
    }
111
}
112