Passed
Push — master ( 3388c8...01e728 )
by Iman
08:15 queued 02:52
created

ScaffoldingParser::extract_unit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 10
rs 9.4285
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
            $d = 'FORM';
54
        } elseif ($type == 'col') {
55
            $d = 'COLUMNS';
56
        }
57
58
        $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...
59
        $cols = str_replace('"', "'", $cols);
60
        $cols = trim(str_replace('$this->'.$type.' = [];', '', $cols));
61
        $colsItem = explode('$this->'.$type.'[] = ', $cols);
62
63
        return array_filter($colsItem);
64
    }
65
66
    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...
67
    {
68
        $pos = stripos($string, $start);
69
        $str = substr($string, $pos);
70
        $str_two = substr($str, strlen($start));
71
        $second_pos = stripos($str_two, $end);
72
        $str_three = substr($str_two, 0, $second_pos);
73
        $unit = trim($str_three); // remove whitespaces
74
75
        return $unit;
76
    }
77
78
    /**
79
     * @param $type
80
     * @param $colsItem
81
     * @return mixed
82
     */
83
    private static function formOptions($type, $colsItem)
84
    {
85
        foreach ($colsItem as &$form) {
86
            if ($type !== 'form') {
87
                continue;
88
            }
89
            if ($form['options']) {
90
                @eval("\$options = $form[options];");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
91
                @$form['options'] = $options;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $options seems to be never defined.
Loading history...
92
            } else {
93
                $form['options'] = [];
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param $s
100
     * @return array
101
     */
102
    private static function parseCallback($s)
103
    {
104
        $s = str_replace("return", "return ", $s);
105
        $val = trim(str_replace(["'callback'=>function(\$row) {", "'callback'=>function(\$row){"], "", $s));
106
        $val = substr($val, 0, -1);
107
108
        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...
109
    }
110
111
    /**
112
     * @param $split
113
     * @return array
114
     */
115
    private static function prepareFields($split)
116
    {
117
        $colInnerItem = [];
118
        foreach ($split as $s) {
119
            if (strpos($s, 'options') !== false) {
120
                $colInnerItem['options'] = trim(str_replace("'options'=>", "", $s));
121
            } elseif (strpos($s, 'callback') !== false) {
122
                $colInnerItem['callback'] = self::parseCallback($s);
123
            } else {
124
                $s = trim($s, "'");
125
                $sSplit = explode('=>', $s);
126
                $colInnerItem[$sSplit[0]] = $sSplit[1];
127
            }
128
        }
129
130
        return $colInnerItem;
131
    }
132
}