Completed
Pull Request — master (#42)
by James
14:20
created

UpdateViewNameSolution::updateViewName()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.0995
c 0
b 0
f 0
cc 8
nc 6
nop 1
1
<?php
2
3
namespace Facade\Ignition\Solutions;
4
5
use Facade\IgnitionContracts\RunnableSolution;
6
7
class UpdateViewNameSolution implements RunnableSolution
8
{
9
    private $missingView;
10
    private $suggestedView;
11
12
    public function __construct($missingView = null, $suggestedView = null, $controllerPath = null)
13
    {
14
        $this->missingView = $missingView;
15
        $this->suggestedView = $suggestedView;
16
        $this->controllerPath = $controllerPath;
0 ignored issues
show
Bug introduced by
The property controllerPath 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...
17
    }
18
19
    public function getSolutionTitle(): string
20
    {
21
        return $this->missingView.' was not found.';
22
    }
23
24
    public function getDocumentationLinks(): array
25
    {
26
        return [];
27
    }
28
29
    public function getSolutionActionDescription(): string
30
    {
31
        return 'Did you mean `'.$this->suggestedView.'`?';
32
    }
33
34
    public function getRunButtonText(): string
35
    {
36
        return 'Update view name';
37
    }
38
39
    public function getSolutionDescription(): string
40
    {
41
        return '';
42
    }
43
44
    public function getRunParameters(): array
45
    {
46
        return [
47
            'missingView' => $this->missingView,
48
            'suggestedView' => $this->suggestedView,
49
            'controllerPath' => $this->controllerPath,
50
        ];
51
    }
52
53
    public function isRunnable()
54
    {
55
        return $this->updateViewName($this->getRunParameters()) !== false;
56
    }
57
58
    public function run(array $parameters = [])
59
    {
60
        $output = $this->updateViewName($parameters);
61
        if ($output !== false) {
62
            file_put_contents(app_path().$parameters['controllerPath'], $output);
63
        }
64
    }
65
66
    public function updateViewName(array $parameters = [])
67
    {
68
        if (strpos($parameters['controllerPath'], 'ignition/tests/Solutions') !== false) {
69
            $file = $parameters['controllerPath'];
70
        } else {
71
            $file = app_path().$parameters['controllerPath'];
72
        }
73
        if (! is_file($file)) {
74
            return false;
75
        }
76
        $contents = file_get_contents($file);
77
        $tokens = token_get_all($contents);
78
        $expectedTokens = collect($tokens)->map(function ($token) use ($parameters) {
79
            if ($token[0] === T_CONSTANT_ENCAPSED_STRING && (
80
                $token[1] == "'".$parameters['missingView']."'" ||
81
                $token[1] == '"'.$parameters['missingView'].'"'
82
            )) {
83
                $token[1] = "'".$parameters['suggestedView']."'";
84
            }
85
86
            return $token;
87
        })->toArray();
88
89
        $newContents = collect($expectedTokens)->map(function ($token) {
90
            return is_array($token) ? $token[1] : $token;
91
        })->implode('');
92
93
        $newTokens = token_get_all($newContents);
94
95
        // If we're generating a file that doesn't mean the same thing then don't allow
96
        if ($expectedTokens !== $newTokens) {
97
            return false;
98
        }
99
100
        return $newContents;
101
    }
102
}
103