Passed
Push — master ( 02cf1a...e288dc )
by Richard
01:43
created

ArrayFilter::processFilter()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 8
nc 9
nop 3
dl 0
loc 9
rs 7.0499
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Maphper\Lib;
3
use Maphper\Maphper;
4
5
class ArrayFilter {
6
    private $array;
7
8
    public function __construct(array $array) {
9
        $this->array = $array;
10
    }
11
12
    public function filter($fields) {
13
        $filteredArray = array_filter($this->array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND));
14
        // Need to reset indexes
15
        $filteredArray = array_values($filteredArray);
16
        return $filteredArray;
17
    }
18
19
    private function getSearchFieldFunction($fields, $mode) {
20
        return function ($data) use ($fields, $mode) {
21
            foreach ($fields as $key => $val) {
22
                $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode);
23
24
                if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true;
25
                else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false;
26
            }
27
            return !(Maphper::FIND_OR & $mode);
28
        };
29
    }
30
31
    private function getIfFieldMatches($key, $val, $data, $mode) {
32
        if (is_numeric($key) && is_array($val)) {
33
            return $this->getSearchFieldFunction($val, $key)($data);
34
        }
35
        else if (!isset($data->$key)) return false;
36
        else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val))
37
            return in_array($data->$key, $val);
38
        else
39
            return $this->processFilter($mode, $val, $data->$key);
40
    }
41
42
    private function processFilter($mode, $expected, $actual) {
43
        if (Maphper::FIND_NOT & $mode) return $expected != $actual;
44
        else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual;
45
        else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual;
46
        else if (Maphper::FIND_GREATER & $mode) return $expected < $actual;
47
        else if (Maphper::FIND_LESS & $mode) return $expected > $actual;
48
        else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1];
49
        else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual);
50
        return $expected == $actual;
51
    }
52
}
53