Completed
Pull Request — master (#38)
by James
01:20
created

getSolutionTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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