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