Conditions | 8 |
Paths | 20 |
Total Lines | 60 |
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 |
||
17 | protected function buildSelectQuery(SQLSelect $query, array &$parameters) |
||
18 | { |
||
19 | list($limit, $offset) = $this->parseLimit($query); |
||
20 | |||
21 | // If not using ofset then query generation is quite straightforward |
||
22 | if (empty($offset)) { |
||
23 | $sql = parent::buildSelectQuery($query, $parameters); |
||
24 | // Inject limit into SELECT fragment |
||
25 | if (!empty($limit)) { |
||
26 | $sql = preg_replace('/^(SELECT (DISTINCT)?)/i', '${1} TOP '.$limit, $sql); |
||
27 | } |
||
28 | return $sql; |
||
29 | } |
||
30 | |||
31 | // When using offset we must use a subselect |
||
32 | // @see http://stackoverflow.com/questions/2135418/equivalent-of-limit-and-offset-for-sql-server |
||
33 | $orderby = $query->getOrderBy(); |
||
34 | |||
35 | // workaround for subselect not working with alias functions |
||
36 | // just use the function directly in the order by instead of the alias |
||
37 | $selects = $query->getSelect(); |
||
38 | foreach ($orderby as $field => $dir) { |
||
39 | if (preg_match('/_SortColumn/', $field)) { |
||
40 | unset($orderby[$field]); |
||
41 | $orderby[$selects[str_replace('"', '', $field)]] = $dir; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | // Create order expression, using the first column if none explicitly specified |
||
46 | if ($orderby) { |
||
|
|||
47 | // Simple implementation of buildOrderByFragment |
||
48 | $statements = array(); |
||
49 | foreach ($orderby as $clause => $dir) { |
||
50 | $statements[] = trim("$clause $dir"); |
||
51 | } |
||
52 | $orderByClause = "ORDER BY " . implode(', ', $statements); |
||
53 | } else { |
||
54 | $selects = $query->getSelect(); |
||
55 | $firstCol = reset($selects); |
||
56 | $orderByClause = "ORDER BY $firstCol"; |
||
57 | } |
||
58 | |||
59 | // Build main query SQL |
||
60 | $sql = parent::buildSelectQuery($query, $parameters); |
||
61 | |||
62 | // Inject row number into selection |
||
63 | $sql = preg_replace('/^(SELECT (DISTINCT)?)/i', '${1} ROW_NUMBER() OVER ('.$orderByClause.') AS Number, ', $sql); |
||
64 | |||
65 | // Sub-query this SQL |
||
66 | if (empty($limit)) { |
||
67 | $limitCondition = "Number > ?"; |
||
68 | $parameters[] = $offset; |
||
69 | } else { |
||
70 | $limitCondition = "Number BETWEEN ? AND ?"; |
||
71 | $parameters[] = $offset + 1; |
||
72 | $parameters[] = $offset + $limit; |
||
73 | } |
||
74 | |||
75 | return "SELECT * FROM ($sql) AS Numbered WHERE $limitCondition ORDER BY Number"; |
||
76 | } |
||
77 | |||
145 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.