Passed
Push — master ( 84b36f...a99c1b )
by Iman
04:15
created

ScaffoldingParser::formOptions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
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
        if ($type == 'form') {
35
            $d = 'FORM';
36
        } elseif ($type == 'col') {
37
            $d = 'COLUMNS';
38
        }
39
40
        $cols = self::extract_unit($code, "# START $d DO NOT REMOVE THIS LINE", "# END $d DO NOT REMOVE THIS LINE");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $d does not seem to be defined for all execution paths leading up to this point.
Loading history...
41
        $cols = str_replace('"', "'", $cols);
42
        $cols = trim(str_replace('$this->'.$type.' = [];', '', $cols));
43
        $colsItem = explode('$this->'.$type.'[] = ', $cols);
44
45
        return array_filter($colsItem);
46
    }
47
48
    public static function extract_unit($string, $start, $end)
49
    {
50
        $pos = stripos($string, $start);
51
        $str = substr($string, $pos);
52
        $str_two = substr($str, strlen($start));
53
        $second_pos = stripos($str_two, $end);
54
        $str_three = substr($str_two, 0, $second_pos);
55
        $unit = trim($str_three); // remove whitespaces
56
57
        return $unit;
58
    }
59
60
    /**
61
     * @param $type
62
     * @param $colsItem
63
     * @return mixed
64
     */
65
    private static function formOptions($type, $colsItem)
66
    {
67
        foreach ($colsItem as $i => $form) {
68
            if ($type !== 'form') {
69
                continue;
70
            }
71
72
            if ($form['options']) {
73
                @eval("\$options = $form[options];");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
74
                @$colsItem[$i]['options'] = $options;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $options seems to be never defined.
Loading history...
75
            } else {
76
                $colsItem[$i]['options'] = [];
77
            }
78
        }
79
80
        return $colsItem;
81
82
    }
83
84
    /**
85
     * @param $s
86
     * @return array
87
     */
88
    private static function parseCallback($s)
89
    {
90
        $s = str_replace("return", "return ", $s);
91
        $val = trim(str_replace(["'callback'=>function(\$row) {", "'callback'=>function(\$row){"], "", $s));
92
        $val = substr($val, 0, -1);
93
94
        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...
95
    }
96
97
    /**
98
     * @param $split
99
     * @return array
100
     */
101
    private static function prepareFields($split)
102
    {
103
        $colInnerItem = [];
104
        foreach ($split as $s) {
105
            if (strpos($s, 'options') !== false) {
106
                $colInnerItem['options'] = trim(str_replace("'options'=>", "", $s), '\'\"\]\[');
107
                continue;
108
            }
109
            if (strpos($s, 'callback') !== false) {
110
                $colInnerItem['callback'] = self::parseCallback($s);
111
                continue;
112
            }
113
114
            $s = str_replace("'", '',$s);
115
            list($key, $val) = explode('=>', $s);
116
            $colInnerItem[$key] = $val;
117
118
        }
119
120
        return $colInnerItem;
121
    }
122
123
    /**
124
     * @param $item
125
     * @return mixed|string
126
     */
127
    private static function removeExtraCharacters($item)
128
    {
129
        $item = str_replace(' ', '', $item);
130
        // "['label'=>'KanapeType','name'=>'kanape_type',];\r\n"
131
132
        $item = str_replace('\',]', ']', $item); // replaces:  ',]  with   ]
133
        // "['label'=>'KanapeType','name'=>'kanape_type];\r\n"
134
135
        $item = trim($item);
136
        // "['label'=>'KanapeType','name'=>'kanape_type];"
137
138
        $item = trim($item, '[');
139
        // "'label'=>'KanapeType','name'=>'kanape_type];"
140
141
        $item = trim($item, '];');
142
        // "'label'=>'KanapeType','name'=>'kanape_type"
143
144
        $item = trim($item);
145
        $item = trim(preg_replace("/[\n\r\t]/", "", $item));
146
147
        return $item;
148
    }
149
150
    /**
151
     * @param $strSplit
152
     * @return mixed
153
     */
154
    private static function putDelimiter($strSplit)
155
    {
156
        $innerCount = 0;
157
        foreach ($strSplit as $index => $s) {
158
            if ($s == '[') {
159
                $innerCount++;
160
            }
161
            if ($s == ']') {
162
                $innerCount--;
163
            }
164
            if ($innerCount == 0 && $s == ',' && $strSplit[$index + 1] == "'") {
165
                $strSplit[$index] = "|SPLIT|";
166
            }
167
        }
168
169
        return $strSplit;
170
    }
171
}