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

FileManipulator::stringify()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 5
nop 2
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator;
4
5
class FileManipulator
6
{
7
/*    static function readMethodContent($code, $functionToFind)
8
    {
9
        $codeArray = explode("\n", $code);
10
        $tagBuka = 0;
11
        $tagPentutups = [];
12
        $methodIndex = [];
13
        foreach ($codeArray as $i => &$line) {
14
            if (strpos($line, 'function '.$functionToFind.'(') !== false) {
15
                $tagBuka = $i;
16
            }
17
            $tagPentutups = self::tagPentutups($line, $i, $tagPentutups);
18
            $methodIndex = self::methodIndex($line, $i, $methodIndex);
19
        }
20
21
        list($methodIndex, $methodNextIndex) = self::tagBuka($methodIndex, $tagBuka);
22
23
        $tagPentutups = array_values($tagPentutups);
24
25
        $finalTagPenutup = self::finalTagPenutup($tagBuka, $tagPentutups, $methodIndex, $methodNextIndex);
26
27
        $content = self::getContents($codeArray, $tagBuka, $finalTagPenutup);
28
29
        return implode("\n", $content);
30
    }*/
31
32
/*    static function writeMethodContent($code, $functionToFind, $stringToInsert)
33
    {
34
        $codeArray = explode("\n", $code);
35
        $tagBuka = 0;
36
        $tagPentutups = [];
37
        $methodIndex = [];
38
        $indentCount = 0;
39
40
        foreach ($codeArray as $i => &$line) {
41
            if (strpos($line, 'function '.$functionToFind.'(') !== false) {
42
                $tagBuka = $i;
43
                $indentCount = substr_count($line, "    ");
44
            }
45
46
            $tagPentutups = self::tagPentutups($line, $i, $tagPentutups);
47
            $methodIndex = self::methodIndex($line, $i, $methodIndex);
48
        }
49
50
        list($methodIndex, $methodNextIndex) = self::tagBuka($methodIndex, $tagBuka);
51
52
        $tagPentutups = array_values($tagPentutups);
53
54
        $finalTagPenutup = self::finalTagPenutup($tagBuka, $tagPentutups, $methodIndex, $methodNextIndex);
55
56
        //Removing Content Of Method
57
        $codeArray = self::removeMethodContent($codeArray, $tagBuka, $finalTagPenutup);
58
59
        //Insert Content To Method
60
        $stringToInsertArray = explode("\n", trim($stringToInsert));
61
        foreach ($stringToInsertArray as $i => &$s) {
62
            $s = str_repeat('    ', $indentCount + 2).trim($s);
63
        }
64
65
        $stringToInsert = implode("\n", $stringToInsertArray);
66
        foreach ($codeArray as $i => $c) {
67
            if ($i == $tagBuka) {
68
                array_splice($codeArray, $i + 1, 0, [$stringToInsert]);
69
            }
70
        }
71
72
        return implode("\n", $codeArray);
73
    }*/
74
75
    /**
76
     * @param $codeArray
77
     * @param $tagBuka
78
     * @param $finalTagPenutup
79
     * @return array
80
     */
81
  /*  private static function getContents($codeArray, $tagBuka, $finalTagPenutup)
82
    {
83
        $content = [];
84
        foreach ($codeArray as $i => $line) {
85
            if ($i > $tagBuka && $i < $finalTagPenutup) {
86
                $content[] = $line;
87
            }
88
        }
89
90
        return $content;
91
    }*/
92
93
    /**
94
     * @param $tagBuka
95
     * @param $tagPentutups
96
     * @param $methodIndex
97
     * @param $methodNextIndex
98
     * @return mixed
99
     */
100
/*    private static function finalTagPenutup($tagBuka, $tagPentutups, $methodIndex, $methodNextIndex)
101
    {
102
        $finalTagPenutup = 0;
103
104
        if ($tagBuka == end($methodIndex)) {
105
            return $tagPentutups[count($tagPentutups) - 2];
106
        }
107
108
        foreach ($tagPentutups as $i => $tp) {
109
            if ($tp > $methodIndex[$methodNextIndex]) {
110
                $finalTagPenutup = $tagPentutups[$i - 1];
111
                break;
112
            }
113
        }
114
115
        return $finalTagPenutup;
116
    }*/
117
118
    /**
119
     * @param $line
120
     * @param $e
121
     * @param $tagPentutups
122
     * @return mixed
123
     */
124
/*    private static function tagPentutups($line, $e, $tagPentutups)
125
    {
126
        if (stripos($line, '}') !== false) {
127
            $tagPentutups[$e] = $e;
128
        }
129
        $tagPembukas = [];
130
        if (stripos($line, '{') !== false) {
131
            $tagPembukas[$e] = $e;
132
        }
133
134
        return $tagPentutups;
135
    }*/
136
137
    /**
138
     * @param $line
139
     * @param $i
140
     * @param $methodIndex
141
     * @return array
142
     */
143
/*    private static function methodIndex($line, $i, $methodIndex)
144
    {
145
        foreach (['public', 'private'] as $m) {
146
            if (strpos($line, $m) !== false) {
147
                $methodIndex[] = $i;
148
                break;
149
            }
150
        }
151
152
        if (strpos($line, ' function ') !== false) {
153
            $methodIndex[] = $i;
154
        }
155
156
        return $methodIndex;
157
    }*/
158
159
    /**
160
     * @param $methodIndex
161
     * @param $tagBuka
162
     * @return array
163
     */
164
/*    private static function tagBuka($methodIndex, $tagBuka)
165
    {
166
        $methodIndex = array_values(array_unique($methodIndex));
167
168
        $keyTagBukaInMethodIndex = array_search($tagBuka, $methodIndex);
169
        $totalMethodIndex = count($methodIndex) - 1;
170
        $methodNextIndex = ($totalMethodIndex == $keyTagBukaInMethodIndex) ? $keyTagBukaInMethodIndex : $keyTagBukaInMethodIndex + 1;
171
172
        return [$methodIndex, $methodNextIndex];
173
    }*/
174
175
    static function putCtrlContent($ctrl, $fileContent)
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...
176
    {
177
        return file_put_contents(controller_path($ctrl), $fileContent);
178
    }
179
180
    static function readCtrlContent($ctrl)
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...
181
    {
182
        return file_get_contents(controller_path($ctrl));
183
    }
184
185
    public static function extractBetween($raw, $mark)
186
    {
187
        list($before, $_rest) = explode("# START $mark DO NOT REMOVE THIS LINE", $raw);
188
        list($_middle, $after) = explode("# END $mark DO NOT REMOVE THIS LINE", $_rest);
189
190
        return [trim($before), trim($_middle), trim($after)];
191
    }
192
193
    /**
194
     * @param $phpCode
195
     * @param $mark
196
     * @param $newCode
197
     * @return string
198
     */
199
    public static function replaceBetweenMark($phpCode, $mark, $newCode)
200
    {
201
        list($top, $_middle, $bottom) = self::extractBetween($phpCode, $mark);
202
203
        $_code = $top."\n\n";
204
        $_code .= str_repeat(' ', 12)."# START $mark DO NOT REMOVE THIS LINE\n";
205
        $_code .= $newCode."\n";
206
        $_code .= str_repeat(' ', 12)."# END $mark DO NOT REMOVE THIS LINE\n\n";
207
        $_code .= str_repeat(' ', 12).$bottom;
208
209
        return $_code;
210
    }
211
212
    public static function stringify($input, $indent = "")
213
    {
214
        if (! is_array($input)) {
215
            return var_export($input, true);
216
        }
217
        $buffer = [];
218
        foreach ($input as $key => $value) {
219
            $buffer[] = $indent.var_export($key, true)."=>".min_var_export($value, ($indent."    "));
0 ignored issues
show
Bug introduced by
The function min_var_export was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

219
            $buffer[] = $indent.var_export($key, true)."=>"./** @scrutinizer ignore-call */ min_var_export($value, ($indent."    "));
Loading history...
220
        }
221
        if (empty($buffer)) {
222
            return "[]";
223
        }
224
225
        return "[\n".implode(",\n", $buffer)."\n$indent]";
226
    }
227
}