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

SuggestImportSolution::importClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
namespace Facade\Ignition\Solutions;
4
5
use Facade\IgnitionContracts\RunnableSolution;
6
7
class SuggestImportSolution implements RunnableSolution
8
{
9
    /** @var string */
10
    protected $class;
11
12
    /** @var string */
13
    protected $file;
14
15
    public function __construct(string $class = null, string $file = null)
16
    {
17
        $this->class = $class;
18
        $this->file = $file;
19
    }
20
21
    public function getSolutionTitle(): string
22
    {
23
        return 'A class import is missing';
24
    }
25
26
    public function getSolutionDescription(): string
27
    {
28
        return '';
29
    }
30
31
    public function getSolutionActionDescription(): string
32
    {
33
        $output = [
34
            'You have a missing class import. Try importing this class: `'.$this->class.'`.',
35
        ];
36
37
        return implode(PHP_EOL, $output);
38
    }
39
40
    public function getRunButtonText(): string
41
    {
42
        return 'Import class';
43
    }
44
45
    public function getDocumentationLinks(): array
46
    {
47
        return [];
48
    }
49
50
    public function getRunParameters(): array
51
    {
52
        return [
53
            'class' => $this->class,
54
            'file' => $this->file,
55
        ];
56
    }
57
58
    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...
59
    {
60
        return true;
61
        //return $this->makeOptional($this->getRunParameters()) !== false;
62
    }
63
64
    public function run(array $parameters = [])
65
    {
66
        $output = $this->importClass($parameters);
67
        if ($output !== false) {
68
            file_put_contents(app_path().'/'.$parameters['file'], $output);
69
        }
70
    }
71
72
    public function importClass(array $parameters = [])
73
    {
74
        $originalContents = file_get_contents(app_path().'/'.$parameters['file']);
75
        $newContents = preg_replace('/use /', 'use '.preg_quote($parameters['class']).";\nuse ", $originalContents, 1);
76
77
        return $newContents;
78
        // $originalContents = file_get_contents($parameters['viewFile']);
79
        // $newContents = str_replace('$'.$parameters['variableName'], '$'.$parameters['variableName']." ?? ''", $originalContents);
80
        // // Compile blade, tokenize
81
        // $originalTokens = token_get_all(Blade::compileString($originalContents));
82
        // $newTokens = token_get_all(Blade::compileString($newContents));
83
        // // Generate what we expect the tokens to be after we change the blade file
84
        // $expectedTokens = [];
85
        // foreach ($originalTokens as $key => $token) {
86
        //     $expectedTokens[] = $token;
87
        //     if ($token[0] === T_VARIABLE && $token[1] === '$'.$parameters['variableName']) {
88
        //         $expectedTokens[] = [T_WHITESPACE, ' ', $token[2]];
89
        //         $expectedTokens[] = [T_COALESCE, '??', $token[2]];
90
        //         $expectedTokens[] = [T_WHITESPACE, ' ', $token[2]];
91
        //         $expectedTokens[] = [T_CONSTANT_ENCAPSED_STRING, "''", $token[2]];
92
        //     }
93
        // }
94
        // if ($expectedTokens !== $newTokens) {
95
        //     return false;
96
        // }
97
        // return $newContents;
98
    }
99
}
100