GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SchemaParser   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 225
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A up() 0 4 1
A render() 9 9 2
A toArray() 0 4 1
A parse() 12 12 2
A getSchemas() 0 8 2
A getColumn() 0 6 1
A getAttributes() 0 6 2
A hasCustomAttribute() 0 4 1
A getCustomAttribute() 0 4 1
A createField() 0 9 2
A down() 9 9 2
A addColumn() 0 14 4
A removeColumn() 0 8 2

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 Milkmeowo\Framework\Repository\Generators\Migrations;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
7
class SchemaParser implements Arrayable
8
{
9
    /**
10
     * The array of custom attributes.
11
     *
12
     * @var array
13
     */
14
    protected $customAttributes = [
15
        'remember_token' => 'rememberToken()',
16
        'soft_delete'    => 'softDeletes()',
17
    ];
18
    /**
19
     * The migration schema.
20
     *
21
     * @var string
22
     */
23
    protected $schema;
24
25
    /**
26
     * Create new instance.
27
     *
28
     * @param string|null $schema
29
     */
30
    public function __construct($schema = null)
31
    {
32
        $this->schema = $schema;
33
    }
34
35
    /**
36
     * Render up migration fields.
37
     *
38
     * @return string
39
     */
40
    public function up()
41
    {
42
        return $this->render();
43
    }
44
45
    /**
46
     * Render the migration to formatted script.
47
     *
48
     * @return string
49
     */
50 View Code Duplication
    public function render()
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...
51
    {
52
        $results = '';
53
        foreach ($this->toArray() as $column => $attributes) {
54
            $results .= $this->createField($column, $attributes);
55
        }
56
57
        return $results;
58
    }
59
60
    /**
61
     * Convert string migration to array.
62
     *
63
     * @return array
64
     */
65
    public function toArray()
66
    {
67
        return $this->parse($this->schema);
68
    }
69
70
    /**
71
     * Parse a string to array of formatted schema.
72
     *
73
     * @param  string $schema
74
     *
75
     * @return array
76
     */
77 View Code Duplication
    public function parse($schema)
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...
78
    {
79
        $this->schema = $schema;
80
        $parsed = [];
81
        foreach ($this->getSchemas() as $schemaArray) {
82
            $column = $this->getColumn($schemaArray);
83
            $attributes = $this->getAttributes($column, $schemaArray);
84
            $parsed[$column] = $attributes;
85
        }
86
87
        return $parsed;
88
    }
89
90
    /**
91
     * Get array of schema.
92
     *
93
     * @return array
94
     */
95
    public function getSchemas()
96
    {
97
        if (is_null($this->schema)) {
98
            return [];
99
        }
100
101
        return explode(',', str_replace(' ', '', $this->schema));
102
    }
103
104
    /**
105
     * Get column name from schema.
106
     *
107
     * @param  string $schema
108
     *
109
     * @return string
110
     */
111
    public function getColumn($schema)
112
    {
113
        return array_first(explode(':', $schema), function ($key, $value) {
114
            return $value;
115
        });
116
    }
117
118
    /**
119
     * Get column attributes.
120
     *
121
     * @param  string $column
122
     * @param  string $schema
123
     *
124
     * @return array
125
     */
126
    public function getAttributes($column, $schema)
127
    {
128
        $fields = str_replace($column.':', '', $schema);
129
130
        return $this->hasCustomAttribute($column) ? $this->getCustomAttribute($column) : explode(':', $fields);
131
    }
132
133
    /**
134
     * Determinte whether the given column is exist in customAttributes array.
135
     *
136
     * @param  string $column
137
     *
138
     * @return bool
139
     */
140
    public function hasCustomAttribute($column)
141
    {
142
        return array_key_exists($column, $this->customAttributes);
143
    }
144
145
    /**
146
     * Get custom attributes value.
147
     *
148
     * @param  string $column
149
     *
150
     * @return array
151
     */
152
    public function getCustomAttribute($column)
153
    {
154
        return (array) $this->customAttributes[$column];
155
    }
156
157
    /**
158
     * Create field.
159
     *
160
     * @param  string $column
161
     * @param  array  $attributes
162
     *
163
     * @return string
164
     */
165
    public function createField($column, $attributes, $type = 'add')
166
    {
167
        $results = "\t\t\t".'$table';
168
        foreach ($attributes as $key => $field) {
169
            $results .= $this->{"{$type}Column"}($key, $field, $column);
170
        }
171
172
        return $results .= ';'.PHP_EOL;
173
    }
174
175
    /**
176
     * Render down migration fields.
177
     *
178
     * @return string
179
     */
180 View Code Duplication
    public function down()
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...
181
    {
182
        $results = '';
183
        foreach ($this->toArray() as $column => $attributes) {
184
            $results .= $this->createField($column, $attributes, 'remove');
185
        }
186
187
        return $results;
188
    }
189
190
    /**
191
     * Format field to script.
192
     *
193
     * @param  int    $key
194
     * @param  string $field
195
     * @param  string $column
196
     *
197
     * @return string
198
     */
199
    protected function addColumn($key, $field, $column)
200
    {
201
        if ($this->hasCustomAttribute($column)) {
202
            return '->'.$field;
203
        }
204
        if ($key == 0) {
205
            return '->'.$field."('".$column."')";
206
        }
207
        if (str_contains($field, '(')) {
208
            return '->'.$field;
209
        }
210
211
        return '->'.$field.'()';
212
    }
213
214
    /**
215
     * Format field to script.
216
     *
217
     * @param  int    $key
218
     * @param  string $field
219
     * @param  string $column
220
     *
221
     * @return string
222
     */
223
    protected function removeColumn($key, $field, $column)
0 ignored issues
show
Unused Code introduced by
The parameter $key 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...
224
    {
225
        if ($this->hasCustomAttribute($column)) {
226
            return '->'.$field;
227
        }
228
229
        return '->dropColumn('."'".$column."')";
230
    }
231
}
232