Completed
Push — master ( fdc4ba...1c2208 )
by Alexander
02:42
created

Filter2::matchesEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Ember Db - An embeddable document database for php.
4
 * Copyright (C) 2016 Alexander During
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * @link      http://github.com/alexanderduring/php-ember-db
20
 * @copyright Copyright (C) 2016 Alexander During
21
 * @license   http://www.gnu.org/licenses GNU General Public License v3.0
22
 */
23
24
namespace EmberDb\Filter;
25
26
class Filter2
27
{
28
    private $operatorHierarchy = array();
29
30
31
32
    public function __construct($filterArray)
33
    {
34
        $this->operatorHierarchy = $this->buildOperatorHierarchy($filterArray);
35
    }
36
37
38
39
    public function matchesEntry(array $entry)
40
    {
41
        $isMatch = $this->matchesArray($this->filterData, $entry);
0 ignored issues
show
Bug introduced by
The property filterData 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...
42
43
        return $isMatch;
44
    }
45
46
47
48 View Code Duplication
    private function matchesArray($filterArray, $entryArray)
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...
49
    {
50
        $isMatch = true;
51
        foreach ($filterArray as $filterKey => $filterValue) {
52
53
            // Check if the filter key exists and fetch corresponding value from entry
54
            if (array_key_exists($filterKey, $entryArray)) {
55
                $entryValue = $entryArray[$filterKey];
56
            } else {
57
                $isMatch = false;
58
                break;
59
            }
60
61
            // Check if the values match
62
            $isMatch &= $this->matchesValue($filterValue, $entryValue);
63
        }
64
65
        return $isMatch;
66
    }
67
68
69
70 View Code Duplication
    private function matchesValue($filterValue, $entryValue)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
71
    {
72
        $isMatch = false;
73
74
        // If both are an array ...
75
        if (is_array($filterValue) && is_array($entryValue)) {
76
            $isMatch = $this->matchesArray($filterValue, $entryValue);
77
        }
78
79
        // If both aren't ...
80
        if (!is_array($filterValue) && !is_array($entryValue)) {
81
            $isMatch = $filterValue === $entryValue;
82
        }
83
84
        // If filter is an operator ...
85
        $operatorManager = new OperatorManager();
86
        if ($operatorManager->isOperator($filterValue)) {
87
            $operator = $operatorManager->createOperator($filterValue);
88
            $isMatch = $operator->matches($entryValue);
89
        }
90
91
        return $isMatch;
92
    }
93
94
95
96
    private function buildOperatorHierarchy($filterArray)
97
    {
98
        $hierarchy = array();
99
        foreach ($filterArray as $queryKey => $queryValue) {
100
            $hierarchy[$queryKey] = $this->buildOperator($queryValue);
101
        }
102
103
        return $hierarchy;
104
    }
105
106
107
108
    private function buildOperator($operatorArray)
109
    {
110
        $operatorManager = new OperatorManager();
111
        if ($operatorManager->isOperator($operatorArray)) {
112
            $operator = $operatorManager->createOperator($operatorArray);
113
        }
114
115
        return $operator;
0 ignored issues
show
Bug introduced by
The variable $operator 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...
116
    }
117
}
118