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

WhereFilter::contains()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 5
rs 9.2
ccs 3
cts 3
cp 1
crap 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
}