Passed
Push — master ( e9dde7...b5e495 )
by Iman
03:56
created

ScaffoldingParser   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 163
rs 10
c 0
b 0
f 0
wmc 24

8 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 18 3
A extract_unit() 0 10 1
A parseCallback() 0 7 1
A removeExtraCharacters() 0 21 1
A prepareFields() 0 20 4
A extractLines() 0 13 2
B putDelimiter() 0 16 7
B formOptions() 0 15 5
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator;
4
5
class ScaffoldingParser
6
{
7
    public static function parse($code, $type = 'form')
8
    {
9
        $colsItem = self::extractLines($code, $type);
10
11
        foreach ($colsItem as &$item) {
12
            $item = self::removeExtraCharacters($item);
13
            $strSplit = str_split($item);
14
            $strSplit = self::putDelimiter($strSplit);
15
            $item = implode("", $strSplit);
16
        }
17
18
        foreach ($colsItem as &$col) {
19
            $col = self::prepareFields(explode('|SPLIT|', $col));
20
        }
21
22
        $colsItem = self::formOptions($type, $colsItem);
23
24
        return $colsItem;
25
    }
26
27
    /**
28
     * @param $code
29
     * @param $type
30
     * @return array
31
     */
32
    private static function extractLines($code, $type)
33
    {
34
        $mark = 'COLUMNS';
35
        if ($type == 'form') {
36
            $mark = 'FORM';
37
        }
38
39
        $cols = self::extract_unit($code, cbStartMarker($mark), cbEndMarker($mark));
40
        $cols = str_replace('"', "'", $cols);
41
        $cols = trim(str_replace('$this->'.$type.' = [];', '', $cols));
42
        $colsItem = explode('$this->'.$type.'[] = ', $cols);
43
44
        return array_filter($colsItem);
45
    }
46
47
    public static function extract_unit($string, $start, $end)
48
    {
49
        $pos = stripos($string, $start);
50
        $str = substr($string, $pos);
51
        $str_two = substr($str, strlen($start));
52
        $second_pos = stripos($str_two, $end);
53
        $str_three = substr($str_two, 0, $second_pos);
54
        $unit = trim($str_three); // remove whitespaces
55
56
        return $unit;
57
    }
58
59
    /**
60
     * @param $type
61
     * @param $colsItem
62
     * @return mixed
63
     */
64
    private static function formOptions($type, $colsItem)
65
    {
66
        if ($type !== 'form') {
67
            return $colsItem;
68
        }
69
        foreach ($colsItem as $i => $form) {
70
            if ( empty($form['options']) == false && $form['options'] !== '[]') {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
71
                @eval("\$options = $form[options];");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
72
                @$colsItem[$i]['options'] = $options;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $options seems to be never defined.
Loading history...
73
            } else {
74
                $colsItem[$i]['options'] = [];
75
            }
76
        }
77
78
        return $colsItem;
79
80
    }
81
82
    /**
83
     * @param $s
84
     * @return array
85
     */
86
    private static function parseCallback($s)
87
    {
88
        $s = str_replace("return", "return ", $s);
89
        $val = trim(str_replace(["'callback'=>function(\$row) {", "'callback'=>function(\$row){"], "", $s));
90
        $val = substr($val, 0, -1);
91
92
        return $val; //to remove last }
0 ignored issues
show
Bug Best Practice introduced by
The expression return $val returns the type string which is incompatible with the documented return type array.
Loading history...
93
    }
94
95
    /**
96
     * @param $split
97
     * @return array
98
     */
99
    private static function prepareFields($split)
100
    {
101
        $colInnerItem = [];
102
        foreach ($split as $s) {
103
            if (strpos($s, 'options') !== false) {
104
                $colInnerItem['options'] = trim(str_replace("'options'=>", "", $s));
105
                continue;
106
            }
107
            if (strpos($s, 'callback') !== false) {
108
                $colInnerItem['callback'] = self::parseCallback($s);
109
                continue;
110
            }
111
112
            $s = str_replace("'", '',$s);
113
            list($key, $val) = explode('=>', $s);
114
            $colInnerItem[$key] = $val;
115
116
        }
117
118
        return $colInnerItem;
119
    }
120
121
    /**
122
     * @param $item
123
     * @return mixed|string
124
     */
125
    private static function removeExtraCharacters($item)
126
    {
127
        $item = str_replace(' ', '', $item);
128
        // "['label'=>'KanapeType','name'=>'kanape_type',];\r\n"
129
130
        $item = str_replace('\',]', ']', $item); // replaces:  ',]  with   ]
131
        // "['label'=>'KanapeType','name'=>'kanape_type];\r\n"
132
133
        $item = trim($item);
134
        // "['label'=>'KanapeType','name'=>'kanape_type];"
135
136
        $item = trim($item, '[');
137
        // "'label'=>'KanapeType','name'=>'kanape_type];"
138
139
        $item = trim($item, '];');
140
        // "'label'=>'KanapeType','name'=>'kanape_type"
141
142
        $item = trim($item);
143
        $item = trim(preg_replace("/[\n\r\t]/", "", $item));
144
145
        return $item;
146
    }
147
148
    /**
149
     * @param $strSplit
150
     * @return mixed
151
     */
152
    private static function putDelimiter($strSplit)
153
    {
154
        $innerCount = 0;
155
        foreach ($strSplit as $index => $s) {
156
            if ($s == '[') {
157
                $innerCount++;
158
            }
159
            if ($s == ']') {
160
                $innerCount--;
161
            }
162
            if ($innerCount == 0 && $s == ',' && $strSplit[$index + 1] == "'") {
163
                $strSplit[$index] = "|SPLIT|";
164
            }
165
        }
166
167
        return $strSplit;
168
    }
169
}