Completed
Push — master ( fdbb40...a3bd88 )
by Mehmet
04:09
created

SQLQueryBuilder::setSortOrders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 7
cp 0.4286
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
crap 4.679
1
<?php
2
3
namespace Soupmix;
4
5
6
class SQLQueryBuilder extends AbstractQueryBuilder
7
{
8
9
    private $queryBuilder;
10
11 1
    public function run(){
12 1
        $this->queryBuilder = $this->getQueryBuilder();
13 1
        $count = $this->getCount();
14 1
        $numberOfRows = 0;
15 1
        if (isset($count[0]['total']) && ($count[0]['total']>0)) {
16 1
            $numberOfRows = $count[0]['total'];
17 1
            $this->setSortOrders();
18 1
            $this->setReturnFields();
19 1
            $this->setOffsetAndLimit();
20 1
            $stmt = $this->soupmix->getConnection()
21 1
                ->executeQuery(
22 1
                    $this->queryBuilder->getSql(),
23 1
                    $this->queryBuilder->getParameters()
24 1
                );
25 1
            $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
26 1
            if($this->distinctFieldName !== null){
27 1
                $numberOfRows = count($result);
28 1
            }
29 1
        }
30 1
        return ['total' => $numberOfRows, 'data' => $result];
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
31
    }
32
33 1
    private function getQueryBuilder()
34
    {
35 1
        $this->andFilters[] = $this->orFilters;
36 1
        $this->filters      = $this->andFilters;
37 1
        return $this->soupmix->buildQuery($this->collection, $this->filters);
38
    }
39
40 1
    private function getCount()
41
    {
42 1
        $queryBuilderCount = clone $this->queryBuilder;
43 1
        $queryBuilderCount->select(" COUNT(*) AS total ");
44 1
        $stmt = $this->soupmix->getConnection()->executeQuery($queryBuilderCount->getSql(), $queryBuilderCount->getParameters());
45 1
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
46
    }
47
48 1
    private function setSortOrders()
49
    {
50 1
        if ($this->sortFields !== null) {
51
            foreach ($this->sortFields as $sort_key => $sort_dir) {
52
                $this->queryBuilder->addOrderBy($sort_key, $sort_dir);
53
            }
54
        }
55 1
    }
56
57 1
    private function setReturnFields()
58
    {
59 1
        if ($this->distinctFieldName === null) {
60 1
            $fieldNames = ($this->fieldNames === null) ? "*" : $this->fieldNames;
61 1
            $this->queryBuilder->select($fieldNames);
62 1
            return;
63
        }
64 1
        $this->queryBuilder->select('DISTINCT (`' . $this->distinctFieldName . '`)');
65 1
    }
66
67 1
    private function setOffsetAndLimit()
68
    {
69 1
        $this->queryBuilder->setFirstResult($this->offset)
70 1
            ->setMaxResults($this->limit);
71
    }
72
}