Passed
Push — master ( 70a5dd...72f7b3 )
by Observer
01:54
created

ClassWorker::stripComments()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 27
c 0
b 0
f 0
rs 9.4555
cc 5
nc 5
nop 1
1
<?php
2
3
namespace VoidEngine;
4
5
class ClassWorker
6
{
7
    public static function applyClass (string $code, string $class, string $apply): string
8
    {
9
        $code = self::stripComments ($code);
10
11
        $split1 = $split2 = false;
12
13
        $len      = strlen ($code);
14
        $classLen = strlen ($class);
15
16
        $class_predefined = false;
17
        $class_close = null;
18
19
        for ($i = 0; $i < $len; ++$i)
20
        {
21
            if ($code[$i] == '\'' && !$split2)
22
                $split1 = !$split1;
0 ignored issues
show
introduced by
The condition $split1 is always false.
Loading history...
23
24
            elseif ($code[$i] == '"' && !$split1)
25
                $split2 = !$split2;
26
27
            elseif (!$split1 && !$split2)
28
            {
29
                if ($code[$i] == 'c' && substr ($code, $i, 5) == 'class')
30
                {
31
                    for ($j = $i + 5; $j < $len; ++$j)
32
                        if (in_array ($code[$j], ["\n", "\r", "\t", ' ']))
33
                            continue;
34
35
                        else
36
                        {
37
                            if (substr ($code, $j, $classLen) == $class)
38
                                $class_predefined = true;
39
40
                            $i = $j;
41
42
                            break;
43
                        }
44
                }
45
46
                elseif ($class_predefined == true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
47
                {
48
                    if ($code[$i] == '{')
49
                    {
50
                        if ($class_close === null)
51
                            $class_close = 1;
52
53
                        else ++$class_close;
54
                    }
55
56
                    elseif ($code[$i] == '}')
57
                        --$class_close;
58
59
                    if ($class_close === 0)
60
                        return substr ($code, 0, $i) . $apply . substr ($code, $i);
61
                }
62
            }
63
        }
64
65
        return $code;
66
    }
67
68
    public static function getAvailableClassMethods (string $code, string $class): array
69
    {
70
        $code = self::stripComments ($code);
71
72
        $split1 = $split2 = false;
73
74
        $len      = strlen ($code);
75
        $classLen = strlen ($class);
76
77
        $class_predefined = false;
78
        $class_close = null;
79
80
        $methods = [];
81
82
        for ($i = 0; $i < $len; ++$i)
83
        {
84
            if ($code[$i] == '\'' && !$split2)
85
                $split1 = !$split1;
0 ignored issues
show
introduced by
The condition $split1 is always false.
Loading history...
86
87
            elseif ($code[$i] == '"' && !$split1)
88
                $split2 = !$split2;
89
90
            elseif (!$split1 && !$split2)
91
            {
92
                if ($code[$i] == 'c' && substr ($code, $i, 5) == 'class')
93
                {
94
                    for ($j = $i + 5; $j < $len; ++$j)
95
                        if (in_array ($code[$j], ["\n", "\r", "\t", ' ']))
96
                            continue;
97
98
                        else
99
                        {
100
                            if (substr ($code, $j, $classLen) == $class)
101
                                $class_predefined = true;
102
103
                            $i = $j;
104
105
                            break;
106
                        }
107
                }
108
109
                elseif ($class_predefined == true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
110
                {
111
                    if ($code[$i] == 's' && substr ($code, $i, 6) == 'static')
112
                    {
113
                        for ($j = $i + 6; $j < $len; ++$j)
114
                            if (!in_array ($code[$j], ["\n", "\r", "\t", ' ']))
115
                                break;
116
117
                        if ($code[$j] == 'f' && substr ($code, $j, 8) == 'function')
118
                        {
119
                            for ($j = $j + 8; $j < $len; ++$j)
120
                                if (in_array ($code[$j], ["\n", "\r", "\t", ' ']))
121
                                    continue;
122
123
                                else
124
                                {
125
                                    $i = $j;
126
                                    $methods[] = trim (substr ($code, $j, strpos ($code, '(', $j) - $j));
127
128
                                    break;
129
                                }
130
                        }
131
                    }
132
                    
133
                    elseif ($code[$i] == '{')
134
                    {
135
                        if ($class_close === null)
136
                            $class_close = 1;
137
138
                        else ++$class_close;
139
                    }
140
141
                    elseif ($code[$i] == '}')
142
                        --$class_close;
143
144
                    if ($class_close === 0)
145
                        return $methods;
146
                }
147
            }
148
        }
149
150
        return $methods;
151
    }
152
153
    public static function stripComments (string $code): string
154
    {
155
        $tokens = token_get_all ("<?php\n\n". $code);
156
        $return = '';
157
158
        foreach ($tokens as $token)
159
            if (is_string ($token))
160
                $return .= $token;
161
162
            else
163
            {
164
                list ($id, $text) = $token;
165
166
                switch ($id)
167
                {
168
                    case T_COMMENT:
169
                    case T_DOC_COMMENT:
170
                        break;
171
172
                    default:
173
                        $return .= $text;
174
175
                        break;
176
                }
177
            }
178
179
        return substr ($return, 7);
180
    }
181
}
182