Completed
Push — master ( d9bece...2ea354 )
by Andrea
10:06
created

ArrayFunctions::multiInMultiarray()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 54
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.0038

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 54
ccs 20
cts 21
cp 0.9524
rs 8.7449
c 1
b 0
f 0
cc 6
eloc 20
nc 6
nop 4
crap 6.0038

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 ArrayFunctions
6
{
7
8
    /**
9
     * La funzione cerca un valore $elem nell'array multidimensionale $array all'interno di ogni elemento con chiave $key di ogni riga di array
10
     * e restituisce l'indice.
11
     *
12
     * @param $elem Oggetto da cercare
13
     * @param $array Array nel quale cercare
14
     * @param $key Nome della chiave nella quale cercare $elem
15
     *
16
     * @return mixed False se non trovato l'elemento, altrimenti l'indice in cui si è trovato il valore
17
     */
18 1
    public static function inMultiarray($elem, $array, $key)
19
    {
20 1
        foreach ($array as $indice => $value) {
21 1
            if (!is_array($value)) {
22
                return false;
23
            }
24 1
            if (array_key_exists($key, $value)) {
25 1
                foreach ($value as $nomecolonna => $colonna) {
26 1
                    if ($colonna === $elem && $nomecolonna == $key) {
27 1
                        return $indice;
28
                    }
29 1
                }
30 1
            } else {
31
                return false;
32
            }
33 1
        }
34 1
        return false;
35
    }
36
37
    /**
38
     * La funzione cerca un valore $elem nell'array multidimensionale $array all'interno di ogni elemento con chiave $key di ogni riga di array
39
     * e restituisce l'indice
40
     *
41
     * @param $elem Oggetto da cercare
42
     * @param $array Array nel quale cercare
43
     * @param $key Nome della chiave nella quale cercare $elem
44
     * @return Mixed False se non trovato l'elemento, altrimenti il vettore con tutti gli indici
45
     */
46 2
    public static function inMultiarrayTutti($elem, $array, $key, $debug)
0 ignored issues
show
Unused Code introduced by
The parameter $debug is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
49
        /*if ($debug) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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
            var_dump($elem);
51
52
            var_dump($key);
53
        }*/
54
55
56 2
        $trovato = array();
57
58 2
        foreach ($array as $indice => $value) {
59 2
            if (!is_array($value)) {
60
                return false;
61
            }
62 2
            if (array_key_exists($key, $value)) {
63 2
                foreach ($value as $nomecolonna => $colonna) {
64 2
                    if ($colonna === $elem && $nomecolonna == $key) {
65
                        /*if ($debug) {
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...
66
                            echo "$colonna = $elem quando indice = $indice \n";
67
                            echo "$nomecolonna = $key quando indice = $indice \n";
68
                            var_dump($array[$indice]);
69
                            echo "\n";
70
                        }*/
71
72 2
                        $trovato[] = $indice;
73 2
                    }
74 2
                }
75 2
            } else {
76
                return false;
77
            }
78 2
        }
79 2
        return (count($trovato) > 0 ? $trovato : false);
80
    }
81
82
    /**
83
     * La funzione cerca un valore $elem nell'array multidimensionale $array all'interno di ogni elemento con chiave $key di ogni riga di array
84
     * e restituisce l'indice
85
     *
86
     * @param $array Array nel quale cercare
87
     * @param $search Chiave-valore da cercare
88
     * @return Mixed False se non trovato l'elemento, altrimenti l'indice in cui si è trovato il valore
89
     */
90 1
    public static function multiInMultiarray($array, $search, $debug = false, $tutti = false)
91
    {
92 1
        $primo = true;
93 1
        $vettorerisultati = array();
94
95
        /*if ($debug) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
96
            echo "<br>\n vettore search <br>\n";
97
            var_dump($search);
98
            echo "<br>\n fine vettore search  <br>\n";
99
100
            echo "<br>\n vettorecompleto <br>\n";
101
            var_dump($array);
102
            echo "<br>\n fine vettorecompleto <br>\n";
103
        }*/
104
105
106 1
        foreach ($search as $key => $singolaricerca) {
107 1
            $trovato = self::inMultiarrayTutti($singolaricerca, $array, $key, $debug);
108
109
            /*if ($debug) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
110
                echo $primo ? "<br>\n primo <br>\n" : "<br>\n non primo <br>\n";
111
                var_dump($trovato);
112
                echo $primo ? "<br>\n fine primo <br>\n" : "<br>\n fine non primo <br>\n";
113
            }*/
114
115 1
            if ($trovato === false) {
116 1
                $vettorerisultati = false;
117 1
                break;
118
            }
119
120 1
            if ($primo) {
121 1
                $vettorerisultati = $trovato;
122 1
            } else {
123
                $vettorerisultati = array_intersect($vettorerisultati, $trovato);
124
                /*if ($debug) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
125
                    echo "<br>\n vettorerisultati<br>\n";
126
                    var_dump($vettorerisultati);
127
                    echo "<br>\n fine vettorerisultati<br>\n";
128
                }*/
129
            }
130
131 1
            $primo = false;
132 1
        }
133
134 1
        if ($vettorerisultati === false) {
135 1
            $risposta = false;
136 1
        } elseif ($tutti === false) {
137 1
            $risposta = reset($vettorerisultati);
138 1
        } else {
139 1
            $risposta = $vettorerisultati;
140
        }
141
142 1
        return $risposta;
143
    }
144
145
    /**
146
     * La funzione ordina un array multidimensionale $array.
147
     *
148
     * @param $array Array da ordinare
149
     * @param $key Nome della chiave dell'array per cui ordinare
150
     * @param $type Tipo di ordinamento SORT_ASC, SORT_DESC
151
     *
152
     * @return array Ritorna l'array ordinato
153
     *
154
     * @example arrayOrderby($rubrica,"cognome",SORT_ASC);<br/>$rubrica = array();<br/>$rubrica[] = array("matricola" => 99999, "cognome" => "rossi", "nome" => "mario");<br/>$rubrica[] = array("matricola" => 99998, "cognome" => "bianchi", "nome" => "andrea");<br/>$rubrica[] = array("matricola" => 99997, "cognome" => "verdi", "nome" => "michele");<br/>rusulterà<br/>$rubrica[0]("matricola"=>99998,"cognome"=>"bianchi","nome"=>"andrea")<br/>$rubrica[1]("matricola"=>99999,"cognome"=>"rossi","nome"=>"mario")<br/>$rubrica[2]("matricola"=>99997,"cognome"=>"verdi","nome"=>"michele")<br/>
155
     */
156 1
    public static function arrayOrderby()
157
    {
158 1
        $args = func_get_args();
159 1
        $data = array_shift($args);
160 1
        foreach ($args as $n => $field) {
161 1
            if (is_string($field)) {
162 1
                $tmp = array();
163 1
                foreach ($data as $key => $row) {
164 1
                    $tmp[$key] = $row[$field];
165 1
                }
166 1
                $args[$n] = $tmp;
167 1
            }
168 1
        }
169 1
        $args[] = &$data;
170 1
        call_user_func_array('array_multisort', $args);
171 1
        return array_pop($args);
172
    }
173
174
    public function arraySearchRecursive($needle, $haystack)
175
    {
176
        foreach ($haystack as $key => $val) {
177
            if (stripos(implode('', $val), $needle) > 0) {
178
                return $key;
179
            }
180
        }
181
182
        return false;
183
    }
184
}
185