Completed
Push — master ( be044a...4ae9fd )
by Mehmet
04:35
created

SQLQueryBuilder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.84%

Importance

Changes 7
Bugs 2 Features 1
Metric Value
wmc 14
c 7
b 2
f 1
lcom 1
cbo 2
dl 0
loc 69
ccs 45
cts 49
cp 0.9184
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 21 4
A getQueryBuilder() 0 8 2
A getCount() 0 7 1
A setSortOrders() 0 8 3
A setReturnFields() 0 9 3
A setOffsetAndLimit() 0 5 1
1
<?php
2
3
namespace Soupmix;
4
5
6
class SQLQueryBuilder extends AbstractQueryBuilder
7
{
8
9
    private $queryBuilder;
10
11 2
    public function run(){
12 2
        $this->queryBuilder = $this->getQueryBuilder();
13 2
        $count = $this->getCount();
14 2
        $numberOfRows = 0;
15 2
        if (isset($count[0]['total']) && ($count[0]['total']>0)) {
16 2
            $numberOfRows = $count[0]['total'];
17 2
            $this->setSortOrders();
18 2
            $this->setReturnFields();
19 2
            $this->setOffsetAndLimit();
20 2
            $stmt = $this->soupmix->getConnection()
21 2
                ->executeQuery(
22 2
                    $this->queryBuilder->getSql(),
23 2
                    $this->queryBuilder->getParameters()
24 2
                );
25 2
            $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
26 2
            if($this->distinctFieldName !== null){
27 1
                $numberOfRows = count($result);
28 1
            }
29 2
        }
30 2
        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 2
    private function getQueryBuilder()
34
    {
35 2
        if ($this->orFilters !== null){
36 2
            $this->andFilters[] = $this->orFilters;
37 2
        }
38 2
        $this->filters      = $this->andFilters;
39 2
        return $this->soupmix->buildQuery($this->collection, $this->filters);
40
    }
41
42 2
    private function getCount()
43
    {
44 2
        $queryBuilderCount = clone $this->queryBuilder;
45 2
        $queryBuilderCount->select(" COUNT(*) AS total ");
46 2
        $stmt = $this->soupmix->getConnection()->executeQuery($queryBuilderCount->getSql(), $queryBuilderCount->getParameters());
47 2
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
48
    }
49
50 2
    private function setSortOrders()
51
    {
52 2
        if ($this->sortFields !== null) {
53
            foreach ($this->sortFields as $sortKey => $sortDir) {
54
                $this->queryBuilder->addOrderBy($sortKey, $sortDir);
55
            }
56
        }
57 2
    }
58
59 2
    private function setReturnFields()
60
    {
61 2
        if ($this->distinctFieldName === null) {
62 2
            $fieldNames = ($this->fieldNames === null) ? "*" : $this->fieldNames;
63 2
            $this->queryBuilder->select($fieldNames);
64 2
            return;
65
        }
66 1
        $this->queryBuilder->select('DISTINCT (`' . $this->distinctFieldName . '`)');
67 1
    }
68
69 2
    private function setOffsetAndLimit()
70
    {
71 2
        $this->queryBuilder->setFirstResult($this->offset)
72 2
            ->setMaxResults($this->limit);
73
    }
74
}