Completed
Push — master ( 166881...6b19b1 )
by Andrea
09:19
created

TabelleController::aggiornaAction()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 60
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 0
cts 33
cp 0
rs 8.9618
c 0
b 0
f 0
cc 4
eloc 33
nc 5
nop 1
crap 20

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\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Fi\CoreBundle\Entity\tabelle;
9
10
/**
11
 * Tabelle controller.
12
 */
13
class TabelleController extends FiCoreController
14
{
15
16
    public function aggiornaAction(Request $request)
17
    {
18
        if ($request->get('oper') == 'edit') {
19
            $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
20
            $operatore = $gestionepermessi->utentecorrente();
21
22
            $id = $request->get('id');
23
24
            $em = $this->getDoctrine()->getManager();
25
            $tabelle = $em->getRepository('FiCoreBundle:Tabelle')->find($id);
26
            $tabelle->setOperatoriId($operatore['id']);
27
            $nometabella = $this->getRequestValue($request, 'nometabella');
28
            if ($nometabella) {
29
                $tabelle->setNometabella($nometabella);
30
            }
31
            $nomecampo = $this->getRequestValue($request, 'nomecampo');
32
            if ($nomecampo) {
33
                $tabelle->setNomecampo($nomecampo);
34
            }
35
            $mostraindex = $this->getRequestValue($request, 'mostraindex');
36
            $tabelle->setMostraindex($mostraindex);
37
            $ordineindex = $this->getRequestValue($request, 'ordineindex');
38
            $tabelle->setOrdineindex($ordineindex);
39
            $etichettaindex = $this->getRequestValue($request, 'etichettaindex');
40
            $tabelle->setEtichettaindex($etichettaindex);
41
42
            $larghezzaindex = $this->getRequestValue($request, 'larghezzaindex');
43
            $tabelle->setLarghezzaindex($larghezzaindex);
44
45
            $mostrastampa = $this->getRequestValue($request, 'mostrastampa');
46
            $tabelle->setMostrastampa($mostrastampa);
47
48
            $ordinestampa = $this->getRequestValue($request, 'ordinestampa');
49
            $tabelle->setOrdinestampa($ordinestampa);
50
51
            $etichettastampa = $this->getRequestValue($request, 'etichettastampa');
52
            $tabelle->setEtichettastampa($etichettastampa);
53
54
            $larghezzastampa = $this->getRequestValue($request, 'larghezzastampa');
55
            $tabelle->setLarghezzastampa($larghezzastampa);
56
            $em->persist($tabelle);
0 ignored issues
show
Bug introduced by
It seems like $tabelle defined by $em->getRepository('FiCo...le:Tabelle')->find($id) on line 25 can also be of type null; however, Doctrine\Common\Persiste...bjectManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
57
            $em->flush();
58
        }
59
60
        /* operatori_id int(11)
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
61
          nometabella   varchar(45)
62
          nomecampo     varchar(45)
63
          mostraindex   tinyint(1)
64
          ordineindex   int(11)
65
          larghezzaindex        int(11)
66
          etichettaindex        varchar(255)
67
          mostrastampa  tinyint(1)
68
          ordinestampa  int(11)
69
          larghezzastampa       int(11)
70
          etichettastampa       varchar(255)
71
         *
72
         */
73
74
        return new Response('OK');
75
    }
76
77
    private function getRequestValue($request, $attribute)
78
    {
79
        if (($request->get($attribute) !== null) && ($request->get($attribute) !== '')) {
80
            return $request->get($attribute);
81
        } else {
82
            return null;
83
        }
84
    }
85
86 1
    public function configuraAction(Request $request, $nometabella)
87
    {
88 1
        $this->setup($request);
89 1
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
90 1
        $operatore = $gestionepermessi->utentecorrente();
91 1
        $this->generaDB(array('tabella' => $nometabella), $request);
92 1
        $this->generaDB(array('tabella' => $nometabella, 'operatore' => $operatore['id']), $request);
93
94 1
        $namespace = $this->getNamespace();
95 1
        $bundle = $this->getBundle();
96 1
        $controller = $this->getController();
97 1
        $container = $this->container;
98
99 1
        $nomebundle = $namespace . $bundle . 'Bundle';
100
101 1
        $em = $this->getDoctrine()->getManager();
102 1
        $entities = $em->getRepository($nomebundle . ':' . $controller)->findAll();
103
104
        $dettaglij = array(
105 1
            'nomecampo' => array(
106
                array('nomecampo' => 'nomecampo', 'lunghezza' => '150', 'descrizione' => 'Campo', 'tipo' => 'text', 'editable' => false),),
107
            'mostraindex' => array(
108
                array('nomecampo' => 'mostraindex', 'lunghezza' => '100', 'descrizione' => 'Vedi in griglia', 'tipo' => 'boolean'),
109
            ),
110
            'ordineindex' => array(
111
                array('nomecampo' => 'ordineindex', 'lunghezza' => '100', 'descrizione' => 'Ordine in griglia', 'tipo' => 'text'),
112
            ),
113
            'etichettaindex' => array(
114
                array('nomecampo' => 'etichettaindex', 'lunghezza' => '150', 'descrizione' => 'Label in griglia', 'tipo' => 'text'),
115
            ),
116
            'larghezzaindex' => array(
117
                array('nomecampo' => 'larghezzaindex', 'lunghezza' => '100', 'descrizione' => 'Largh. in griglia', 'tipo' => 'text'),
118
            ),
119
            'mostrastampa' => array(
120
                array('nomecampo' => 'mostrastampa', 'lunghezza' => '100', 'descrizione' => 'Vedi in stampa', 'tipo' => 'boolean'),
121
            ),
122
            'ordinestampa' => array(
123
                array('nomecampo' => 'ordinestampa', 'lunghezza' => '100', 'descrizione' => 'Ordine in stampa', 'tipo' => 'text'),
124
            ),
125
            'etichettastampa' => array(
126
                array('nomecampo' => 'etichettastampa', 'lunghezza' => '150', 'descrizione' => 'Label in stampa', 'tipo' => 'text'),
127
            ),
128
            'larghezzastampa' => array(
129
                array('nomecampo' => 'larghezzastampa', 'lunghezza' => '100', 'descrizione' => 'Largh. in stampa', 'tipo' => 'text'),
130
            ),
131
        );
132
133
        $paricevuti = array(
134 1
            'doctrine' => $em,
135 1
            'nomebundle' => $nomebundle,
136 1
            'nometabella' => $controller,
137 1
            'dettaglij' => $dettaglij,
138 1
            'container' => $container,
139
        );
140
141 1
        $paricevuti['escludere'] = array('nometabella', 'operatori_id');
142
143 1
        $testata = Griglia::testataPerGriglia($paricevuti);
144
145 1
        $testata['titolo'] = "Configurazione colonne per tabella $nometabella";
146 1
        $testata['multisearch'] = 0;
147 1
        $testata['showdel'] = 0;
148 1
        $testata['showadd'] = 0;
149 1
        $testata['showedit'] = 0;
150 1
        $testata['showprint'] = 0;
151 1
        $testata['editinline'] = 1;
152 1
        $testata['nomelist'] = '#listconfigura';
153 1
        $testata['nomepager'] = '#pagerconfigura';
154 1
        $testata['tastochiudi'] = 1;
155 1
        $testata['div'] = '#dettaglioconf';
156 1
        $testata['chiamante'] = $nometabella;
157 1
        $testata['percorsogriglia'] = $nometabella . '/grigliapopup';
158 1
        $testata['altezzagriglia'] = '300';
159 1
        $testata['larghezzagriglia'] = '900';
160
161 1
        $testata['permessiedit'] = 1;
162 1
        $testata['permessidelete'] = 1;
163 1
        $testata['permessicreate'] = 1;
164 1
        $testata['permessiread'] = 1;
165
        $twigparm = array(
166 1
            'entities' => $entities,
167 1
            'nomecontroller' => $controller,
168 1
            'testata' => json_encode($testata),
169 1
            'chiamante' => $nometabella,
170
        );
171
172 1
        return $this->render($nomebundle . ':' . $controller . ':configura.html.twig', $twigparm);
173
    }
174
175 1
    public function generaDB($parametri, Request $request)
176
    {
177 1
        $this->setup($request);
178 1
        if (!isset($parametri['tabella'])) {
179
            return false;
180
        }
181
182 1
        $namespace = $this->getNamespace();
183 1
        $bundle = $this->getBundle();
184
185 1
        $nomebundle = $namespace . $bundle . 'Bundle';
186
187 1
        $nometabella = $parametri['tabella'];
188 1
        $em = $this->getDoctrine()->getManager();
189
190 1
        $bundles = $this->get('kernel')->getBundles();
191 1
        foreach ($bundles as $bundle) {
192 1
            $className = get_class($bundle);
193 1
            $entityClass = substr($className, 0, strrpos($className, '\\'));
194 1
            $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella;
195 1
            if (!class_exists($tableClassName)) {
196 1
                $tableClassName = '';
197 1
                continue;
198
            } else {
199 1
                break;
200
            }
201
        }
202
203 1
        if (!$tableClassName) {
0 ignored issues
show
Bug introduced by
The variable $tableClassName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
204
            throw new \Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1');
205
        }
206
207 1
        $bundleClass = str_replace('\\', '', $entityClass);
0 ignored issues
show
Bug introduced by
The variable $entityClass does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
208
209 1
        $c = $em->getClassMetadata($bundleClass . ':' . $nometabella);
210
211 1
        $colonne = $c->getColumnNames();
212 1
        $this->scriviDB($colonne, $nometabella, $nomebundle, $parametri);
213 1
    }
214
215 1
    private function scriviDB($colonne, $nometabella, $nomebundle, $parametri)
216
    {
217 1
        foreach ($colonne as $colonna) {
218
            $vettorericerca = array(
219 1
                'nometabella' => $nometabella,
220 1
                'nomecampo' => $colonna,
221
            );
222
223 1
            if (isset($parametri['operatore'])) {
224 1
                $vettorericerca['operatori_id'] = $parametri['operatore'];
225
            }
226
227 1
            $trovato = $this->getDoctrine()->getRepository($nomebundle . ':tabelle')->findBy($vettorericerca, array());
228
229 1
            if (!$trovato) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $trovato of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
230 1
                $crea = new tabelle();
231 1
                $crea->setNometabella($nometabella);
232 1
                $crea->setNomecampo($colonna);
233
234 1
                if (isset($parametri['operatore'])) {
235 1
                    $arraycreaoperatore = array('id' => $parametri['operatore']);
236 1
                    $creaoperatore = $this->getDoctrine()->getRepository($nomebundle . ':operatori')->findOneBy($arraycreaoperatore, array());
0 ignored issues
show
Unused Code introduced by
The call to ObjectRepository::findOneBy() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
237 1
                    $crea->setOperatori($creaoperatore);
0 ignored issues
show
Bug introduced by
It seems like $creaoperatore defined by $this->getDoctrine()->ge...creaoperatore, array()) on line 236 can also be of type object; however, Fi\CoreBundle\Entity\Tabelle::setOperatori() does only seem to accept null|object<Fi\CoreBundle\Entity\Operatori>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
238
239 1
                    unset($vettorericerca['operatori_id']);
240 1
                    $vettorericerca['operatori_id'] = null;
241 1
                    $ritrovato = $this->getDoctrine()->getRepository($nomebundle . ':tabelle')->findOneBy($vettorericerca, array());
0 ignored issues
show
Unused Code introduced by
The call to ObjectRepository::findOneBy() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
242
243 1
                    if ($ritrovato) {
244 1
                        $crea->setMostrastampa($ritrovato->hasMostrastampa() ? true : false);
245 1
                        $crea->setMostraindex($ritrovato->hasMostraindex() ? true : false);
246
                    }
247
                } else {
248 1
                    $crea->setMostrastampa(true);
249 1
                    $crea->setMostraindex(true);
250
                }
251
252 1
                $ma = $this->getDoctrine()->getManager();
253 1
                $ma->persist($crea);
254 1
                $ma->flush();
255
            }
256
        }
