Completed
Push — master ( cb1113...b09b3a )
by Maarten
16s queued 11s
created

EditModelAction   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 139
Duplicated Lines 16.55 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 23
loc 139
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 37 4
A generateFillables() 0 8 1
A getFillableIndex() 0 10 3
A removeLeading() 0 10 2
A removeTrailing() 0 10 2
A endOfFillable() 0 4 1
A startOfFillable() 9 9 4
A getFillables() 14 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        return str_replace(' ', '', $line) === '];';
112
    }
113
114
    /**
115
     * @param $line
116
     * @return bool
117
     */
118 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...
119
    {
120
        $line = str_replace(' ', '', $line);
121
122
        return $line === '/**'
123
            || $line === '{'
124
            || $line === '];'
125
            || $line === '}';
126
    }
127
128
    /**
129
     * @param array $fields
130
     * @return string
131
     */
132 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...
133
    {
134
        $fillables = '';
135
136
        foreach ($fields as $index => $field) {
137
            if ($index === 0) {
138
                $fillables .= "'{$field}',";
139
            } else {
140
                $fillables .= PHP_EOL . str_repeat(' ', 8) . "'{$field}',";
141
            }
142
        }
143
144
        return $fillables;
145
    }
146
}
147