Completed
Pull Request — master (#42)
by James
01:26
created

UpdateViewNameSolution   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSolutionTitle() 0 4 1
A getDocumentationLinks() 0 4 1
A getSolutionActionDescription() 0 4 1
A getRunButtonText() 0 4 1
A getSolutionDescription() 0 4 1
A getRunParameters() 0 8 1
A isRunnable() 0 4 1
A updateViewName() 0 25 4
A run() 0 7 3
B getProposedFileFromTokens() 0 27 9
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 && $output !== '') {
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/stubs') !== 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
79
        $contents = $this->getProposedFileFromTokens(
80
            $tokens,
81
            $parameters['missingView'],
82
            $parameters['suggestedView']
83
        );
84
85
        if ($contents === false) {
86
            return false;
87
        }
88
89
        return $contents;
90
    }
91
92
    protected function getProposedFileFromTokens(array $tokens, string $missingView, string $suggestedView)
93
    {
94
        $viewKey = 0;
95
        $expectedTokens = collect($tokens)->map(function ($token, $key) use ($missingView, $suggestedView, &$viewKey) {
96
            if ($token[0] == T_STRING && $token[1] == 'view') {
97
                $viewKey = $key;
98
            }
99
            if ($token[0] === T_CONSTANT_ENCAPSED_STRING && ($viewKey == ($key - 2)) && (
100
                $token[1] == "'$missingView'" ||
101
                $token[1] == '"'.$missingView.'"'
102
            )) {
103
                $token[1] = "'$suggestedView'";
104
            }
105
            return $token;
106
        })->toArray();
107
        $newContents = collect($expectedTokens)->map(function ($token) {
108
            return is_array($token) ? $token[1] : $token;
109
        })->implode('');
110
111
        $newTokens = token_get_all($newContents);
112
113
        if ($expectedTokens !== $newTokens) {
114
            return false;
115
        }
116
117
        return $newContents;
118
    }
119
}
120