257 1
    }
258
259
    public function grigliapopupAction(Request $request, $chiamante)
260
    {
261
        $this->setup($request);
262
        $namespace = $this->getNamespace();
263
        $bundle = $this->getBundle();
264
        $controller = $this->getController();
265
266
        $nomebundle = $namespace . $bundle . 'Bundle';
267
        $em = $this->getDoctrine()->getManager();
268
269
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
270
        $operatore = $gestionepermessi->utentecorrente();
271
272
        $tabellej['operatori_id'] = array('tabella' => 'operatori', 'campi' => array('username', 'operatore'));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tabellej was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tabellej = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
273
274
        $paricevuti = array(
275
            'request' => $request,
276
            'doctrine' => $em,
277
            'container' => $this->container,
278
            'nomebundle' => $nomebundle,
279
            'nometabella' => $controller,
280
            'tabellej' => $tabellej,);
281
282
        $paricevuti['escludere'] = array('nometabella', 'operatori_id');
283
        $paricevuti['precondizioni'] = array('Tabelle.nometabella' => $chiamante, 'Tabelle.operatori_id' => $operatore['id']);
284
285
        return new Response(Griglia::datiPerGriglia($paricevuti));
286
    }
287
288
    public function grigliaAction(Request $request)
289
    {
290
        $this->setParametriGriglia(array('request' => $request));
291
        $paricevuti = self::$parametrigriglia;
292
293
        return new Response(Griglia::datiPerGriglia($paricevuti));
294
    }
