Conditions | 22 |
Paths | 1117 |
Total Lines | 84 |
Code Lines | 47 |
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 |
||
139 | public function column(Query $query, $field, $key = '') |
||
140 | { |
||
141 | $options = $query->getOptions(); |
||
142 | |||
143 | if (empty($options['fetch_sql']) && !empty($options['cache'])) { |
||
144 | // 判断查询缓存 |
||
145 | $cache = $options['cache']; |
||
146 | |||
147 | $guid = is_string($cache['key']) ? $cache['key'] : $this->getCacheKey($query, $field); |
||
148 | |||
149 | $result = Container::get('cache')->get($guid); |
||
150 | |||
151 | if (false !== $result) { |
||
152 | return $result; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | if (isset($options['field'])) { |
||
157 | $query->removeOption('field'); |
||
158 | } |
||
159 | |||
160 | if (is_null($field)) { |
||
161 | $field = '*'; |
||
162 | } elseif ($key && '*' != $field) { |
||
163 | $field = $key . ',' . $field; |
||
164 | } |
||
165 | |||
166 | if (is_string($field)) { |
||
167 | $field = array_map('trim', explode(',', $field)); |
||
168 | } |
||
169 | |||
170 | $query->setOption('field', $field); |
||
171 | |||
172 | // 生成查询SQL |
||
173 | $sql = $this->builder->select($query); |
||
174 | |||
175 | $bind = $query->getBind(); |
||
176 | |||
177 | if (!empty($options['fetch_sql'])) { |
||
178 | // 获取实际执行的SQL语句 |
||
179 | return $this->getRealSql($sql, $bind); |
||
180 | } |
||
181 | |||
182 | // 执行查询操作 |
||
183 | $pdo = $this->query($sql, $bind, $options['master'], true); |
||
184 | |||
185 | if (1 == $pdo->columnCount()) { |
||
186 | $result = $pdo->fetchAll(PDO::FETCH_COLUMN); |
||
187 | } else { |
||
188 | $resultSet = $pdo->fetchAll(PDO::FETCH_ASSOC); |
||
189 | |||
190 | if ('*' == $field && $key) { |
||
191 | $result = array_column($resultSet, null, $key); |
||
192 | } elseif ($resultSet) { |
||
193 | $fields = array_keys($resultSet[0]); |
||
194 | $count = count($fields); |
||
195 | $key1 = array_shift($fields); |
||
196 | $key2 = $fields ? array_shift($fields) : ''; |
||
197 | $key = $key ?: $key1; |
||
198 | |||
199 | if (strpos($key, '.')) { |
||
200 | list($alias, $key) = explode('.', $key); |
||
201 | } |
||
202 | |||
203 | if (3 == $count) { |
||
204 | $column = $key2; |
||
205 | } elseif ($count < 3) { |
||
206 | $column = $key1; |
||
207 | } else { |
||
208 | $column = null; |
||
209 | } |
||
210 | |||
211 | $result = array_column($resultSet, $column, $key); |
||
212 | } else { |
||
213 | $result = []; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | if (isset($cache) && isset($guid)) { |
||
218 | // 缓存数据 |
||
219 | $this->cacheData($guid, $result, $cache); |
||
220 | } |
||
221 | |||
222 | return $result; |
||
223 | } |
||
236 |