Completed
Push — master ( ea4111...dd4c41 )
by Vladimir
02:36
created

WhereFilter::compare()   C

Complexity

Conditions 15
Paths 11

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 15

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 15
eloc 14
c 2
b 0
f 0
nc 11
nop 4
dl 0
loc 22
ccs 15
cts 15
cp 1
crap 15
rs 5.8335

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\FrontMatterObject;
6
use allejo\stakx\Object\JailObject;
7
use Twig_Error_Syntax;
8
9
class WhereFilter
10
{
11
    /**
12
     * @param  array|\ArrayAccess[] $array      The elements to filter through
13
     * @param  string               $key        The key value in an associative array or FrontMatter
14
     * @param  string               $comparison The actual comparison symbols being used
15
     * @param  mixed                $value      The value we're searching for
16
     *
17
     * @return array
18
     */
19 21
    public function __invoke ($array, $key, $comparison, $value)
20
    {
21 21
        $results = array();
22 21
        $this->search_r($array, $key, $comparison, $value, $results);
23
24 20
        return $results;
25
    }
26
27
    /**
28
     * @return \Twig_SimpleFilter
29
     */
30 2
    public static function get ()
31
    {
32 2
        return new \Twig_SimpleFilter('where', new self());
33
    }
34
35
    /**
36
     * Recursive searching calling our comparison
37
     *
38
     * @param array|\ArrayAccess[] $array      The elements to filter through
39
     * @param string               $key        The key value in an associative array or FrontMatter
40
     * @param string               $comparison The actual comparison symbols being used
41
     * @param string               $value      The value we're searching for
42
     * @param array                $results    The reference to where to keep the filtered elements
43
     */
44 21
    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...
45
    {
46 21
        if (!is_array($array) && !($array instanceof \ArrayAccess))
47 21
        {
48 19
            return;
49
        }
50
51 21
        if ($this->compare($array, $key, $comparison, $value))
52 21
        {
53 15
            $results[] = $array;
54 15
        }
55
56 21
        foreach ($array as $subarray)
57
        {
58 21
            $this->search_r($subarray, $key, $comparison, $value, $results);
59 20
        }
60 20
    }
61
62
    /**
63
     * The logic for determining if an element matches the filter
64
     *
65
     * @param  array|\ArrayAccess[] $array      The elements to filter through
66
     * @param  string               $key        The key value in an associative array or FrontMatter
67
     * @param  string               $comparison The actual comparison symbols being used
68
     * @param  mixed                $value      The value we're searching for
69
     *
70
     * @return bool
71
     *
72
     * @throws Twig_Error_Syntax
73
     */
74 21
    private function compare ($array, $key, $comparison, $value)
75
    {
76 21
        if (!isset($array[$key]) &&
77 21
            !($array instanceof JailObject && $array->coreInstanceOf(FrontMatterObject::class) && $comparison == '==' && is_null($value)))
78 21
        {
79 21
            return false;
80
        }
81
82
        switch ($comparison)
83
        {
84 20
            case "==": return ((is_null($value) && !isset($array[$key])) || $array[$key] === $value);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
85 14
            case "!=": return ($array[$key] !== $value);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
86 11
            case ">" : return ($array[$key] > $value);
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
87 9
            case ">=": return ($array[$key] >= $value);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
88 7
            case "<" : return ($array[$key] < $value);
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
89 5
            case "<=": return ($array[$key] <= $value);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
90 3
            case "~=": return ($this->contains($array[$key], $value));
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
91
92 1
            default:
93 1
                throw new Twig_Error_Syntax("Invalid where comparison ({$comparison})");
94 1
        }
95
    }
96
97 2
    private function contains ($haystack, $needle)
98
    {
99 2
        return ((is_array($haystack) && in_array($needle, $haystack)) ||
100 2
                (is_string($haystack) && strpos($haystack, $needle) !== false));
101
    }
102
}