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 classes like EnvioDte 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 EnvioDte, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class EnvioDte extends \sasco\LibreDTE\Sii\Base\Envio |
||
32 | { |
||
33 | |||
34 | private $dtes = []; ///< Objetos con los DTE que se enviarán |
||
35 | private $config = [ // 0: DTE, 1: boleta |
||
|
|||
36 | 'SubTotDTE_max' => [20, 2], ///< máxima cantidad de tipos de documentos en el envío |
||
37 | 'DTE_max' => [2000, 1000], ///< máxima cantidad de DTE en un envío |
||
38 | 'tipos' => ['EnvioDTE', 'EnvioBOLETA'], ///< Tag para el envío, según si son Boletas o no |
||
39 | 'schemas' => ['EnvioDTE_v10', 'EnvioBOLETA_v11'], ///< Schema (XSD) que se deberá usar para validar según si son boletas o no |
||
40 | ]; ///< Configuración/reglas para el documento XML |
||
41 | private $tipo = null; ///< =0 DTE, =1 boleta |
||
42 | |||
43 | /** |
||
44 | * Método que agrega un DTE al listado que se enviará |
||
45 | * @param DTE Objeto del DTE |
||
46 | * @return =true si se pudo agregar el DTE o =false si no se agregó por exceder el límite de un envío |
||
47 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
48 | * @version 2015-12-11 |
||
49 | */ |
||
50 | public function agregar(Dte $DTE) |
||
71 | |||
72 | /** |
||
73 | * Método para asignar la caratula |
||
74 | * @param caratula Arreglo con datos del envío: RutEnvia, FchResol y NroResol |
||
75 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
76 | * @version 2015-12-11 |
||
77 | */ |
||
78 | public function setCaratula(array $caratula) |
||
112 | |||
113 | /** |
||
114 | * Método que realiza el envío del sobre con el o los DTEs al SII |
||
115 | * @return Track ID del envío o =false si hubo algún problema al enviar el documento |
||
116 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
117 | * @version 2015-12-15 |
||
118 | */ |
||
119 | public function enviar() |
||
128 | |||
129 | /** |
||
130 | * Método que genera el XML para el envío del DTE al SII |
||
131 | * @return XML con el envio del DTE firmado o =false si no se pudo generar o firmar el envío |
||
132 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
133 | * @version 2016-08-06 |
||
134 | */ |
||
135 | public function generar() |
||
176 | |||
177 | /** |
||
178 | * Método que obtiene los datos para generar los tags SubTotDTE |
||
179 | * @return Arreglo con los datos para generar los tags SubTotDTE |
||
180 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
181 | * @version 2015-09-02 |
||
182 | */ |
||
183 | private function getSubTotDTE() |
||
200 | |||
201 | /** |
||
202 | * Método que carga un XML de EnvioDte y asigna el objeto XML correspondiente |
||
203 | * para poder obtener los datos del envío |
||
204 | * @return Objeto XML |
||
205 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
206 | * @version 2016-11-21 |
||
207 | */ |
||
208 | public function loadXML($xml_data) |
||
241 | |||
242 | /** |
||
243 | * Método que entrega un arreglo con los datos de la carátula del envío |
||
244 | * @return Arreglo con datos de carátula |
||
245 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
246 | * @version 2015-12-11 |
||
247 | */ |
||
248 | public function getCaratula() |
||
252 | |||
253 | /** |
||
254 | * Método que entrega el ID de SetDTE |
||
255 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
256 | * @version 2015-12-11 |
||
257 | */ |
||
258 | public function getID() |
||
262 | |||
263 | /** |
||
264 | * Método que entrega el DigestValue de la firma del envío |
||
265 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
266 | * @version 2015-12-11 |
||
267 | */ |
||
268 | public function getDigest() |
||
272 | |||
273 | /** |
||
274 | * Método que entrega el rut del emisor del envío |
||
275 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
276 | * @version 2015-09-07 |
||
277 | */ |
||
278 | public function getEmisor() |
||
283 | |||
284 | /** |
||
285 | * Método que entrega el rut del receptor del envío |
||
286 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
287 | * @version 2015-09-07 |
||
288 | */ |
||
289 | public function getReceptor() |
||
294 | |||
295 | /** |
||
296 | * Método que entrega la fecha del DTE más antiguo del envio |
||
297 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
298 | * @version 2015-12-12 |
||
299 | */ |
||
300 | public function getFechaEmisionInicial() |
||
309 | |||
310 | /** |
||
311 | * Método que entrega la fecha del DTE más nuevo del envio |
||
312 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
313 | * @version 2015-12-12 |
||
314 | */ |
||
315 | public function getFechaEmisionFinal() |
||
324 | |||
325 | /** |
||
326 | * Método que entrega el arreglo con los objetos DTE del envío |
||
327 | * @return Arreglo de objetos DTE |
||
328 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
329 | * @version 2016-11-21 |
||
330 | */ |
||
331 | public function getDocumentos($c14n = true) |
||
352 | |||
353 | /** |
||
354 | * Método que entrega el objeto DTE solicitado del envío |
||
355 | * @return Objeto DTE |
||
356 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
357 | * @version 2016-11-21 |
||
358 | */ |
||
359 | public function getDocumento($emisor, $dte, $folio) |
||
384 | |||
385 | /** |
||
386 | * Método que indica si es EnvioDTE o EnvioBOLETA |
||
387 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
388 | * @version 2016-08-10 |
||
389 | */ |
||
390 | public function esBoleta() |
||
394 | |||
395 | /** |
||
396 | * Método que determina el estado de validación sobre el envío |
||
397 | * @param datos Arreglo con datos para hacer las validaciones |
||
398 | * @return Código del estado de la validación |
||
399 | * @warning No se está validando la firma del EnvioDTE |
||
400 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
401 | * @version 2015-09-07 |
||
402 | */ |
||
403 | public function getEstadoValidacion(array $datos = null) |
||
413 | |||
414 | /** |
||
415 | * Método que indica si la firma del documento es o no válida |
||
416 | * @return =true si la firma del documento del envío es válida, =null si no se pudo determinar |
||
417 | * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl) |
||
418 | * @version 2017-04-07 |
||
419 | */ |
||
420 | public function checkFirma() |
||
443 | |||
444 | } |
||
445 |
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.