Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SQLQueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SQLQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class SQLQueryBuilder extends AbstractQueryBuilder |
||
6 | { |
||
7 | |||
8 | private $queryBuilder; |
||
9 | |||
10 | 2 | public function run() |
|
11 | { |
||
12 | 2 | $this->queryBuilder = $this->getQueryBuilder(); |
|
13 | 2 | $this->setJoins(); |
|
14 | 2 | $count = $this->getCount(); |
|
15 | 2 | if (!isset($count[0]['total']) || ($count[0]['total']===0)) { |
|
16 | return ['total' => 0, 'data' => null]; |
||
17 | } |
||
18 | 2 | $numberOfRows = $count[0]['total']; |
|
19 | 2 | $this->setSortOrders(); |
|
20 | 2 | $this->setOffsetAndLimit(); |
|
21 | 2 | $this->setReturnFields(); |
|
22 | 2 | $stmt = $this->conn->executeQuery( |
|
23 | 2 | $this->queryBuilder->getSql(), |
|
24 | 2 | $this->queryBuilder->getParameters() |
|
25 | ); |
||
26 | 2 | $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
|
27 | 2 | if ($this->distinctFieldName !== null) { |
|
28 | 1 | $numberOfRows = count($result); |
|
29 | } |
||
30 | 2 | return ['total' => $numberOfRows, 'data' => $result]; |
|
31 | } |
||
32 | |||
33 | 2 | private function getQueryBuilder() |
|
34 | { |
||
35 | 2 | if ($this->orFilters !== null) { |
|
36 | 2 | $this->andFilters[] = $this->orFilters; |
|
37 | } |
||
38 | 2 | $this->filters = $this->andFilters; |
|
39 | 2 | return $this->buildQuery($this->collection, $this->filters); |
|
40 | } |
||
41 | |||
42 | 2 | private function getCount() |
|
49 | |||
50 | 2 | private function setSortOrders() |
|
51 | { |
||
52 | 2 | if ($this->sortFields !== null) { |
|
53 | foreach ($this->addAlias($this->sortFields) as $sortKey => $sortDir) { |
||
|
|||
54 | $this->queryBuilder->addOrderBy($sortKey, $sortDir); |
||
55 | } |
||
56 | } |
||
57 | 2 | } |
|
58 | |||
59 | 2 | private function setJoins() |
|
66 | |||
67 | 2 | private function setJoinsForType($joinType) |
|
68 | { |
||
69 | 2 | if ($this->{$joinType} === null) { |
|
70 | 2 | return; |
|
71 | } |
||
72 | 1 | foreach ($this->{$joinType} as $collectionName => $collection) { |
|
73 | 1 | $fieldNames = $this->addAlias($collection['returnFields'], $collectionName); |
|
74 | 1 | $this->returnFieldsForJoin($fieldNames); |
|
75 | 1 | $joinCondition = ''; |
|
76 | 1 | foreach ($collection['relations'] as $relation) { |
|
77 | 1 | $joinCondition .= empty($joinCondition) ? '':' AND '; |
|
78 | 1 | $relationType = array_keys($relation)[0]; |
|
79 | 1 | $source = array_keys($relation[$relationType])[0]; |
|
80 | 1 | $condition = $collectionName . '.' . $source |
|
81 | 1 | . ' = ' . $this->collection . '.' . $relation[$relationType][$source]; |
|
82 | 1 | if ($relationType != 'field') { |
|
83 | 1 | $condition = $collectionName . '.' . $source . ' ' |
|
84 | 1 | . $relationType . ' '. $relation[$relationType][$source]; |
|
85 | } |
||
86 | 1 | $joinCondition .= $condition; |
|
87 | } |
||
88 | 1 | $this->queryBuilder->{$joinType}($this->collection, $collectionName, $collectionName, $joinCondition); |
|
89 | } |
||
90 | 1 | return $this; |
|
91 | } |
||
92 | 1 | public function returnFieldsForJoin(array $fieldNames = null) |
|
93 | { |
||
94 | 1 | if ($fieldNames !== null) { |
|
95 | 1 | foreach ($fieldNames as $fieldName) { |
|
96 | 1 | $this->fieldNames[] = $fieldName; |
|
97 | } |
||
98 | } |
||
99 | 1 | } |
|
100 | |||
101 | 2 | private function setReturnFields() |
|
112 | |||
113 | 2 | private function setOffsetAndLimit() |
|
118 | |||
119 | 2 | private function addAlias($fields, $collection = null) |
|
120 | { |
||
121 | 2 | $collection = (null !== $collection) ? $collection : $this->collection; |
|
122 | 2 | if (!is_array($fields)) { |
|
123 | 1 | return $collection . '.' . $fields; |
|
141 | |||
142 | 2 | protected function buildQuery($collection, $filters) |
|
151 | |||
152 | 2 | protected function buildQueryFilters($queryBuilder, $filters) |
|
163 | |||
164 | 2 | protected function buildQueryForAnd($queryBuilder, $key, $value) |
|
205 | |||
206 | |||
207 | 2 | public static function buildFilter($filter) |
|
246 | } |
||
247 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.