Conditions | 18 |
Paths | 16 |
Total Lines | 59 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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; |
||
|
|||
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) |
||
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 | } |
||
182 |