Passed
Pull Request — master (#1108)
by Iman
04:22
created

ScaffoldingParser::prepareFields()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
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
    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 = str_replace(' ','', $item);
13
            $item = str_replace('\',]',']', $item);
14
            $item = trim($item);
15
            $item = trim($item, '[');
16
            $item = trim($item, '];');
17
            $item = trim($item);
18
            $item = trim(preg_replace("/[\n\r\t]/", "", $item));
19
            $strSplit = str_split($item);
20
            $innerCount = 0;
21
            foreach ($strSplit as $index => $s) {
22
                if ($s == '[') {
23
                    $innerCount++;
24
                }
25
                if ($s == ']') {
26
                    $innerCount--;
27
                }
28
                if ($innerCount == 0 && $s == ',' && $strSplit[$index + 1] == "'") {
29
                    $strSplit[$index] = "|SPLIT|";
30
                }
31
            }
32
            $item = implode("", $strSplit);
33
        }
34
35
        foreach ($colsItem as &$col) {
36
            $colInnerItem = self::prepareFields(explode('|SPLIT|', $col));
37
            $col = $colInnerItem;
38
        }
39
40
        self::formOptions($type, $colsItem);
41
42
        return $colsItem;
43
    }
44
45
    /**
46
     * @param $code
47
     * @param $type
48
     * @return array
49
     */
50
    private static function extractLines($code, $type)
51
    {
52
        if ($type == 'form') {
53
            $cols = extract_unit($code, "# START FORM DO NOT REMOVE THIS LINE", "# END FORM DO NOT REMOVE THIS LINE");
54
            $cols = str_replace('"', "'", $cols);
55
            $cols = trim(str_replace('$this->form = [];', '', $cols));
56
            $colsItem = explode('$this->form[] = ', $cols);
57
        } elseif ($type == 'col') {
58
            $cols = extract_unit($code, "# START COLUMNS DO NOT REMOVE THIS LINE", "# END COLUMNS DO NOT REMOVE THIS LINE");
59
            $cols = str_replace('"', "'", $cols);
60
            $cols = trim(str_replace('$this->col = [];', '', $cols));
61
            $colsItem = explode('$this->col[] = ', $cols);
62
        }
63
64
        $colsItem = array_filter($colsItem);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $colsItem does not seem to be defined for all execution paths leading up to this point.
Loading history...
65
66
        return $colsItem;
67
    }
68
69
    /**
70
     * @param $type
71
     * @param $colsItem
72
     * @return mixed
73
     */
74
    private static function formOptions($type, $colsItem)
75
    {
76
        foreach ($colsItem as &$form) {
77
            if ($type !== 'form') {
78
                continue;
79
            }
80
            if ($form['options']) {
81
                @eval("\$options = $form[options];");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
82
                @$form['options'] = $options;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $options seems to be never defined.
Loading history...
83
            } else {
84
                $form['options'] = [];
85
            }
86
        }
87
    }
88
89
    /**
90
     * @param $s
91
     * @return array
92
     */
93
    private static function parseCallback($s)
94
    {
95
        $s = str_replace("return", "return ", $s);
96
        $val = trim(str_replace(["'callback'=>function(\$row) {", "'callback'=>function(\$row){"], "", $s));
97
        $val = substr($val, 0, -1);
98
99
        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...
100
    }
101
102
    /**
103
     * @param $split
104
     * @return array
105
     */
106
    private static function prepareFields($split)
107
    {
108
        $colInnerItem = [];
109
        foreach ($split as $s) {
110
            if (strpos($s, 'options') !== false) {
111
                $colInnerItem['options'] = trim(str_replace("'options'=>", "", $s));
112
            } elseif (strpos($s, 'callback') !== false) {
113
                $colInnerItem['callback'] = self::parseCallback($s);
114
            } else {
115
                $s = trim($s, "'");
116
                $sSplit = explode('=>', $s);
117
                $colInnerItem[$sSplit[0]] = $sSplit[1];
118
            }
119
        }
120
121
        return $colInnerItem;
122
    }
123
}