Conditions | 20 |
Paths | 55 |
Total Lines | 53 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
118 | public function parseKey(Query $query, $key, $strict = false) |
||
119 | { |
||
120 | if (is_numeric($key)) { |
||
121 | return $key; |
||
122 | } elseif ($key instanceof Expression) { |
||
123 | return $key->getValue(); |
||
124 | } |
||
125 | |||
126 | $key = trim($key); |
||
127 | |||
128 | if(strpos($key, '->>') && false === strpos($key, '(')){ |
||
129 | // JSON字段支持 |
||
130 | list($field, $name) = explode('->>', $key, 2); |
||
131 | |||
132 | return $this->parseKey($query, $field, true) . '->>\'$' . (strpos($name, '[') === 0 ? '' : '.') . str_replace('->>', '.', $name) . '\''; |
||
133 | } |
||
134 | elseif (strpos($key, '->') && false === strpos($key, '(')) { |
||
135 | // JSON字段支持 |
||
136 | list($field, $name) = explode('->', $key, 2); |
||
137 | |||
138 | return 'json_extract(' . $this->parseKey($query, $field, true) . ', \'$' . (strpos($name, '[') === 0 ? '' : '.') . str_replace('->', '.', $name) . '\')'; |
||
139 | } elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) { |
||
140 | list($table, $key) = explode('.', $key, 2); |
||
141 | |||
142 | $alias = $query->getOptions('alias'); |
||
143 | |||
144 | if ('__TABLE__' == $table) { |
||
145 | $table = $query->getOptions('table'); |
||
146 | $table = is_array($table) ? array_shift($table) : $table; |
||
147 | } |
||
148 | |||
149 | if (isset($alias[$table])) { |
||
150 | $table = $alias[$table]; |
||
151 | } |
||
152 | } |
||
153 | |||
154 | if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) { |
||
155 | throw new Exception('not support data:' . $key); |
||
156 | } |
||
157 | |||
158 | if ('*' != $key && !preg_match('/[,\'\"\*\(\)`.\s]/', $key)) { |
||
159 | $key = '`' . $key . '`'; |
||
160 | } |
||
161 | |||
162 | if (isset($table)) { |
||
163 | if (strpos($table, '.')) { |
||
164 | $table = str_replace('.', '`.`', $table); |
||
165 | } |
||
166 | |||
167 | $key = '`' . $table . '`.' . $key; |
||
168 | } |
||
169 | |||
170 | return $key; |
||
171 | } |
||
185 |