Completed
Push — master ( 448cba...1b860c )
by Lorenzo
02:13
created

ValidateHelper   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 202
Duplicated Lines 5.94 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 49
c 2
b 0
f 0
lcom 1
cbo 8
dl 12
loc 202
ccs 0
cts 121
cp 0
rs 8.5455

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B checkCodice() 0 14 6
A checkCodiceRegione() 0 4 1
A checkCodiceSSA() 0 4 1
A IsoDateValidate() 0 8 2
C checkArrVociSpesa() 3 24 9
D checkArrSpesa() 9 45 19
A checkPIva() 0 11 3
A checkCfProprietario() 0 11 3
A checkCodiceAsl() 0 4 1
A checkDataValida() 0 10 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ValidateHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ValidateHelper, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Padosoft\TesseraSanitaria;
3
4
/**
5
 * Class ValidateHelper
6
 * @package Padosoft\TesseraSanitaria
7
 */
8
class ValidateHelper
9
{
10
	use traits\Errorable;
11
12
    private $objPartitaIVA = null;
13
    private $objCFChecker = null;
14
15
	/**+
16
	 * ValidateHelper constructor.
17
	 *
18
	 * @param \fdisotto\PartitaIVA   $objPartitaIVA
19
	 * @param \CodiceFiscale\Checker $objCFChecker
20
	 */
21
	public function __construct(\fdisotto\PartitaIVA $objPartitaIVA, \CodiceFiscale\Checker $objCFChecker)
22
	{
23
		$this->arrErrors = array();
24
		$this->objPartitaIVA = $objPartitaIVA;
25
		$this->objCFChecker = $objCFChecker;
26
	}
27
28
    /**
29
     * @param       $codice
30
     * @param       $codice_nome
31
     * @param array $arrCodiciValidi
32
     * @param       $codice_len
33
     */
34
    public function checkCodice($codice, $codice_nome, array $arrCodiciValidi, $codice_len)
35
    {
36
        if($codice==''){
37
            $this->AddError("$codice_nome mancante");
38
        }else{
39
            if( is_int($codice_len) && ($codice_len>0) && strlen($codice) != $codice_len){
40
                $this->AddError("<b>".$codice."</b> - Il $codice_nome deve essere lungo $codice_len caratteri");
41
            }
42
43
            if(!in_array($codice, $arrCodiciValidi)){
44
                $this->AddError("<b>".$codice."</b> - $codice_nome non valido. Codici validi: ".implode(", ",$arrCodiciValidi));
45
            }
46
        }
47
    }
48
49
	/**
50
	 * @param $codiceRegione
51
	 */
52
	public function checkCodiceRegione($codiceRegione)
53
	{
54
        $this->checkCodice($codiceRegione, 'Codice regione (codiceRegione)', CodiceRegione::getCostants(), 3);
55
	}
56
57
	/**
58
	 * @param $codiceSSA
59
	 */
60
	public function checkCodiceSSA($codiceSSA)
61
	{
62
        $this->checkCodice($codiceSSA, 'Codice SSA (codiceSSA)', CodiceSSA::getCostants(), 0);
63
	}
64
65
	/**
66
	 * @param $dateStr
67
	 *
68
	 * @return bool
69
	 */
70
	private function IsoDateValidate($dateStr)
71
	{
72
		if (preg_match('/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/', $dateStr) > 0){
73
			return TRUE;
74
		}else{
75
			return FALSE;
76
		}
77
	}
78
79
	/**+
80
	 * @param $arrVociSpesa
81
	 */
82
	public function checkArrVociSpesa($arrVociSpesa)
83
	{
84
	if(empty($arrVociSpesa)){
85
86
			$this->AddError("Voci spesa mancanti");
87
		}else{
88
89
			$arrTipiSpesaPermessi = TipoSpesa::getCostants();
90
91
			foreach ($arrVociSpesa as $rigaVociSpesa){
92
				foreach ($rigaVociSpesa as $colonnaVociSpesa){
93
					foreach($colonnaVociSpesa as $campo=>$valore){
94
95 View Code Duplication
						if($campo == "tipoSpesa" && !in_array($valore,$arrTipiSpesaPermessi)){
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
								$this->AddError("<b>".$valore."</b> - Codice tipo spesa (tipoSpesa) non valido. Codici validi: ".implode(", ",$arrTipiSpesaPermessi));
97
						}
98
						if($campo == "importo" && !is_numeric($valore)){
99
								$this->AddError("<b>".$valore."</b> - Importo (importo) non numerico");
100
						}
101
					}
102
				}
103
			}
104
		}
105
	}
106
107
	/**
108
	 * @param $arrSpesa
109
	 */
110
	public function checkArrSpesa($arrSpesa)
111
	{
112
		if(empty($arrSpesa)){
113
			$this->AddError("Dati spesa mancanti");
114
		}else{
115
116
			$arrFlagOperazione = FlagOperazione::getCostants();
117
118
			// Controllo interno array spesa
119
			foreach($arrSpesa as $rigaSpesa){
120
121
				if(count($rigaSpesa)<6){
122
					$this->AddError("Dati spesa incompleti");
123
				}
124
125
				foreach($rigaSpesa as $campo => $valore){
126
127
					// generico per campo mancante obbligatorio
128
					if($valore == "" && $campo != "flagPagamentoAnticipato"){ // flagPagamentoAnticipato e' facoltativo
129
						$this->AddError("Dato spesa mancante campo: ".$campo);
130
					}
131
132
                    if ($campo == "dataEmissione" || $campo == "dataPagamento") {
133
                        $this->checkDataValida($campo, $valore);
134
                    }
135
136 View Code Duplication
					if($campo == "flagOperazione" && (!in_array($valore, $arrFlagOperazione)) ){
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
						$this->AddError("<b>".$valore."</b> - Flag Operazione (flagOperazione) non valido. Codici validi: ".implode(", ",$arrFlagOperazione));
138
					}
139
140
					if($campo == "cfCittadino" && !$this->objCFChecker->isFormallyCorrect($valore)){
141
						$this->AddError("<b>".$valore."</b> - Codice fiscale (cfCittadino) cittadino non valido");
142
					}
143
144 View Code Duplication
					if($campo == "dispositivo" && (!is_numeric($valore) || strlen($valore) > 3) ){
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
						$this->AddError("<b>".$valore."</b> - Codice dispositivo (dispositivo) non valido: deve essere numerico, al massimo di 3 cifre");
146
					}
147
148 View Code Duplication
					if($campo == "numDocumento" && (!is_numeric($valore) || strlen($valore) > 20) ){
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
						$this->AddError("<b>".$valore."</b> - Numero documento (numDocumento) non valido: deve essere numerico, al massimo di 20 cifre");
150
					}
151
				}
152
			}
153
		}
154
	}
155
156
	/**
157
	 * @param $pIva
158
	 */
159
	public function checkPIva($pIva)
160
	{
161
		if(empty($pIva)){
162
			$this->AddError("Partita IVA mancante");
163
		}else{
164
			// Verifica formale della partita IVA
165
			if(!$this->objPartitaIVA->check($pIva)){
166
				$this->AddError("<b>".$pIva."</b> - Partita IVA formalmente non corretta");
167
			}
168
		}
169
	}
170
171
	/**
172
	 * @param $cfProprietario
173
	 */
174
	public function checkCfProprietario($cfProprietario)
175
	{
176
		if(empty($cfProprietario)){
177
			$this->AddError("Codice fiscale proprietario (cfProprietario) mancante");
178
		}else{
179
			// Verifica formale del codice fiscale
180
			if (!$this->objCFChecker->isFormallyCorrect($cfProprietario)){
181
				$this->AddError("<b>".$cfProprietario."</b> - Codice fiscale proprietario (cfProprietario) formalmente non corretto");
182
			}
183
		}
184
	}
185
186
	/**
187
	 * @param $codiceAsl
188
	 */
189
	public function checkCodiceAsl($codiceAsl)
190
	{
191
        $this->checkCodice($codiceAsl, 'Codice ASL (codiceAsl)', CodiceAsl::getCostants(), 3);
192
	}
193
194
    /**
195
     * @param $campo
196
     * @param $valore
197
     */
198
    private function checkDataValida($campo, $valore)
199
    {
200
        if (!$this->IsoDateValidate($valore)) {
201
            $this->AddError("<b>" . $valore . "</b> - $campo non valida. La data deve essere nel formato ISO Es.: 2015-08-01");
202
        }
203
204
        if ($valore < "2015-01-01") {
205
            $this->AddError("<b>" . $valore . "</b> - $campo deve essere successiva al 01/01/2015");
206
        }
207
    }
208
209
}
210