295
296 1
    protected function setParametriGriglia($prepar = array())
297
    {
298 1
        $this->setup($prepar['request']);
299 1
        $namespace = $this->getNamespace();
300 1
        $bundle = $this->getBundle();
301 1
        $controller = $this->getController();
302
303 1
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
304 1
        $canRead = ($gestionepermessi->leggere(array('modulo' => $controller)) ? 1 : 0);
305 1
        if (!$canRead) {
306
            throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto");
307
        }
308
309 1
        $nomebundle = $namespace . $bundle . 'Bundle';
310 1
        $tabellej = array();
311 1
        $tabellej['operatori_id'] = array('tabella' => 'operatori', 'campi' => array('username'));
312 1
        $escludi = array("operatori"); //'operatori_id'
313
314
        $paricevuti = array(
315 1
            'container' => $this->container,
316 1
            'nomebundle' => $nomebundle,
317 1
            'nometabella' => $controller,
318 1
            'tabellej' => $tabellej,
319 1
            'escludere' => $escludi
320
        );
321
322 1
        if (! empty($prepar)) {
323 1
            $paricevuti = array_merge($paricevuti, $prepar);
324
        }
325
326 1
        self::$parametrigriglia = $paricevuti;
327 1
    }
328
329
    public function listacampitabellaAction(Request $request)
