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

MakeViewVariableOptionalSolution   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 99
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSolutionTitle() 0 4 1
A getDocumentationLinks() 0 4 1
A getSolutionActionDescription() 0 10 1
A getRunButtonText() 0 4 1
A getSolutionDescription() 0 4 1
A getRunParameters() 0 7 1
A isRunnable() 0 4 1
A run() 0 7 2
A makeOptional() 0 16 2
A generateExpectedTokens() 0 15 4
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
    /** @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);
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...
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 = [])
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...
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