Completed
Push — master ( 9a7f03...117e39 )
by Mehmet
02:52
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 0
CRAP Score 90

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 6
loc 37
ccs 0
cts 1
cp 0
rs 4.909
cc 9
eloc 29
nc 13
nop 0
crap 90
1
<?php
2
3
namespace Soupmix;
4
5
6
class SQLQueryBuilder extends AbstractQueryBuilder
7
{
8
9
    public function run(){
10
        $this->andFilters[]= $this->orFilters;
0 ignored issues
show
Bug introduced by
The property andFilters cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Bug introduced by
The property orFilters cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
11
        $this->filters    = $this->andFilters;
0 ignored issues
show
Bug introduced by
The property filters cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Bug introduced by
The property andFilters cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
12
        $queryBuilder = $this->soupmix->buildQuery($this->collection, $this->filters);
0 ignored issues
show
Bug introduced by
The property collection cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Bug introduced by
The property filters cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Bug introduced by
The property soupmix does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
13
        $queryBuilderCount = clone $queryBuilder;
14
        $queryBuilderCount->select(" COUNT(*) AS total ");
15
        $stmt = $this->soupmix->getConnection()->executeQuery($queryBuilderCount->getSql(), $queryBuilderCount->getParameters());
16
        $count = $stmt->fetchAll(\PDO::FETCH_ASSOC);
17
        $numberOfSet = 0;
18
        if (isset($count[0]['total']) && ($count[0]['total']>0)) {
19
            $numberOfSet = $count[0]['total'];
20
            if ($this->sortFields !== null) {
0 ignored issues
show
Bug introduced by
The property sortFields cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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
Bug introduced by
The property sortFields cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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
            if ($this->distinctFieldName === null) {
0 ignored issues
show
Bug introduced by
The property distinctFieldName cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
30
                $fieldNames = ($this->fieldNames === null) ? "*" : $this->fieldNames;
0 ignored issues
show
Bug introduced by
The property fieldNames cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
31
                $queryBuilder->select($fieldNames)
32
                    ->setFirstResult($this->offset)
0 ignored issues
show
Bug introduced by
The property offset cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
33
                    ->setMaxResults($this->limit);
0 ignored issues
show
Bug introduced by
The property limit cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
34
            }
35
            else {
36
                $queryBuilder->select('DISTINCT (`' . $this->distinctFieldName . '`)');
0 ignored issues
show
Bug introduced by
The property distinctFieldName cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
37
            }
38
            $stmt = $this->soupmix->getConnection()->executeQuery($queryBuilder->getSql(), $queryBuilder->getParameters());
39
            $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
40
            if($this->distinctFieldName !== null){
0 ignored issues
show
Bug introduced by
The property distinctFieldName cannot be accessed from this context as it is declared private in class Soupmix\AbstractQueryBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
41
                $numberOfSet = count($result);
42
            }
43
        }
44
        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
}