Passed
Push — master ( 7f0dd2...2d27c8 )
by Richard
01:42
created

SelectBuilder::createSql()   C

Complexity

Conditions 19
Paths 154

Size

Total Lines 58
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 19
eloc 41
c 1
b 0
f 0
nc 154
nop 2
dl 0
loc 58
rs 5.7964

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
namespace Maphper\Lib;
3
class SelectBuilder {
4
	public function select($table, $criteria, $args, $options = []) {
5
		$where = $criteria ? ' WHERE ' . $criteria : '';
6
		$limit = (isset($options['limit'])) ? ' LIMIT ' . $options['limit'] : '';
7
8
		if (isset($options['offset'])) {
9
			$offset = ' OFFSET ' . $options['offset'];
10
			if (!$limit) $limit = ' LIMIT  1000';
11
		}
12
		else $offset = '';
13
14
		$order = isset($options['order']) ? ' ORDER BY ' . $options['order'] : '';
15
		return new Query('SELECT * FROM ' . $table . ' ' . $where . $order . $limit . $offset, $args);
16
	}
17
18
	public function aggregate($table, $function, $field, $where, $args, $group) {
19
		if ($group == true) $groupBy = ' GROUP BY ' . $field;
20
		else $groupBy = '';
21
		return new Query('SELECT ' . $function . '(' . $field . ') as val, ' . $field . '   FROM ' . $table . ($where != null ? ' WHERE ' . $where : '') . ' ' . $groupBy, $args);
22
	}
23
}
24