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

ExactAndFilter   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 131
Duplicated Lines 45.8 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 3
dl 60
loc 131
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setModifiers() 11 11 2
A applyOne() 0 4 1
B applyMany() 26 39 6
A excludeOne() 0 4 1
B excludeMany() 23 35 6
A isEmpty() 0 4 3

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
class ExactAndFilter 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...
4
{
5
6
    // @codeCoverageIgnoreStart
7 View Code Duplication
    public function setModifiers(array $modifiers)
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...
8
    {
9
        if (($extras = array_diff($modifiers, array('not'))) != array()) {
10
            throw new InvalidArgumentException(
11
                get_class($this) . 'only used for filtering many_many relation, so does not accept '
12
                .implode(', ', $extras) . ' as modifiers'
13
            );
14
        }
15
16
        parent::setModifiers($modifiers);
17
    }
18
    /**
19
     * Applies an exact match (equals) on a field value.
20
     *
21
     * @return DataQuery
0 ignored issues
show
Documentation introduced by
Should the return type not be DataQuery|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
22
     */
23
    protected function applyOne(DataQuery $query)
24
    {
25
        /* NO OP */
26
    }
27
    // @codeCoverageIgnoreEnd
28
29
    /**
30
     * Applies an exact match (equals) on a field value against multiple
31
     * possible values.
32
     *
33
     * @return DataQuery
34
     */
35
    protected function applyMany(DataQuery $query)
36
    {
37
        $idDBName = '"'.$this->model.'"."ID"';
38
        $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...
39
        $values = array();
40
        foreach ($this->getValue() as $value) {
0 ignored issues
show
Bug introduced by
The expression $this->getValue() of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
41
            $values[] = Convert::raw2sql($value);
42
        }
43
44
        $ids = array();
45
        //$dl = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46 View Code Duplication
        foreach ($values as &$v) {
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...
47
            $v = $this->getDbName()." IN ('". $v ."')";
48
            $dl= $query->where($v);
49
            $ids[] = $dl->column('ID');
50
51
            $query->removeFilterOn($v);
52
        }
53 View Code Duplication
        if (!empty($ids)) {
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...
54
            $intersect = $ids[0];
55
            foreach ($ids as $id) {
56
                $intersect = array_intersect($intersect, $id);
57
            }
58
        }
59
60 View Code Duplication
        if (!empty($intersect)) {
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...
61
            $valueStr = "'" . implode("', '", $intersect) . "'";
62
            return $query->where(sprintf(
63
                '%s IN (%s)',
64
                $idDBName,
65
                $valueStr
66
            ));
67
        } else {
68
            return $query->where(sprintf(
69
                '%s IN (-1)',
70
                $idDBName
71
            ));
72
        }
73
    }
74
75
    // @codeCoverageIgnoreStart
76
    /**
77
     * Excludes an exact match (equals) on a field value.
78
     *
79
     * @return DataQuery
0 ignored issues
show
Documentation introduced by
Should the return type not be DataQuery|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
80
     */
81
    protected function excludeOne(DataQuery $query)
82
    {
83
        /* NO OP */
84
    }
85
86
    /**
87
     * Excludes an exact match (equals) on a field value against multiple
88
     * possible values.
89
     *
90
     * @return DataQuery
91
     */
92
    protected function excludeMany(DataQuery $query)
93
    {
94
        $dbName = $this->getDBName();
95
        $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...
96
        $values = array();
97
        foreach ($this->getValue() as $value) {
0 ignored issues
show
Bug introduced by
The expression $this->getValue() of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
98
            $values[] = Convert::raw2sql($value);
99
        }
100
101
        $ids = array();
102
        //$dl = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103 View Code Duplication
        foreach ($values as &$v) {
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...
104
            $v = $this->getDbName()." IN ('". $v ."')";
105
            $dl= $query->where($v);
106
            $ids[] = $dl->column('ID');
107
108
            $query->removeFilterOn($v);
109
        }
110 View Code Duplication
        if (!empty($ids)) {
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...
111
            $intersect = $ids[0];
112
            foreach ($ids as $id) {
113
                $intersect = array_intersect($intersect, $id);
114
            }
115
        }
116 View Code Duplication
        if (!empty($intersect)) {
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...
117
            $valueStr = "'" . implode("', '", $intersect) . "'";
118
            return $query->where(sprintf(
119
                '%s NOT IN (%s)',
120
                $dbName,
121
                $valueStr
122
            ));
123
        } else {
124
            return $query->where(true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|array|object<SQLConditionGroup>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
125
        }
126
    }
127
    // @codeCoverageIgnoreEnd
128
    
129
    public function isEmpty()
130
    {
131
        return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
132
    }
133
}
134