EditModelAction::startOfFillable()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Actions\Model;
4
5
use Illuminate\Support\Facades\File;
6
use ReflectionClass;
7
8
class EditModelAction
9
{
10
    /**
11
     * @param $request
12
     * @return void
13
     * @throws \ReflectionException
14
     */
15
    public function execute($request)
16
    {
17
        $file = (new ReflectionClass($request['model']))->getFileName();
18
        $lines = file($file, FILE_IGNORE_NEW_LINES);
19
        $index = $this->getFillableIndex($lines) - 1;
20
21
        if ($index > 0) {
22
            $fields = array_values(array_map(static function ($field) {
23
                return $field['name'];
24
            }, $request['fields']));
25
            $stub = __DIR__ . '/../../../resources/stubs/fillables.stub';
26
            $removeLines = array_merge(
27
                $this->removeTrailing($lines, $index),
28
                $this->removeLeading($lines, $index + 1)
29
            );
30
31
            sort($removeLines);
32
            $injectionLine = $removeLines[0];
33
34
            foreach ($removeLines as $index => $removeLine) {
35
                if (in_array(str_replace(' ', '', $lines[$removeLine]), ['{', '}'])) {
36
                    $injectionLine++;
37
                    continue;
38
                }
39
40
                unset($lines[$removeLine]);
41
            }
42
43
            $lines[$injectionLine] = rtrim($this->generateFillables($fields, $stub));
44
            ksort($lines);
45
            file_put_contents($file, implode("\n", $lines));
46
47
            return;
48
        }
49
50
        abort(422, 'Cannot replace non-existing $fillable');
51
    }
52
53
    private function generateFillables($fields, $stub)
54
    {
55
        return str_replace([
56
            '$fillables$'
57
        ], [
58
            $this->getFillables($fields),
59
        ], File::get($stub));
60
    }
61
62
    private function getFillableIndex(array $lines)
63
    {
64
        foreach ($lines as $index => $line) {
65
            if (strpos($line, '$fillable') !== false) {
66
                return $index;
67
            }
68
        }
69
70
        return -1;
71
    }
72
73
    /**
74
     * @param $lines
75
     * @param $index
76
     * @return array
77
     */
78
    private function removeLeading($lines, $index): array
79
    {
80
        $remove = [];
81
82
        while (! $this->startOfFillable($lines[$index--])) {
83
            $remove[] = $index;
84
        }
85
86
        return $remove;
87
    }
88
89
    /**
90
     * @param $lines
91
     * @param $index
92
     * @return array
93
     */
94
    private function removeTrailing($lines, $index): array
95
    {
96
        $remove = [];
97
98
        while (! $this->endOfFillable($lines[$index++])) {
99
            $remove[] = $index;
100
        }
101
102
        return $remove;
103
    }
104
105
    /**
106
     * @param $line
107
     * @return bool
108
     */
109
    private function endOfFillable($line): bool
110
    {
111
        $stripped = str_replace(' ', '', $line);
112
113
        return $stripped === '];' || $stripped === '';
114
    }
115
116
    /**
117
     * @param $line
118
     * @return bool
119
     */
120 View Code Duplication
    private function startOfFillable($line): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        $line = str_replace(' ', '', $line);
123
124
        return $line === '/**'
125
            || $line === '{'
126
            || $line === '];'
127
            || $line === '}';
128
    }
129
130
    /**
131
     * @param array $fields
132
     * @return string
133
     */
134 View Code Duplication
    private function getFillables(array $fields): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        $fillables = '';
137
138
        foreach ($fields as $index => $field) {
139
            if ($index === 0) {
140
                $fillables .= "'{$field}',";
141
            } else {
142
                $fillables .= PHP_EOL . str_repeat(' ', 8) . "'{$field}',";
143
            }
144
        }
145
146
        return $fillables;
147
    }
148
}
149