Passed
Push — master ( 1e8655...fbfe4b )
by Andrea
17:02
created

elaboraColonneOpzioniTabellaMancanti()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 64
nop 5
dl 0
loc 17
ccs 12
cts 12
cp 1
crap 7
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Tabella;
4
5
use Cdf\BiCoreBundle\Utils\Entity\EntityUtils;
6
use Cdf\BiCoreBundle\Utils\Arrays\ArrayUtils;
7
8
trait TabellaOpzioniTrait
9
{
10
    use TabellaOpzioniFromCoreTrait, TabellaOpzioniFromModelloColonneTrait;
11
12 12
    protected function getAllOpzioniTabella()
13
    {
14 12
        $opzionibuilder = array();
15 12
        foreach ($this->colonnedatabase as $colonnadatabase) {
16
            // Inserire dati da definizione entity
17 12
            $this->setOpzioniTabellaDefault($colonnadatabase, $opzionibuilder, null, false);
18
        }
19 12
        $this->setOpzioniTabellaFromModellocolonne($opzionibuilder);
20 12
        $this->setOpzioniTabellaFromCore($colonnadatabase, $opzionibuilder);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $colonnadatabase seems to be defined by a foreach iteration on line 15. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
21 12
        $this->setOrdinaColonneTabella($opzionibuilder);
22 12
        $this->setLarghezzaColonneTabella($opzionibuilder);
23
24 12
        return $opzionibuilder;
25
    }
26
27 4
    private function getOpzionitabellaCampiExtra($campo, $modellocolonna, &$opzionibuilder)
28
    {
29 4
        if ((isset($modellocolonna['campoextra']) && true == $modellocolonna['campoextra'])) {
30 1
            $opzionibuilder[$campo] = array(
31 1
                'tipocampo' => $modellocolonna['tipocampo'],
32 1
                'nomecampo' => $campo,
33 1
                'nometabella' => $modellocolonna['nometabella'],
34
                'entityclass' => null,
35
                'sourceentityclass' => null,
36
                'ordine' => null,
37 1
                'etichetta' => $campo,
38 1
                'larghezza' => 5,
39
                'editabile' => false,
40
                'campoextra' => true,
41
                'association' => null,
42
                'associationtable' => null,
43
                'escluso' => false,
44
            );
45
        }
46 4
    }
47
48 12
    protected function setOpzioniTabellaDefault($infoentity, &$opzionibuilder, $jointable = null, $ricursione = false, $ancestors = array())
49
    {
50 12
        $nometabella = ((isset($jointable)) ? $jointable : $this->tablename);
51 12
        if (!in_array($nometabella, $ancestors)) {
52 12
            $ancestors[] = $nometabella;
53
        }
54 12
        $nomecolonna = ucfirst(implode('.', $ancestors)).'.'.$infoentity['fieldName'];
55
56 12
        $this->elaboraColonneOpzioniTabellaMancanti($opzionibuilder, $infoentity, $nometabella, $nomecolonna, $ricursione);
57
58 12
        if (isset($infoentity['association'])) {
59 6
            $this->elaboraJoin($opzionibuilder, $infoentity, $ancestors);
60
        }
61 12
    }
62
63 12
    private function elaboraColonneOpzioniTabellaMancanti(&$opzionibuilder, $colonnadatabase, $nometabella, $nomecolonna, $ricursione)
64
    {
65 12
        $opzionibuilder[$nomecolonna] = array(
66 12
            'tipocampo' => isset($colonnadatabase['association']) ? 'join' : $colonnadatabase['type'],
67 12
            'nomecampo' => $nomecolonna,
68 12
            'nometabella' => $nometabella,
69 12
            'entityclass' => $colonnadatabase['entityClass'],
70 12
            'sourceentityclass' => isset($colonnadatabase['sourceEntityClass']) ? $colonnadatabase['sourceEntityClass'] : null,
71
            'ordine' => null,
72 12
            'etichetta' => ucfirst($colonnadatabase['columnName']),
73 12
            'larghezza' => 10,
74
            'editabile' => true,
75
            'campoextra' => false,
76 12
            'association' => isset($colonnadatabase['association']) ? $colonnadatabase['association'] : false,
77 12
            'associationtable' => isset($colonnadatabase['associationtable']) ? $colonnadatabase['associationtable'] : null,
78
            'decodifiche' => null,
79 12
            'escluso' => (true === $ricursione) ? true : '_id' == substr($colonnadatabase['fieldName'], -3) ? true : false,
80
        );
81 12
    }
82
83 12
    private function getLarghezzaColonneTabellaTotalePercentuale($opzionibuilder)
84
    {
85 12
        $larghezzatotalepercentuale = 0;
86 12
        foreach ($opzionibuilder as $opzione) {
87 12
            if (false === $opzione['escluso']) {
88 12
                $larghezzatotalepercentuale += $opzione['larghezza'];
89
            }
90
        }
91
92 12
        return $larghezzatotalepercentuale;
93
    }
94
95 12
    private function getLarghezzaColonneTabellaPercentualeFinale()
96
    {
97
        // il 5% si lascia per la ruzzolina in fondo alla riga, il 3% si lascia per il checkbox in testa alla riga,
98
        // quindi per le colonne dati resta il 92%
99 12
        $percentualefinale = 95; // il 5% si lascia per la ruzzolina in fondo
100 12
        if (true === $this->getTabellaParameter('multiselezione')) {
0 ignored issues
show
Bug introduced by
It seems like getTabellaParameter() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

100
        if (true === $this->/** @scrutinizer ignore-call */ getTabellaParameter('multiselezione')) {
Loading history...
101
            $percentualefinale -= 3;
102
        }
103
104 12
        return $percentualefinale;
105
    }
106
107 12
    private function setLarghezzaColonneTabella(&$opzionibuilder)
108
    {
109 12
        $larghezzatotalepercentuale = $this->getLarghezzaColonneTabellaTotalePercentuale($opzionibuilder);
110 12
        $percentualefinale = $this->getLarghezzaColonneTabellaPercentualeFinale();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
111 12
        $percentualerelativatotale = $percentualefinale * 100 / $larghezzatotalepercentuale;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
112 12
        if (0 != $percentualefinale - $larghezzatotalepercentuale) {
113 12
            foreach ($opzionibuilder as $key => $opzione) {
114 12
                if (false === $opzione['escluso']) {
115 12
                    $larghezzapercentualericalcolata = ceil($opzione['larghezza'] * $percentualerelativatotale / 100);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
116 12
                    $opzionibuilder[$key]['larghezza'] = ($larghezzapercentualericalcolata < 5 ? 5 : $larghezzapercentualericalcolata);
117
                }
118
            }
119
        }
120 12
    }
121
122 6
    private function elaboraJoin(&$opzionibuilder, $colonnadatabase, $ancestors)
123
    {
124 6
        $entitycollegata = $colonnadatabase['associationtable']['targetEntity'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
125 6
        $utils = new EntityUtils($this->em, $entitycollegata);
0 ignored issues
show
Unused Code introduced by
The call to Cdf\BiCoreBundle\Utils\E...ityUtils::__construct() has too many arguments starting with $entitycollegata. ( Ignorable by Annotation )

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

125
        $utils = /** @scrutinizer ignore-call */ new EntityUtils($this->em, $entitycollegata);

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. Please note the @ignore annotation hint above.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
126 6
        $tablecollegataname = $this->em->getClassMetadata($entitycollegata)->getTableName();
127 6
        $colonnecollegate = $utils->getEntityColumns($entitycollegata);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
128 6
        foreach ($colonnecollegate as $colonnacorrente) {
129 6
            if (!isset($colonnacorrente['type'])) {
130 5
                $this->setOpzioniTabellaDefault($colonnacorrente, $opzionibuilder, $tablecollegataname, true, $ancestors);
131 5
                continue;
132
            }
133
134 6
            if (!in_array($tablecollegataname, $ancestors)) {
135 6
                $ancestors[] = $tablecollegataname;
136
            }
137 6
            $nomecampo = ucfirst(implode('.', $ancestors)).'.'.$colonnacorrente['fieldName'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 18 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
138 6
            $opzionibuilder[$nomecampo] = array(
139 6
                'tipocampo' => $colonnacorrente['type'],
140 6
                'nomecampo' => $nomecampo,
141 6
                'nometabella' => $tablecollegataname,
142 6
                'entityclass' => $colonnadatabase['entityClass'],
143 6
                'sourceentityclass' => isset($colonnadatabase['sourceEntityClass']) ? $colonnadatabase['sourceEntityClass'] : null,
144
                'ordine' => null,
145 6
                'etichetta' => ucfirst($colonnacorrente['columnName']),
146 6
                'larghezza' => 0,
147
                'editabile' => false,
148
                'campoextra' => false,
149
                'association' => null,
150
                'associationtable' => null,
151
                'escluso' => true,
152
            );
153
        }
154 6
    }
155
156 12
    protected function setOrdinaColonneTabella(&$opzionibuilder)
157
    {
158 12
        foreach ($opzionibuilder as $key => $opzione) {
159 12
            if (null === $opzione['ordine']) {
160 12
                $newordine = $this->getMaxOrdine() + 10;
0 ignored issues
show
Bug introduced by
It seems like getMaxOrdine() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

160
                $newordine = $this->/** @scrutinizer ignore-call */ getMaxOrdine() + 10;
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 22 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
161 12
                $opzionibuilder[$key]['ordine'] = $newordine;
162 12
                $this->setMaxOrdine($newordine);
0 ignored issues
show
Bug introduced by
It seems like setMaxOrdine() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

162
                $this->/** @scrutinizer ignore-call */ 
163
                       setMaxOrdine($newordine);
Loading history...
163
            }
164
        }
165
        // Ordinamento per colonna ordine
166 12
        ArrayUtils::sortMultiAssociativeArray($opzionibuilder, 'ordine', true);
167 12
    }
168
169 5
    private function bonificaNomeCampo($nomecampo)
170
    {
171 5
        $parti = explode('.', $nomecampo);
172 5
        $campo = '';
173 5
        for ($index = 0; $index < count($parti); ++$index) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
174 5
            if ($index == count($parti) - 1) {
175 5
                $campo .= '.'.lcfirst($parti[$index]);
176
            } else {
177 5
                $campo .= '.'.ucfirst($parti[$index]);
178
            }
179
        }
180
181 5
        return substr($campo, 1);
182
    }
183
}
184