Conditions | 23 |
Paths | 225 |
Total Lines | 79 |
Code Lines | 38 |
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 |
||
44 | public static function get_Select(array $param, string $driver = null): string |
||
45 | { |
||
46 | $query = 'SELECT '; |
||
47 | $where = ''; |
||
48 | |||
49 | if (empty($param)) { |
||
50 | throw new TypeError('Missing parameters: you should provide an array'); |
||
51 | } |
||
52 | |||
53 | // Buidling SQL query |
||
54 | // Fields |
||
55 | if (isset($param['fields'])) { |
||
56 | foreach ($param['fields'] as $field) { |
||
57 | $query .= $field; |
||
58 | if (end($param['fields']) != $field) { |
||
59 | $query .= ', '; |
||
60 | } else { |
||
61 | $query .= ' '; |
||
62 | } |
||
63 | } |
||
64 | } else { |
||
65 | $query .= '* '; |
||
66 | } |
||
67 | |||
68 | // From |
||
69 | $query .= 'FROM ' . $param['table'] . ' '; |
||
70 | |||
71 | // Join |
||
72 | if (isset($param['join']) && !is_null($param['join']) && is_array($param['join'])) { |
||
73 | foreach ($param['join'] as $join) { |
||
74 | $query .= 'LEFT JOIN ' . $join['table'] . ' ON ' . $join['condition'] . ' '; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | // Where |
||
79 | if (isset($param['where']) && is_array($param['where'])) { |
||
80 | foreach ($param['where'] as $key => $where_item) { |
||
81 | if ($key > 0) { |
||
82 | $where .= "AND $where_item "; |
||
83 | } else { |
||
84 | $where .= "$where_item "; |
||
85 | } |
||
86 | } // end foreach |
||
87 | |||
88 | $query .= 'WHERE ' . $where . ' '; |
||
89 | } |
||
90 | |||
91 | // Group by |
||
92 | if (isset($param['groupby']) && !is_null($param['groupby'])) { |
||
93 | $query .= 'GROUP BY ' . $param['groupby'] . ' '; |
||
94 | } |
||
95 | |||
96 | // Order by |
||
97 | if (isset($param['orderby']) && !is_null($param['orderby'])) { |
||
98 | $query .= 'ORDER BY ' . $param['orderby'] . ' '; |
||
99 | } |
||
100 | |||
101 | // Limit |
||
102 | if (isset($param['limit']) && !is_null($param['limit'])) { |
||
103 | $limit = $param['limit']; |
||
104 | |||
105 | // we passed an array( 'count' => $count, 'offset' => $offset) |
||
106 | if (is_array($limit)) { |
||
107 | if (($driver == 'pgsql') || ($driver == 'mysql')) { |
||
108 | // postgreSQL query |
||
109 | $query .= 'LIMIT ' . $limit['count'] . ' OFFSET ' . $limit['offset']; |
||
110 | } else { |
||
111 | // MySQL query |
||
112 | $query .= 'LIMIT ' . $limit['offset'] . ',' . $limit['count']; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | // we passed limit as an integer |
||
117 | if (is_numeric($limit)) { |
||
118 | $query .= 'LIMIT ' . $param['limit']; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | return $query; |
||
123 | } |
||
146 |