Completed
Push — master ( 8722ac...bad84f )
by Nicolas
08:01
created

NameParser   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 196
ccs 42
cts 42
cp 1
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAction() 0 4 1
A getOriginalName() 0 4 1
A getTableName() 0 6 1
A getMatches() 0 6 1
B getPattern() 0 21 8
A fetchData() 0 4 1
A getData() 0 4 1
A is() 0 4 1
A isAdd() 0 4 1
A isDelete() 0 4 1
A isCreate() 0 4 1
A isDrop() 0 4 1
1
<?php
2
3
namespace Nwidart\Modules\Support\Migrations;
4
5
class NameParser
6
{
7
    /**
8
     * The migration name.
9
     *
10
     * @var string
11
     */
12
    protected $name;
13
14
    /**
15
     * The array data.
16
     *
17
     * @var array
18
     */
19
    protected $data = [];
20
21
    /**
22
     * The available schema actions.
23
     *
24
     * @var array
25
     */
26
    protected $actions = [
27
        'create' => [
28
            'create',
29
            'make',
30
        ],
31
        'delete' => [
32
            'delete',
33
            'remove',
34
        ],
35
        'add' => [
36
            'add',
37
            'update',
38
            'append',
39
            'insert',
40
        ],
41
        'drop' => [
42
            'destroy',
43
            'drop',
44
        ],
45
    ];
46
47
    /**
48
     * The constructor.
49
     *
50
     * @param string $name
51
     */
52 14
    public function __construct($name)
53
    {
54 14
        $this->name = $name;
55 14
        $this->data = $this->fetchData();
56 14
    }
57
58
    /**
59
     * Get original migration name.
60
     *
61
     * @return string
62
     */
63 1
    public function getOriginalName()
64
    {
65 1
        return $this->name;
66
    }
67
68
    /**
69
     * Get schema type or action.
70
     *
71
     * @return string
72
     */
73 12
    public function getAction()
74
    {
75 12
        return head($this->data);
76
    }
77
78
    /**
79
     * Get the table will be used.
80
     *
81
     * @return string
82
     */
83 3
    public function getTableName()
84
    {
85 3
        $matches = array_reverse($this->getMatches());
86
87 3
        return array_shift($matches);
88
    }
89
90
    /**
91
     * Get matches data from regex.
92
     *
93
     * @return array
94
     */
95 4
    public function getMatches()
96
    {
97 4
        preg_match($this->getPattern(), $this->name, $matches);
98
99 4
        return $matches;
100
    }
101
102
    /**
103
     * Get name pattern.
104
     *
105
     * @return string
106
     */
107 5
    public function getPattern()
108
    {
109 5
        switch ($action = $this->getAction()) {
110 5
            case 'add':
111 5
            case 'append':
112 5
            case 'update':
113 5
            case 'insert':
114 1
                return "/{$action}_(.*)_to_(.*)_table/";
115
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
116
117 5
            case 'delete':
118 5
            case 'remove':
119 5
            case 'alter':
120 1
                return "/{$action}_(.*)_from_(.*)_table/";
121
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
122
123 5
            default:
124 5
                return "/{$action}_(.*)_table/";
125
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
126 5
        }
127
    }
128
129
    /**
130
     * Fetch the migration name to an array data.
131
     *
132
     * @return array
133
     */
134 14
    protected function fetchData()
135
    {
136 14
        return explode('_', $this->name);
137
    }
138
139
    /**
140
     * Get the array data.
141
     *
142
     * @return array
143
     */
144 1
    public function getData()
145
    {
146 1
        return $this->data;
147
    }
148
149
    /**
150
     * Determine whether the given type is same with the current schema action or type.
151
     *
152
     * @param $type
153
     *
154
     * @return bool
155
     */
156 1
    public function is($type)
157
    {
158 1
        return $type === $this->getAction();
159
    }
160
161
    /**
162
     * Determine whether the current schema action is a adding action.
163
     *
164
     * @return bool
165
     */
166 1
    public function isAdd()
167
    {
168 1
        return in_array($this->getAction(), $this->actions['add']);
169
    }
170
171
    /**
172
     * Determine whether the current schema action is a deleting action.
173
     *
174
     * @return bool
175
     */
176 1
    public function isDelete()
177
    {
178 1
        return in_array($this->getAction(), $this->actions['delete']);
179
    }
180
181
    /**
182
     * Determine whether the current schema action is a creating action.
183
     *
184
     * @return bool
185
     */
186 3
    public function isCreate()
187
    {
188 3
        return in_array($this->getAction(), $this->actions['create']);
189
    }
190
191
    /**
192
     * Determine whether the current schema action is a dropping action.
193
     *
194
     * @return bool
195
     */
196 1
    public function isDrop()
197
    {
198 1
        return in_array($this->getAction(), $this->actions['drop']);
199
    }
200
}
201