330
    {
331
        $this->setup($request);
332
        $namespace = $this->getNamespace();
333
        $bundle = $this->getBundle();
334
        $controller = $this->getController();
335
        $nomebundle = $namespace . $bundle . 'Bundle';
336
337
        $nometabella = trim($request->get('tabella'));
338
        if (!isset($nometabella)) {
339
            return false;
340
        }
341
342
        $escludiid = $request->get('escludiid');
343
        if (!isset($escludiid)) {
344
            $escludiid = 0;
345
        }
346
347
        $em = $this->getDoctrine()->getManager();
348
349
        $bundles = $this->get('kernel')->getBundles();
350
        foreach ($bundles as $bundle) {
351
            $className = get_class($bundle);
352
            $entityClass = substr($className, 0, strrpos($className, '\\'));
353
            $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella;
354
            if (!class_exists($tableClassName)) {
355
                $tableClassName = '';
356
                continue;
357
            } else {
358
                break;
359
            }
360
        }
361
362
        if (!$tableClassName) {
0 ignored issues
show
Bug introduced by
The variable $tableClassName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
363
            throw new \Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1');
364
        }
365
366
        $bundleClass = str_replace('\\', '', $entityClass);
0 ignored issues
show
Bug introduced by
The variable $entityClass does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
367
        $c = $em->getClassMetadata($bundleClass . ':' . $nometabella);
368
        $colonne = $c->getColumnNames();
369
370
        $risposta = $this->listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller);
