Completed
Push — master ( a63bc5...f003cc )
by Iman
17s
created

ScaffoldingParser::removeExtraCharacters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator;
4
5
class ScaffoldingParser
6
{
7
    static function parse($code, $type = 'form')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
8
    {
9
        $colsItem = self::extractLines($code, $type);
10
11
        foreach ($colsItem as &$item) {
12
            $item = self::removeExtraCharacters($item);
13
            $strSplit = str_split($item);
14
            $innerCount = 0;
15
            foreach ($strSplit as $index => $s) {
16
                if ($s == '[') {
17
                    $innerCount++;
18
                }
19
                if ($s == ']') {
20
                    $innerCount--;
21
                }
22
                if ($innerCount == 0 && $s == ',' && $strSplit[$index + 1] == "'") {
23
                    $strSplit[$index] = "|SPLIT|";
24
                }
25
            }
26
            $item = implode("", $strSplit);
27
        }
28
29
        foreach ($colsItem as &$col) {
30
            $col = self::prepareFields(explode('|SPLIT|', $col));
31
        }
32
33
        self::formOptions($type, $colsItem);
34
35
        return $colsItem;
36
    }
37
38
    /**
39
     * @param $code
40
     * @param $type
41
     * @return array
42
     */
43
    private static function extractLines($code, $type)
44
    {
45
        if ($type == 'form') {
46
            $d = 'FORM';
47
        } elseif ($type == 'col') {
48
            $d = 'COLUMNS';
49
        }
50
51
        $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...
52
        $cols = str_replace('"', "'", $cols);
53
        $cols = trim(str_replace('$this->'.$type.' = [];', '', $cols));
54
        $colsItem = explode('$this->'.$type.'[] = ', $cols);
55
56
        return array_filter($colsItem);
57
    }
58
59
    static function extract_unit($string, $start, $end)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
60
    {
61
        $pos = stripos($string, $start);
62
        $str = substr($string, $pos);
63
        $str_two = substr($str, strlen($start));
64
        $second_pos = stripos($str_two, $end);
65
        $str_three = substr($str_two, 0, $second_pos);
66
        $unit = trim($str_three); // remove whitespaces
67
68
        return $unit;
69
    }
70
71
    /**
72
     * @param $type
73
     * @param $colsItem
74
     * @return mixed
75
     */
76
    private static function formOptions($type, $colsItem)
77
    {
78
        foreach ($colsItem as &$form) {
79
            if ($type !== 'form') {
80
                continue;
81
            }
82
83
            if ($form['options']) {
84
                @eval("\$options = $form[options];");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
85
                @$form['options'] = $options;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $options seems to be never defined.
Loading history...
86
            } else {
87
                $form['options'] = [];
88
            }
89
        }
90
    }
91
92
    /**
93
     * @param $s
94
     * @return array
95
     */
96
    private static function parseCallback($s)
97
    {
98
        $s = str_replace("return", "return ", $s);
99
        $val = trim(str_replace(["'callback'=>function(\$row) {", "'callback'=>function(\$row){"], "", $s));
100
        $val = substr($val, 0, -1);
101
102
        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...
103
    }
104
105
    /**
106
     * @param $split
107
     * @return array
108
     */
109
    private static function prepareFields($split)
110
    {
111
        $colInnerItem = [];
112
        foreach ($split as $s) {
113
            if (strpos($s, 'options') !== false) {
114
                $colInnerItem['options'] = trim(str_replace("'options'=>", "", $s), '\'\"\]\[');
115
                continue;
116
            }
117
            if (strpos($s, 'callback') !== false) {
118
                $colInnerItem['callback'] = self::parseCallback($s);
119
                continue;
120
            }
121
122
            $s = str_replace("'", '',$s);
123
            list($key, $val) = explode('=>', $s);
124
            $colInnerItem[$key] = $val;
125
126
        }
127
128
        return $colInnerItem;
129
    }
130
131
    /**
132
     * @param $item
133
     * @return mixed|string
134
     */
135
    private static function removeExtraCharacters($item)
136
    {
137
        $item = str_replace(' ', '', $item);
138
        // "['label'=>'KanapeType','name'=>'kanape_type',];\r\n"
139
140
        $item = str_replace('\',]', ']', $item); // replaces:  ',]  with   ]
141
        // "['label'=>'KanapeType','name'=>'kanape_type];\r\n"
142
143
        $item = trim($item);
144
        // "['label'=>'KanapeType','name'=>'kanape_type];"
145
146
        $item = trim($item, '[');
147
        // "'label'=>'KanapeType','name'=>'kanape_type];"
148
149
        $item = trim($item, '];');
150
        // "'label'=>'KanapeType','name'=>'kanape_type"
151
152
        $item = trim($item);
153
        $item = trim(preg_replace("/[\n\r\t]/", "", $item));
154
155
        return $item;
156
    }
157
}