Completed
Push — master ( d95d7a...9b093a )
by Andrea
52:34 queued 49:57
created

setPrecondizioniAvanzate()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 60
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 9.2363

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 42
cts 49
cp 0.8571
rs 6.8358
c 0
b 0
f 0
cc 9
eloc 51
nc 9
nop 3
crap 9.2363

How to fix   Long Method   

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 Fi\CoreBundle\DependencyInjection;
4
5
class GrigliaDatiPrecondizioniUtils
6
{
7
    public static function setPrecondizioni(&$q, &$primo, $parametri = array())
8
    {
9
        $precondizioni = $parametri['precondizioni'];
10
11
        $i = 1;
12
        foreach ($precondizioni as $nomecampopre => $precondizione) {
13
            if ($primo) {
14
                $q->where("$nomecampopre = :var$i");
15
16
                $primo = false;
17
            } else {
18
                $q->andWhere("$nomecampopre = :var$i");
19
            }
20
            $q->setParameter("var$i", $precondizione);
21
            ++$i;
22
        }
23
    }
24
25 1
    public static function setPrecondizioniAvanzate(&$q, &$primo, $parametri = array())
26
    {
27 1
        $doctrine = $parametri['doctrine'];
28 1
        $nometabella = $parametri['nometabella'];
29 1
        $entityName = $parametri['entityName'];
30 1
        $bundle = $parametri['bundle'];
31 1
        $precondizioniAvanzate = $parametri['precondizioniAvanzate'];
32 1
        $regole = array();
33
34 1
        foreach ($precondizioniAvanzate as $elem) {
35 1
            $nometabellaprecondizione = '';
36 1
            $nomecampoprecondizione = '';
37 1
            $valorecampoprecondizione = '';
38 1
            $operatoreprecondizione = '=';
39 1
            $operatorelogicoprecondizione = '';
40 1
            foreach ($elem as $keypre => $valuepre) {
41 1
                switch ($keypre) {
42
                    case 'nometabella':
43 1
                        $nometabellaprecondizione = $valuepre;
44 1
                        break;
45
                    case 'nomecampo':
46 1
                        $nomecampoprecondizione = $valuepre;
47 1
                        break;
48
                    case 'operatore':
49 1
                        $array_operatori = array('=' => 'eq', '<>' => 'ne', '<' => 'lt', '<=' => 'le', '>' => 'gt', '>=' => 'ge', 'LIKE' => 'bw', 'NOT LIKE' => 'bn', 'IN' => 'in', 'NOT IN' => 'ni', 'LIKE' => 'eq', 'NOT LIKE' => 'en', 'LIKE' => 'cn', 'NOT LIKE' => 'nc', 'IS' => 'nu', 'IS NOT' => 'nn'); //, '<>' => 'nt');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50 1
                        $operatoreprecondizione = $array_operatori[strtoupper($valuepre)];
51 1
                        break;
52
                    case 'valorecampo':
53 1
                        if (is_array($valuepre)) {
54
                            $type = $doctrine->getClassMetadata($parametri['entityName'])->getFieldMapping($nomecampoprecondizione);
55
                            $valorecampoprecondizione = self::elaboravalorecampo($type, $valuepre);
56
                        } else {
57 1
                            $valorecampoprecondizione = $valuepre;
58
                        }
59 1
                        break;
60
                    case 'operatorelogico':
61 1
                        $operatorelogicoprecondizione = strtoupper($valuepre);
62 1
                        break;
63
                    default:
64 1
                        break;
65
                }
66
            }
67
68 1
            $regole[] = array(
69 1
                'field' => "$nometabellaprecondizione.$nomecampoprecondizione",
70 1
                'op' => $operatoreprecondizione,
71 1
                'data' => $valorecampoprecondizione, );
72 1
            $tipof = $operatorelogicoprecondizione;
73
            $regolearray = array(
74 1
                'regole' => $regole,
75 1
                'doctrine' => $doctrine,
76 1
                'nometabella' => $nometabella,
77 1
                'entityName' => $entityName,
78 1
                'bundle' => $bundle,
79 1
                'tipof' => $tipof,
80
            );
81 1
            GrigliaRegoleUtils::setRegole($q, $primo, $regolearray);
82 1
            $primo = false;
83
        }
84 1
    }
85
86
    private static function elaboravalorecampo($type, $valuepre)
87
    {
88
        $tipo = $type['type'];
89
        if ($tipo && ($tipo == 'date' || $tipo == 'datetime')) {
90
            GrigliaUtils::setVettoriPerData();
91
            foreach ($valuepre as $chiave => $valore) {
92
                $valuepre[$chiave] = fiUtilita::data2db($valore);
93
            }
94
            return implode(', ', $valuepre);
95
        } elseif ($tipo && $tipo == 'string') {
96
            GrigliaUtils::setVettoriPerStringa();
97
            foreach ($valuepre as $chiave => $valore) {
98
                $valuepre[$chiave] = strtolower("'" . $valore . "'");
99
            }
100
            return "'" . implode(', ', $valuepre) . "'";
101
        } else {
102
            GrigliaUtils::setVettoriPerNumero();
103
            return implode(', ', $valuepre);
104
        }
105
    }
106
}
107