371
        //natcasesort($risposta);
372
        asort($risposta, SORT_NATURAL | SORT_FLAG_CASE);
373
374
        return new JsonResponse($risposta);
375
    }
376
377
    private function listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller)
378
    {
379
        $risposta = array();
380
        if ($escludiid == 1) {
381
            $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
382
            $operatore = $gestionepermessi->utentecorrente();
383
            foreach ($colonne as $colonna) {
384
                $nomecampo = trim(strtolower($colonna));
385
                if (($nomecampo !== 'id') && (strpos($colonna, '_id') == false)) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($colonna, '_id') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
386
                    $qb = $this->getDoctrine()->getRepository("$nomebundle:$controller")
387
                            ->createQueryBuilder('t')
388
                            ->where('LOWER(t.nometabella) = :nometabella')
389
                            ->andWhere('LOWER(t.nomecampo) = :nomecampo')
390
                            ->andWhere('t.operatori_id = :operatori_id')
391
                            ->setParameter('nometabella', $nometabella)
0 ignored issues
show
Bug introduced by
The variable $nometabella does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
392
                            ->setParameter('nomecampo', $nomecampo)
393
                            ->setParameter('operatori_id', $operatore['id'])
394
                            ->getQuery();
395
                    $labeltrovata = $qb->getResult();
396
                    if (!$labeltrovata) {
397
                        $qb = $this->getDoctrine()->getRepository("$nomebundle:$controller")
398
                                ->createQueryBuilder('t')
399
                                ->where('LOWER(t.nometabella) = :nometabella')
400
                                ->andWhere('LOWER(t.nomecampo) = :nomecampo')
401
                                ->andWhere('t.operatori_id IS NULL')
402
                                ->setParameter('nometabella', $nometabella)
403
                                ->setParameter('nomecampo', $nomecampo)
404
                                ->getQuery();
405
                        $labeltrovata = $qb->getResult();
406
                        if (!$labeltrovata) {
407
                            $risposta[$colonna] = $colonna;
408
                        } else {
409
                            if (($labeltrovata[0]->getEtichettaindex()) && ($labeltrovata[0]->getEtichettaindex() != '')) {
410
                                $risposta[$colonna] = trim($labeltrovata[0]->getEtichettaindex());
411
                            } else {
412
                                $risposta[$colonna] = $colonna;
413
                            }
414
                        }
415
                    } else {
416
                        if (($labeltrovata[0]->getEtichettaindex()) && ($labeltrovata[0]->getEtichettaindex() != '')) {
417
                            $risposta[$colonna] = trim($labeltrovata[0]->getEtichettaindex());
418
                        } else {
419
                            $risposta[$colonna] = $colonna;
420
                        }
421
                    }
422
                }
423
            }
424
        } else {
425
            foreach ($colonne as $colonna) {
426
                $risposta[$colonna] = $colonna;
427
            }
428
        }
429
430
        return $risposta;
431
    }
432
}
433