Completed
Push — master ( 2e405d...fdbb40 )
by Mehmet
06:00
created

SQLQueryBuilder::run()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 37
Code Lines 29

Duplication

Lines 6
Ratio 16.22 %

Code Coverage

Tests 26
CRAP Score 10.0551

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 6
loc 37
ccs 26
cts 34
cp 0.7647
rs 4.909
cc 9
eloc 29
nc 13
nop 0
crap 10.0551
1
<?php
2
3
namespace Soupmix;
4
5
6
class SQLQueryBuilder extends AbstractQueryBuilder
7
{
8
9 1
    public function run(){
10 1
        $this->andFilters[]= $this->orFilters;
11 1
        $this->filters    = $this->andFilters;
12 1
        $queryBuilder = $this->soupmix->buildQuery($this->collection, $this->filters);
13 1
        $queryBuilderCount = clone $queryBuilder;
14 1
        $queryBuilderCount->select(" COUNT(*) AS total ");
15 1
        $stmt = $this->soupmix->getConnection()->executeQuery($queryBuilderCount->getSql(), $queryBuilderCount->getParameters());
16 1
        $count = $stmt->fetchAll(\PDO::FETCH_ASSOC);
17 1
        $numberOfSet = 0;
18 1
        if (isset($count[0]['total']) && ($count[0]['total']>0)) {
19 1
            $numberOfSet = $count[0]['total'];
20 1
            if ($this->sortFields !== null) {
21
                $params['sort'] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
22 View Code Duplication
                foreach ($this->sortFields as $sort_key => $sort_dir) {
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...
23
                    if ($params['sort']!='') {
24
                        $params['sort'] .= ',';
25
                    }
26
                    $queryBuilder->addOrderBy($sort_key, $sort_dir);
27
                }
28
            }
29 1
            if ($this->distinctFieldName === null) {
30 1
                $fieldNames = ($this->fieldNames === null) ? "*" : $this->fieldNames;
31 1
                $queryBuilder->select($fieldNames)
32 1
                    ->setFirstResult($this->offset)
33 1
                    ->setMaxResults($this->limit);
34 1
            }
35
            else {
36 1
                $queryBuilder->select('DISTINCT (`' . $this->distinctFieldName . '`)');
37
            }
38 1
            $stmt = $this->soupmix->getConnection()->executeQuery($queryBuilder->getSql(), $queryBuilder->getParameters());
39 1
            $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
40 1
            if($this->distinctFieldName !== null){
41 1
                $numberOfSet = count($result);
42 1
            }
43 1
        }
44 1
        return ['total' => $numberOfSet, '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...
45
    }
46
}