Completed
Pull Request — master (#119)
by Franco
02:40
created

RangeFilter   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 79
Duplicated Lines 45.57 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 29
lcom 2
cbo 3
dl 36
loc 79
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B applyMany() 15 15 5
A isEmpty() 0 5 4
B applyOne() 0 13 5
B excludeOne() 0 13 5
B excludeMany() 15 15 5
B setValue() 6 9 5

How to fix   Duplicated Code   

Duplicated Code

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:

1
<?php
2
3
/**
4
 * Class RangeFilter
5
 *
6
 * @package dms
7
 */
8
9
class RangeFilter extends SearchFilter
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11 View Code Duplication
    protected function applyMany(DataQuery $query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
12
    {
13
        $this->model = $query->applyRelation($this->relation);
0 ignored issues
show
Documentation Bug introduced by
It seems like $query->applyRelation($this->relation) of type object<The> is incompatible with the declared type string of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
14
        $where = array();
15
        $value = $this->getValue();
16
        $dbName = $this->getDbName();
17
        if (isset($value['min']) && $value['min']) {
18
            $where[] = sprintf("%s > '%s'", $dbName, Convert::raw2sql($value['min']));
19
        }
20
21
        if (isset($value['max']) && $value['max']) {
22
            $where[] = sprintf("%s <= '%s'", $dbName, Convert::raw2sql($value['max']));
23
        }
24
        return $query->where(implode(' AND ', $where));
25
    }
26
27
    public function isEmpty()
28
    {
29
        $value = $this->getValue();
30
        return !(isset($value['min']) && $value['min']) && !(isset($value['max']) && $value['max']);
31
    }
32
33
    // @codeCoverageIgnoreStart
34
    protected function applyOne(DataQuery $query)
35
    {
36
        $this->model = $query->applyRelation($this->relation);
0 ignored issues
show
Documentation Bug introduced by
It seems like $query->applyRelation($this->relation) of type object<The> is incompatible with the declared type string of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $where = array();
0 ignored issues
show
Unused Code introduced by
$where is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38
        $value = $this->getValue();
39
        $dbName = $this->getDbName();
40
        if (isset($value['min']) && $value['min']) {
41
            $filter = sprintf("%s > '%s'", $dbName, Convert::raw2sql($value['min']));
42
        } elseif (isset($value['max']) && $value['max']) {
43
            $filter = sprintf("%s <= '%s'", $dbName, Convert::raw2sql($value['max']));
44
        }
45
        return $query->where($filter);
0 ignored issues
show
Bug introduced by
The variable $filter 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...
46
    }
47
    protected function excludeOne(DataQuery $query)
48
    {
49
        $this->model = $query->applyRelation($this->relation);
0 ignored issues
show
Documentation Bug introduced by
It seems like $query->applyRelation($this->relation) of type object<The> is incompatible with the declared type string of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
        $where = array();
0 ignored issues
show
Unused Code introduced by
$where is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
        $value = $this->getValue();
52
        $dbName = $this->getDbName();
53
        if (isset($value['min']) && $value['min']) {
54
            $filter = sprintf("%s <= '%s'", $dbName, Convert::raw2sql($value['min']));
55
        } elseif (isset($value['max']) && $value['max']) {
56
            $filter = sprintf("%s > '%s'", $dbName, Convert::raw2sql($value['max']));
57
        }
58
        return $query->where($filter);
0 ignored issues
show
Bug introduced by
The variable $filter 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...
59
    }
60
61 View Code Duplication
    protected function excludeMany(DataQuery $query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
62
    {
63
        $this->model = $query->applyRelation($this->relation);
0 ignored issues
show
Documentation Bug introduced by
It seems like $query->applyRelation($this->relation) of type object<The> is incompatible with the declared type string of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
        $where = array();
65
        $value = $this->getValue();
66
        $dbName = $this->getDbName();
67
        if (isset($value['min']) && $value['min']) {
68
            $where[] = sprintf("%s <= '%s'", $dbName, Convert::raw2sql($value['min']));
69
        }
70
71
        if (isset($value['max']) && $value['max']) {
72
            $where[] = sprintf("%s > '%s'", $dbName, Convert::raw2sql($value['max']));
73
        }
74
        return $query->where(implode(' AND ', $where));
75
    }
76
    // @codeCoverageIgnoreEnd
77
78
    public function setValue($value)
79
    {
80 View Code Duplication
        if (isset($value['min']) && $value['min']) {
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...
81
            $this->value['min'] = $value['min']." 00:00:00";
82
        }
83 View Code Duplication
        if (isset($value['max']) && $value['max']) {
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...
84
            $this->value['max'] = $value['max']." 23:59:59";
85
        }
86
    }
87
}
88