Passed
Push — master ( 80923f...9fa0e9 )
by Andrea
16:11
created

ParametriQueryTabellaDecoder::setInCriteria()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Tabella;
4
5
//use Doctrine\ORM\QueryBuilder;
6
use Doctrine\Common\Collections\Expr\Comparison;
7
use Doctrine\ORM\Query\Expr;
8
use Cdf\BiCoreBundle\Utils\FieldType\FieldTypeUtils;
9
10
class ParametriQueryTabellaDecoder extends BaseParametriQueryTabellaDecoder
11
{
12 3
    protected function buildQuery()
13
    {
14 3
        switch ($this->fieldoperator) {
15 3
            case Comparison::EQ:
16 3
                $this->setEqCriteria();
17 3
                break;
18 2
            case Comparison::NEQ:
19 1
                $this->setNeqCriteria();
20 1
                break;
21 2
            case Comparison::IN:
22 1
                $this->setInCriteria();
23 1
                break;
24 2
            case Comparison::NIN:
25 1
                $this->setNinCriteria();
26 1
                break;
27 2
            case Comparison::CONTAINS:
28 1
                $this->setContainsCriteria();
29 1
                break;
30 2
            case Comparison::STARTS_WITH:
31 1
                $this->setStartswithCriteria();
32 1
                break;
33 2
            case Comparison::ENDS_WITH:
34 1
                $this->setEndswithCriteria();
35 1
                break;
36
            default:
37 2
                $this->criteria = null;
38 2
                break;
39
        }
40 3
    }
41
42 3
    protected function setEqCriteria()
43
    {
44 3
        $expr = new Expr();
45
46 3
        if (null === $this->fieldvalue) {
47 1
            $this->criteria = $expr->isnull($this->fieldname);
48
        } else {
49 3
            if (is_a(FieldTypeUtils::extractDateTime($this->fieldvalue), "\DateTime")) {
0 ignored issues
show
Bug introduced by
It seems like Cdf\BiCoreBundle\Utils\F...Time($this->fieldvalue) can also be of type false; however, parameter $object of is_a() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            if (is_a(/** @scrutinizer ignore-type */ FieldTypeUtils::extractDateTime($this->fieldvalue), "\DateTime")) {
Loading history...
Coding Style Comprehensibility introduced by
The string literal \DateTime does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
50 1
                $this->criteria = $expr->eq($this->fieldname, ':'.$this->fieldqueryparameter);
51
            } else {
52 3
                if (is_string($this->fieldvalue)) {
53 1
                    $this->criteria = $expr->eq('lower('.$this->fieldname.')', 'lower(:'.$this->fieldqueryparameter.')');
54
                } else {
55 2
                    $this->criteria = $expr->eq($this->fieldname, ':'.$this->fieldqueryparameter);
56
                }
57
            }
58 3
            $this->parameters = array($this->fieldqueryparameter => $this->fieldvalue);
59
        }
60 3
    }
61
62 1
    protected function setNeqCriteria()
63
    {
64 1
        $expr = new Expr();
65
66 1
        if (null === $this->fieldvalue) {
67 1
            $this->criteria = $expr->isnotnull($this->fieldname);
68
        } else {
69 1
            if (is_a(FieldTypeUtils::extractDateTime($this->fieldvalue), "\DateTime")) {
0 ignored issues
show
Bug introduced by
It seems like Cdf\BiCoreBundle\Utils\F...Time($this->fieldvalue) can also be of type false; however, parameter $object of is_a() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            if (is_a(/** @scrutinizer ignore-type */ FieldTypeUtils::extractDateTime($this->fieldvalue), "\DateTime")) {
Loading history...
Coding Style Comprehensibility introduced by
The string literal \DateTime does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
70 1
                $this->criteria = $expr->neq($this->fieldname, ':'.$this->fieldqueryparameter);
71
            } else {
72 1
                if (is_string($this->fieldvalue)) {
73 1
                    $this->criteria = $expr->neq('lower('.$this->fieldname.')', 'lower(:'.$this->fieldqueryparameter.')');
74
                } else {
75
                    $this->criteria = $expr->neq($this->fieldname, ':'.$this->fieldqueryparameter);
76
                }
77
            }
78 1
            $this->parameters = array($this->fieldqueryparameter => $this->fieldvalue);
79
        }
80 1
    }
81
82 1
    protected function setNinCriteria()
83
    {
84 1
        $expr = new Expr();
85
86 1
        if (is_string($this->fieldvalue)) {
87
            $this->criteria = $expr->notin('lower('.$this->fieldname.')', 'lower(:'.$this->fieldqueryparameter.')');
88
        } else {
89 1
            $this->criteria = $expr->notin($this->fieldname, ':'.$this->fieldqueryparameter);
90
        }
91 1
        $this->parameters = array($this->fieldqueryparameter => $this->fieldvalue);
92 1
    }
93
94 1
    protected function setInCriteria()
95
    {
96 1
        $expr = new Expr();
97
98 1
        if (is_string($this->fieldvalue)) {
99
            $this->criteria = $expr->in('lower('.$this->fieldname.')', 'lower(:'.$this->fieldqueryparameter.')');
100
        } else {
101 1
            $this->criteria = $expr->in($this->fieldname, ':'.$this->fieldqueryparameter);
102
        }
103 1
        $this->parameters = array($this->fieldqueryparameter => $this->fieldvalue);
104 1
    }
105
106 1
    protected function setContainsCriteria()
107
    {
108 1
        $expr = new Expr();
109 1
        if (is_string($this->fieldvalue)) {
110 1
            $this->criteria = $expr->like('lower('.$this->fieldname.')', 'lower(:'.$this->fieldqueryparameter.')');
111
        } else {
112
            $this->criteria = $expr->like($this->fieldname, ':'.$this->fieldqueryparameter);
113
        }
114 1
        $this->parameters = array($this->fieldqueryparameter => '%'.$this->fieldvalue.'%');
115 1
    }
116
117 1
    protected function setStartswithCriteria()
118
    {
119 1
        $expr = new Expr();
120 1
        if (is_string($this->fieldvalue)) {
121 1
            $this->criteria = $expr->like('lower('.$this->fieldname.')', 'lower(:'.$this->fieldqueryparameter.')');
122
        } else {
123
            $this->criteria = $expr->like($this->fieldname, ':'.$this->fieldqueryparameter);
124
        }
125 1
        $this->parameters = array($this->fieldqueryparameter => $this->fieldvalue.'%');
126 1
    }
127
128 1
    protected function setEndswithCriteria()
129
    {
130 1
        $expr = new Expr();
131 1
        if (is_string($this->fieldvalue)) {
132 1
            $this->criteria = $expr->like('lower('.$this->fieldname.')', 'lower(:'.$this->fieldqueryparameter.')');
133
        } else {
134
            $this->criteria = $expr->like($this->fieldname, ':'.$this->fieldqueryparameter);
135
        }
136 1
        $this->parameters = array($this->fieldqueryparameter => '%'.$this->fieldvalue);
137 1
    }
138
}
139