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

WhereFilter::compare()   C

Complexity

Conditions 16
Paths 384

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 16

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 16
eloc 11
c 4
b 0
f 1
nc 384
nop 4
dl 0
loc 17
rs 5.6252
ccs 12
cts 12
cp 1
crap 16

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
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
}