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

ExactHierarchyOrFilter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 77.27 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 51
loc 66
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setModifiers() 11 11 2
A applyOne() 0 4 1
A excludeOne() 0 4 1
A excludeMany() 20 20 3
A applyMany() 20 20 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
/**
4
 * Class ExactHierarchyOrFilter
5
 *
6
 * @package dms
7
 */
8
9
class ExactHierarchyOrFilter 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
    // @codeCoverageIgnoreStart
12 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...
13
    {
14
        if (($extras = array_diff($modifiers, array('not'))) != array()) {
15
            throw new InvalidArgumentException(
16
                get_class($this) . 'only used for filtering many_many relation, so does not accept '
17
                .implode(', ', $extras) . ' as modifiers'
18
            );
19
        }
20
21
        parent::setModifiers($modifiers);
22
    }
23
24
    protected function applyOne(DataQuery $query)
25
    {
26
        /* NO OP */
27
    }
28
    protected function excludeOne(DataQuery $query)
29
    {
30
        /* NO OP */
31
    }
32
33 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...
34
    {
35
        $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...
36
        $values = array();
37
        $relationModelClass = $this->model;
38
        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...
39
            $relationModel = $relationModelClass::get_by_id($relationModelClass, (int)$value);
40
            if ($relationModel) {
41
                $values = array_merge($values, $relationModel->getHierarchicalIDs());
42
            }
43
        }
44
45
        $valueStr = "'" . implode("', '", $values) . "'";
46
        $dbName = singleton($relationModelClass)->getDBName();
47
        return $query->where(sprintf(
48
            '%s NOT IN (%s)',
49
            $dbName,
50
            $valueStr
51
        ));
52
    }
53
    // @codeCoverageIgnoreEnd
54 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...
55
    {
56
        $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...
57
        $values = array();
58
        $relationModelClass = $this->model;
59
        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...
60
            $relationModel = $relationModelClass::get_by_id($relationModelClass, (int)$value);
61
            if ($relationModel) {
62
                $values = array_merge($values, $relationModel->getHierarchicalIDs());
63
            }
64
        }
65
66
        $valueStr = "'" . implode("', '", $values) . "'";
67
        $dbName = singleton($relationModelClass)->getDBName();
68
        return $query->where(sprintf(
69
            '%s IN (%s)',
70
            $dbName,
71
            $valueStr
72
        ));
73
    }
74
}
75