Ajde_Query::getSql()   F
last analyzed

Complexity

Conditions 19
Paths 388

Size

Total Lines 78
Code Lines 44

Duplication

Lines 32
Ratio 41.03 %

Importance

Changes 0
Metric Value
cc 19
eloc 44
nc 388
nop 0
dl 32
loc 78
rs 3.8942
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
class Ajde_Query extends Ajde_Object_Standard
4
{
5
    const ORDER_ASC = 'ASC';
6
    const ORDER_DESC = 'DESC';
7
8
    const OP_AND = 'AND';
9
    const OP_OR = 'OR';
10
11
    const JOIN_INNER = 'INNER';
12
    const JOIN_LEFT = 'LEFT';
13
14
    public $select = [];
15
    public $distinct = false;
16
    public $from = [];
17
    public $where = [];
18
    public $having = [];
19
    public $join = [];
20
    public $groupBy = [];
21
    public $orderBy = [];
22
    public $limit = ['start' => null, 'count' => null];
23
24
    public function reset()
25
    {
26
        $this->select = [];
27
        $this->from = [];
28
        $this->where = [];
29
        $this->having = [];
30
        $this->join = [];
31
        $this->groupBy = [];
32
        $this->orderBy = [];
33
        $this->limit = ['start' => null, 'count' => null];
34
    }
35
36
    public function addSelect($select)
37
    {
38
        $this->select[] = $select;
39
    }
40
41
    public function setDistinct($distinct)
42
    {
43
        $this->distinct = (bool) $distinct;
44
    }
45
46
    public function addFrom($from)
47
    {
48
        $this->from[] = $from;
49
    }
50
51
    public function addWhere($where, $operator = self::OP_AND)
52
    {
53
        $this->where[] = ['sql' => $where, 'operator' => $operator];
54
    }
55
56
    public function addHaving($having, $operator = self::OP_AND)
57
    {
58
        $this->having[] = ['sql' => $having, 'operator' => $operator];
59
    }
60
61
    public function addJoin($join, $type = self::JOIN_INNER)
62
    {
63
        $this->join[] = ['sql' => $join, 'type' => $type];
64
    }
65
66
    public function addOrderBy($field, $direction = self::ORDER_ASC)
67
    {
68
        $direction = strtoupper($direction);
69
        if (!in_array($direction, [self::ORDER_ASC, self::ORDER_DESC])) {
70
            // TODO:
71
            throw new Ajde_Exception('Collection ordering direction "'.$direction.'" not valid');
72
        }
73
        $this->orderBy[] = ['field' => $field, 'direction' => $direction];
74
    }
75
76
    public function addGroupBy($field)
77
    {
78
        $this->groupBy[] = $field;
79
    }
80
81
    public function limit($count, $start = 0)
82
    {
83
        $this->limit = ['count' => (int) $count, 'start' => (int) $start];
84
    }
85
86
    public function getSql()
87
    {
88
        $sql = '';
89
        $distinct = $this->distinct ? 'DISTINCT ' : '';
90
91
        // SELECT
92
        if (empty($this->select)) {
93
            $sql .= 'SELECT '.$distinct.'*';
94
        } else {
95
            $sql .= 'SELECT '.$distinct.implode(', ', $this->select);
96
        }
97
98
        // FROM
99 View Code Duplication
        if (empty($this->from)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
            // TODO:
101
            throw new Ajde_Exception('FROM clause can not be empty in query');
102
        } else {
103
            $sql .= ' FROM '.implode(', ', $this->from);
104
        }
105
106
        // JOIN
107
        if (!empty($this->join)) {
108
            foreach ($this->join as $join) {
109
                $sql .= ' '.$join['type'].' JOIN '.$join['sql'];
110
            }
111
        }
112
113
        // WHERE
114 View Code Duplication
        if (!empty($this->where)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
            $first = true;
116
            $sql .= ' WHERE';
117
            foreach ($this->where as $where) {
118
                if ($first === false) {
119
                    $sql .= ' '.$where['operator'];
120
                }
121
                $sql .= ' '.$where['sql'];
122
                $first = false;
123
            }
124
        }
125
126
        // GROUP BY
127 View Code Duplication
        if (!empty($this->groupBy)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
            $sql .= ' GROUP BY';
129
            $sql .= ' '.implode(', ', $this->groupBy);
130
        }
131
132
        // HAVING
133 View Code Duplication
        if (!empty($this->having)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
            $first = true;
135
            $sql .= ' HAVING';
136
            foreach ($this->having as $having) {
137
                if ($first === false) {
138
                    $sql .= ' '.$having['operator'];
139
                }
140
                $sql .= ' '.$having['sql'];
141
                $first = false;
142
            }
143
        }
144
145
        // ORDER BY
146
        if (!empty($this->orderBy)) {
147
            $sql .= ' ORDER BY';
148
            $orderBySql = [];
149
            foreach ($this->orderBy as $orderBy) {
150
                $orderBySql[] = $orderBy['field'].' '.$orderBy['direction'];
151
            }
152
            $sql .= ' '.implode(', ', $orderBySql);
153
        }
154
155
        // LIMIT
156
        if (isset($this->limit['count']) && !isset($this->limit['start'])) {
157
            $sql .= ' LIMIT '.$this->limit['count'];
158
        } elseif (isset($this->limit['count']) && isset($this->limit['start'])) {
159
            $sql .= ' LIMIT '.$this->limit['start'].', '.$this->limit['count'];
160
        }
161
162
        return $sql;
163
    }
164
}
165