GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CDBQuery::get_Select()   F
last analyzed

Complexity

Conditions 23
Paths 225

Size

Total Lines 79
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 38
nc 225
nop 2
dl 0
loc 79
rs 3.0208
c 0
b 0
f 0

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
3
/**
4
 * Copyright (C) 2010-2023 Davide Franco
5
 *
6
 * This file is part of Bacula-Web.
7
 *
8
 * Bacula-Web is free software: you can redistribute it and/or modify it under the terms of the GNU
9
 * General Public License as published by the Free Software Foundation, either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * Bacula-Web is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
13
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with Bacula-Web. If not, see
17
 * <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace Core\Db;
21
22
use Exception;
23
use TypeError;
24
25
class CDBQuery
26
{
27
    /**
28
     * get_Select
29
     *
30
     * @param array $param array which could contain key listed below
31
     *  $param = [
32
     *      'table'     => (string) name of the table for FROM
33
     *      'fields'    => (array) array of fields
34
     *      'where'     => (array) array of key/pair conditions (separated by AND if several)
35
     *      'join'      => (array) array ['table' => table name to LEFT JOIN, 'condition' => JOIN condition]
36
     *      'groupby'   => (string) add GROUP BY field
37
     *      'orderby'   => (string) add ORDER BY field
38
     *      'limit'     => (array) array ['count' => define the limit, 'offset' => define the OFFSET]
39
     *
40
     * @param string|null $driver
41
     * @return string computed SQL query
42
     */
43
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
    }
124
125
    /**
126
     * @param string $driver
127
     * @param [] $period_timestamp
0 ignored issues
show
Documentation Bug introduced by
The doc comment [] at position 0 could not be parsed: Unknown type name '[' at position 0 in [].
Loading history...
128
     * @return array
129
     */
130
    public static function get_Timestamp_Interval(string $driver, $period_timestamp = []): array
131
    {
132
        $period = [];
133
        $dateformat = 'Y-m-d H:i:s';
134
135
        if ($driver === 'pgsql') {
136
            $period['starttime'] = "TIMESTAMP '" . date($dateformat, $period_timestamp[0]) . "'";
137
            $period['endtime'] = "TIMESTAMP '" . date($dateformat, $period_timestamp[1]) . "'";
138
        } else {
139
            $period['starttime'] = "'" . date($dateformat, $period_timestamp[0]) . "'";
140
            $period['endtime'] = "'" . date($dateformat, $period_timestamp[1]) . "'";
141
        }
142
143
        return $period;
144
    }
145
}
146