Total Complexity | 61 |
Total Lines | 159 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like RDatabaseSqlparserPositioncalculator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RDatabaseSqlparserPositioncalculator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class RDatabaseSqlparserPositioncalculator extends RDatabaseSqlparserSqlparserutils { |
||
51 | |||
52 | private static $allowedOnOperator = array("\t", "\n", "\r", " ", ",", "(", ")", "_", "'", "\""); |
||
53 | private static $allowedOnOther = array("\t", "\n", "\r", " ", ",", "(", ")", "<", ">", "*", "+", "-", "/", "|", |
||
54 | "&", "=", "!", ";"); |
||
55 | |||
56 | private function printPos($text, $sql, $charPos, $key, $parsed, $backtracking) { |
||
|
|||
57 | if (!isset($_ENV['DEBUG'])) { |
||
58 | return; |
||
59 | } |
||
60 | |||
61 | $spaces = ""; |
||
62 | $caller = debug_backtrace(); |
||
63 | $i = 1; |
||
64 | while ($caller[$i]['function'] === 'lookForBaseExpression') { |
||
65 | $spaces .= " "; |
||
66 | $i++; |
||
67 | } |
||
68 | $holdem = substr($sql, 0, $charPos) . "^" . substr($sql, $charPos); |
||
69 | echo $spaces . $text . " key:" . $key . " parsed:" . $parsed . " back:" . serialize($backtracking) . " " |
||
70 | . $holdem . "\n"; |
||
71 | } |
||
72 | |||
73 | public function setPositionsWithinSQL($sql, $parsed) { |
||
74 | $charPos = 0; |
||
75 | $backtracking = array(); |
||
76 | $this->lookForBaseExpression($sql, $charPos, $parsed, 0, $backtracking); |
||
77 | return $parsed; |
||
78 | } |
||
79 | |||
80 | private function findPositionWithinString($sql, $value, $expr_type) { |
||
81 | |||
82 | $offset = 0; |
||
83 | while (true) { |
||
84 | |||
85 | $pos = strpos($sql, $value, $offset); |
||
86 | if ($pos === false) { |
||
87 | break; |
||
88 | } |
||
89 | |||
90 | $before = ""; |
||
91 | if ($pos > 0) { |
||
92 | $before = $sql[$pos - 1]; |
||
93 | } |
||
94 | |||
95 | $after = ""; |
||
96 | if (isset($sql[$pos + strlen($value)])) { |
||
97 | $after = $sql[$pos + strlen($value)]; |
||
98 | } |
||
99 | |||
100 | // if we have an operator, it should be surrounded by |
||
101 | // whitespace, comma, parenthesis, digit or letter, end_of_string |
||
102 | // an operator should not be surrounded by another operator |
||
103 | |||
104 | if ($expr_type === 'operator') { |
||
105 | |||
106 | $ok = ($before === "" || in_array($before, self::$allowedOnOperator, true)) |
||
107 | || (strtolower($before) >= 'a' && strtolower($before) <= 'z') |
||
108 | || ($before >= '0' && $before <= '9'); |
||
109 | $ok = $ok |
||
110 | && ($after === "" || in_array($after, self::$allowedOnOperator, true) |
||
111 | || (strtolower($after) >= 'a' && strtolower($after) <= 'z') |
||
112 | || ($after >= '0' && $after <= '9') || ($after === '?') || ($after === '@')); |
||
113 | |||
114 | if (!$ok) { |
||
115 | $offset = $pos + 1; |
||
116 | continue; |
||
117 | } |
||
118 | |||
119 | break; |
||
120 | } |
||
121 | |||
122 | // in all other cases we accept |
||
123 | // whitespace, comma, operators, parenthesis and end_of_string |
||
124 | |||
125 | $ok = ($before === "" || in_array($before, self::$allowedOnOther, true)); |
||
126 | $ok = $ok && ($after === "" || in_array($after, self::$allowedOnOther, true)); |
||
127 | |||
128 | if ($ok) { |
||
129 | break; |
||
130 | } |
||
131 | |||
132 | $offset = $pos + 1; |
||
133 | } |
||
134 | |||
135 | return $pos; |
||
136 | } |
||
137 | |||
138 | private function lookForBaseExpression($sql, &$charPos, &$parsed, $key, &$backtracking) { |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 |
This check looks for private methods that have been defined, but are not used inside the class.