Completed
Push — master ( 3bdbda...1be64b )
by Vladimir
02:48
created

WhereFilter   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 57
rs 10
ccs 33
cts 33
cp 1
wmc 27
lcom 0
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 7 1
B search_r() 0 17 5
A get() 0 4 1
C compare() 0 17 16
A contains() 0 5 4
1
<?php
2
3
namespace allejo\stakx\Twig;
4
5
use allejo\stakx\Object\ContentItem;
6
7
class WhereFilter
8
{
9 20
    public function __invoke ($array, $key, $comparison, $value)
10
    {
11 20
        $results = array();
12 20
        $this->search_r($array, $key, $comparison, $value, $results);
13
14 20
        return $results;
15
    }
16
17 1
    public static function get ()
18
    {
19 1
        return new \Twig_SimpleFilter('where', new self());
20
    }
21
22 20
    private function search_r ($array, $key, $comparison, $value, &$results)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
23
    {
24 20
        if (!is_array($array) && !($array instanceof ContentItem))
25 20
        {
26 20
            return;
27
        }
28
29 20
        if ($this->compare($array, $key, $comparison, $value))
30 20
        {
31 14
            $results[] = $array;
32 14
        }
33
34 20
        foreach ($array as $subarray)
0 ignored issues
show
Bug introduced by
The expression $array of type array|object<allejo\stakx\Object\ContentItem> 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...
35
        {
36 20
            $this->search_r($subarray, $key, $comparison, $value, $results);
37 20
        }
38 20
    }
39
40 20
    private function compare ($array, $key, $comparison, $value)
41
    {
42 20
        $array = ($array instanceof ContentItem) ? $array->getFrontMatter() : $array;
43
44 20
        if (!isset($array[$key]))
45 20
        {
46 20
            return false;
47
        }
48
49 19
        return (($comparison === "==" && $array[$key] === $value) ||
50 19
                ($comparison === "!=" && $array[$key] !== $value) ||
51 19
                ($comparison === ">"  && $array[$key] > $value)   ||
52 19
                ($comparison === ">=" && $array[$key] >= $value)  ||
53 19
                ($comparison === "<"  && $array[$key] < $value)   ||
54 19
                ($comparison === "<=" && $array[$key] <= $value)  ||
55 19
                ($comparison === "~=" && $this->contains($array[$key], $value)));
56
    }
57
58 2
    private function contains ($haystack, $needle)
59
    {
60 2
        return ((is_array($haystack) && in_array($needle, $haystack)) ||
61 2
                (is_string($haystack) && strpos($haystack, $needle) !== false));
62
    }
63
}