GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Condition::parseString()   D
last analyzed

Complexity

Conditions 10
Paths 20

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 26
nc 20
nop 2
dl 0
loc 39
ccs 24
cts 24
cp 1
crap 10
rs 4.8196
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
3
4
namespace Nip\Database\Query\Condition;
5
6
use Nip\Database\Query\AbstractQuery as Query;
7
8
class Condition
9
{
10
11
    protected $_string;
12
    protected $_values;
13
    protected $_query;
14
15 13
    public function __construct($string, $values = array())
16
    {
17 13
        $this->_string = $string;
18 13
        $this->_values = $values;
19 13
    }
20
21 6
    public function __toString()
22
    {
23 6
        return $this->getString();
24
    }
25
26 12
    public function getString()
27
    {
28 12
        return $this->parseString($this->_string, $this->_values);
29
    }
30
31
    /**
32
     * Parses $string and replaces all instances of "?" with corresponding $values
33
     *
34
     * @param string $string
35
     * @param array $values
36
     * @return string
37
     */
38 12
    public function parseString($string, $values)
39
    {
40 12
        $positions = [];
41 12
        $pos = 0;
0 ignored issues
show
Unused Code introduced by
$pos is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
42 12
        $offset = 0;
43
44 12
        while (($pos = strpos($string, "?", $offset)) !== false) {
45 4
            $positions[] = $pos;
46 4
            $offset = $pos + 1;
47
        }
48
49 12
        $count = count($positions);
50
51 12
        if ($count == 1) {
52 4
            $values = array($values);
53
        }
54
55 12
        for ($i = 0; $i < $count; $i++) {
56 4
            $value = $values[$i];
57 4
            if ($value instanceof Query) {
58 1
                $value = $this->parseValueQuery($value);
59 3
            } elseif (is_array($value)) {
60 1
                foreach ($value as $key => $subvalue) {
61 1
                    if (trim($subvalue) != '') {
62 1
                        $value[$key] = is_numeric($subvalue) ? $subvalue : $this->getQuery()->getManager()->getAdapter()->quote($subvalue);
63
                    } else {
64 1
                        unset($value[$key]);
65
                    }
66
                }
67 1
                $value = "(" . implode(", ", $value) . ")";
68 2
            } elseif (is_numeric($value)) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
69
            } else {
70 2
                $value = $this->getQuery()->getManager()->getAdapter()->quote($values[$i]);
71
            }
72 4
            $string = substr_replace($string, $value, strpos($string, '?'), 1);
73
        }
74
75 12
        return $string;
76
    }
77
78 1
    protected function parseValueQuery($value)
79
    {
80 1
        return "(".$value->assemble().")";
81
    }
82
83
    /**
84
     * @return Query
85
     */
86 2
    public function getQuery()
87
    {
88 2
        return $this->_query;
89
    }
90
91
    /**
92
     * @param Query $query
93
     * @return $this
94
     */
95 13
    public function setQuery($query)
96
    {
97 13
        $this->_query = $query;
98
99 13
        return $this;
100
    }
101
102 5
    public function and_($condition)
103
    {
104 5
        return new AndCondition($this, $condition);
105
    }
106
107 4
    public function or_($condition)
108
    {
109 4
        return new OrCondition($this, $condition);
110
    }
111
112 7
    public function protectCondition($condition)
113
    {
114 7
        return strpos($condition, ' AND ') || strpos($condition, ' OR ') ? '('.$condition.')' : $condition;
115
    }
116
117
}