Complex classes like Daevento 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 Daevento, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Daevento extends Common |
||
| 24 | { |
||
| 25 | public $chCTe; |
||
| 26 | |||
| 27 | protected $logoAlign = 'C'; |
||
| 28 | protected $yDados = 0; |
||
| 29 | protected $debugMode = 0; |
||
| 30 | protected $aEnd = array(); |
||
| 31 | protected $pdf; |
||
| 32 | protected $xml; |
||
| 33 | protected $logomarca = ''; |
||
| 34 | protected $errMsg = ''; |
||
| 35 | protected $errStatus = false; |
||
| 36 | protected $orientacao = 'P'; |
||
| 37 | protected $papel = 'A4'; |
||
| 38 | protected $destino = 'I'; |
||
| 39 | protected $pdfDir = ''; |
||
| 40 | protected $fontePadrao = 'Times'; |
||
| 41 | protected $version = '0.1.1'; |
||
| 42 | protected $wPrint; |
||
| 43 | protected $hPrint; |
||
| 44 | protected $wCanhoto; |
||
| 45 | protected $formatoChave = "#### #### #### #### #### #### #### #### #### #### ####"; |
||
| 46 | protected $id; |
||
| 47 | protected $tpAmb; |
||
| 48 | protected $cOrgao; |
||
| 49 | protected $xCorrecao; |
||
| 50 | protected $xCondUso; |
||
| 51 | protected $dhEvento; |
||
| 52 | protected $cStat; |
||
| 53 | protected $xMotivo; |
||
| 54 | protected $xJust; |
||
| 55 | protected $CNPJDest = ''; |
||
| 56 | protected $CPFDest = ''; |
||
| 57 | protected $dhRegEvento; |
||
| 58 | protected $nProt; |
||
| 59 | protected $tpEvento; |
||
| 60 | |||
| 61 | private $dom; |
||
| 62 | private $procEventoCTe; |
||
| 63 | private $evento; |
||
| 64 | private $infEvento; |
||
| 65 | private $retEvento; |
||
| 66 | private $rinfEvento; |
||
| 67 | |||
| 68 | /** |
||
| 69 | *__construct |
||
| 70 | * @param string $docXML Arquivo XML da cce |
||
| 71 | * @param string $sOrientacao (Opcional) Orientação da impressão P-retrato L-Paisagem |
||
| 72 | * @param string $sPapel Tamanho do papel (Ex. A4) |
||
| 73 | * @param string $sPathLogo Caminho para o arquivo do logo |
||
| 74 | * @param string $sDestino Estabelece a direção do envio do documento PDF I-browser D-browser com download S- |
||
| 75 | * @param string $sDirPDF Caminho para o diretorio de armazenamento dos arquivos PDF |
||
| 76 | * @param string $fonteDACTE Nome da fonte alternativa do DACTE |
||
|
|
|||
| 77 | * @param array $aEnd array com o endereço do emitente |
||
| 78 | * @param integer $mododebug 1-SIM e 0-Não (0 default) |
||
| 79 | */ |
||
| 80 | public function __construct( |
||
| 162 | |||
| 163 | /** |
||
| 164 | * monta |
||
| 165 | * @param string $orientacao |
||
| 166 | * @param string $papel |
||
| 167 | * @param string $logoAlign |
||
| 168 | * @param string $situacao_externa |
||
| 169 | * @param string $classPDF |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function monta( |
||
| 185 | |||
| 186 | /** |
||
| 187 | * printDocument |
||
| 188 | * @param string $nome |
||
| 189 | * @param string $destino |
||
| 190 | * @param string $printer |
||
| 191 | * @return string pdf |
||
| 192 | */ |
||
| 193 | public function printDocument($nome = '', $destino = 'I', $printer = '') |
||
| 197 | |||
| 198 | /** |
||
| 199 | * montaDAEventoCTe |
||
| 200 | * Esta função monta a DAEventoCTe conforme as informações fornecidas para a classe |
||
| 201 | * durante sua construção. |
||
| 202 | * A definição de margens e posições iniciais para a impressão são estabelecidas no |
||
| 203 | * pelo conteúdo da funçao e podem ser modificados. |
||
| 204 | * |
||
| 205 | * @param string $orientacao (Opcional) Estabelece a orientação da impressão (ex. P-retrato), |
||
| 206 | * se nada for fornecido será usado o padrão da CTe |
||
| 207 | * @param string $papel (Opcional) Estabelece o tamanho do papel (ex. A4) |
||
| 208 | * @return string O ID do evento extraido do arquivo XML |
||
| 209 | */ |
||
| 210 | public function montaDAEventoCTe( |
||
| 289 | |||
| 290 | /** |
||
| 291 | * header |
||
| 292 | * @param integer $x |
||
| 293 | * @param integer $y |
||
| 294 | * @param integer $pag |
||
| 295 | * @return integer |
||
| 296 | */ |
||
| 297 | private function header( |
||
| 298 | $x, |
||
| 299 | $y, |
||
| 300 | $pag |
||
| 301 | ) { |
||
| 302 | $oldX = $x; |
||
| 303 | $oldY = $y; |
||
| 304 | $maxW = $this->wPrint; |
||
| 305 | //#################################################################################### |
||
| 306 | //coluna esquerda identificação do emitente |
||
| 307 | $w = round($maxW*0.41, 0);// 80; |
||
| 308 | if ($this->orientacao == 'P') { |
||
| 309 | $aFont = array('font'=>$this->fontePadrao, 'size'=>6, 'style'=>'I'); |
||
| 310 | } else { |
||
| 311 | $aFont = array('font'=>$this->fontePadrao, 'size'=>8, 'style'=>'B'); |
||
| 312 | } |
||
| 313 | $w1 = $w; |
||
| 314 | $h=32; |
||
| 315 | $oldY += $h; |
||
| 316 | $this->pdf->textBox($x, $y, $w, $h); |
||
| 317 | $texto = 'IDENTIFICAÇÃO DO EMITENTE'; |
||
| 318 | $this->pdf->textBox($x, $y, $w, 5, $texto, $aFont, 'T', 'C', 0, ''); |
||
| 319 | if (is_file($this->logomarca)) { |
||
| 320 | $logoInfo = getimagesize($this->logomarca); |
||
| 321 | //largura da imagem em mm |
||
| 322 | $logoWmm = ($logoInfo[0]/72)*25.4; |
||
| 323 | //altura da imagem em mm |
||
| 324 | $logoHmm = ($logoInfo[1]/72)*25.4; |
||
| 325 | if ($this->logoAlign=='L') { |
||
| 326 | $nImgW = round($w/3, 0); |
||
| 327 | $nImgH = round($logoHmm * ($nImgW/$logoWmm), 0); |
||
| 328 | $xImg = $x+1; |
||
| 329 | $yImg = round(($h-$nImgH)/2, 0)+$y; |
||
| 330 | //estabelecer posições do texto |
||
| 331 | $x1 = round($xImg + $nImgW +1, 0); |
||
| 332 | $y1 = round($h/3+$y, 0); |
||
| 333 | $tw = round(2*$w/3, 0); |
||
| 334 | } |
||
| 335 | if ($this->logoAlign=='C') { |
||
| 336 | $nImgH = round($h/3, 0); |
||
| 337 | $nImgW = round($logoWmm * ($nImgH/$logoHmm), 0); |
||
| 338 | $xImg = round(($w-$nImgW)/2+$x, 0); |
||
| 339 | $yImg = $y+3; |
||
| 340 | $x1 = $x; |
||
| 341 | $y1 = round($yImg + $nImgH + 1, 0); |
||
| 342 | $tw = $w; |
||
| 343 | } |
||
| 344 | if ($this->logoAlign=='R') { |
||
| 345 | $nImgW = round($w/3, 0); |
||
| 346 | $nImgH = round($logoHmm * ($nImgW/$logoWmm), 0); |
||
| 347 | $xImg = round($x+($w-(1+$nImgW)), 0); |
||
| 348 | $yImg = round(($h-$nImgH)/2, 0)+$y; |
||
| 349 | $x1 = $x; |
||
| 350 | $y1 = round($h/3+$y, 0); |
||
| 351 | $tw = round(2*$w/3, 0); |
||
| 352 | } |
||
| 353 | $this->pdf->image($this->logomarca, $xImg, $yImg, $nImgW, $nImgH, 'jpeg'); |
||
| 354 | } else { |
||
| 355 | $x1 = $x; |
||
| 356 | $y1 = round($h/3+$y, 0); |
||
| 357 | $tw = $w; |
||
| 358 | } |
||
| 359 | //Nome emitente |
||
| 360 | $aFont = array('font'=>$this->fontePadrao, 'size'=>12, 'style'=>'B'); |
||
| 361 | $texto = (isset($this->aEnd['razao'])?$this->aEnd['razao']:''); |
||
| 362 | $this->pdf->textBox($x1, $y1, $tw, 8, $texto, $aFont, 'T', 'C', 0, ''); |
||
| 363 | //endereço |
||
| 364 | $y1 = $y1+6; |
||
| 365 | $aFont = array('font'=>$this->fontePadrao, 'size'=>8, 'style'=>''); |
||
| 366 | $lgr = (isset($this->aEnd['logradouro'])?$this->aEnd['logradouro']:''); |
||
| 367 | $nro = (isset($this->aEnd['numero'])?$this->aEnd['numero']:''); |
||
| 368 | $cpl = (isset($this->aEnd['complemento'])?$this->aEnd['complemento']:''); |
||
| 369 | $bairro = (isset($this->aEnd['bairro'])?$this->aEnd['bairro']:''); |
||
| 370 | $CEP = (isset($this->aEnd['CEP'])?$this->aEnd['CEP']:''); |
||
| 371 | $CEP = $this->formatField($CEP, "#####-###"); |
||
| 372 | $mun = (isset($this->aEnd['municipio'])?$this->aEnd['municipio']:''); |
||
| 373 | $UF = (isset($this->aEnd['UF'])?$this->aEnd['UF']:''); |
||
| 374 | $fone = (isset($this->aEnd['telefone'])?$this->aEnd['telefone']:''); |
||
| 375 | $email = (isset($this->aEnd['email'])?$this->aEnd['email']:''); |
||
| 376 | $foneLen = strlen($fone); |
||
| 377 | if ($foneLen > 0) { |
||
| 378 | $fone2 = substr($fone, 0, $foneLen-4); |
||
| 379 | $fone1 = substr($fone, 0, $foneLen-8); |
||
| 380 | $fone = '(' . $fone1 . ') ' . substr($fone2, -4) . '-' . substr($fone, -4); |
||
| 381 | } else { |
||
| 382 | $fone = ''; |
||
| 383 | } |
||
| 384 | if ($email != '') { |
||
| 385 | $email = 'Email: '.$email; |
||
| 386 | } |
||
| 387 | $texto = ""; |
||
| 388 | $tmp_txt = trim(($lgr!=''?"$lgr, ":'').($nro!=0?$nro:"SN").($cpl!=''?" - $cpl":'')); |
||
| 389 | $tmp_txt = ($tmp_txt=='SN'?'':$tmp_txt); |
||
| 390 | $texto .= ($texto!='' && $tmp_txt!=''?"\n":'').$tmp_txt; |
||
| 391 | $tmp_txt = trim($bairro.($bairro!='' && $CEP!=''?" - ":'').$CEP); |
||
| 392 | $texto .= ($texto!='' && $tmp_txt!=''?"\n":'').$tmp_txt; |
||
| 393 | $tmp_txt = $mun; |
||
| 394 | $tmp_txt.= ($tmp_txt!='' && $UF!=''?" - ":'').$UF; |
||
| 395 | $tmp_txt.= ($tmp_txt!='' && $fone!=''?" - ":'').$fone; |
||
| 396 | $texto .= ($texto!='' && $tmp_txt!=''?"\n":'').$tmp_txt; |
||
| 397 | $tmp_txt = $email; |
||
| 398 | $texto .= ($texto!='' && $tmp_txt!=''?"\n":'').$tmp_txt; |
||
| 399 | $this->pdf->textBox($x1, $y1-2, $tw, 8, $texto, $aFont, 'T', 'C', 0, ''); |
||
| 400 | //################################################## |
||
| 401 | $w2 = round($maxW - $w, 0); |
||
| 402 | $x += $w; |
||
| 403 | $this->pdf->textBox($x, $y, $w2, $h); |
||
| 404 | $y1 = $y + $h; |
||
| 405 | $aFont = array('font'=>$this->fontePadrao, 'size'=>16, 'style'=>'B'); |
||
| 406 | if ($this->tpEvento=='110110') { |
||
| 407 | $texto='Representação Gráfica de CCe'; |
||
| 408 | } else { |
||
| 409 | $texto='Representação Gráfica de Evento'; |
||
| 410 | } |
||
| 411 | $this->pdf->textBox($x, $y+2, $w2, 8, $texto, $aFont, 'T', 'C', 0, ''); |
||
| 412 | $aFont = array('font'=>$this->fontePadrao, 'size'=>12, 'style'=>'I'); |
||
| 413 | if ($this->tpEvento=='110110') { |
||
| 414 | $texto='(Carta de Correção Eletrônica)'; |
||
| 415 | } elseif ($this->tpEvento=='110111') { |
||
| 416 | $texto='(Cancelamento de CTe)'; |
||
| 417 | } |
||
| 418 | $this->pdf->textBox($x, $y+7, $w2, 8, $texto, $aFont, 'T', 'C', 0, ''); |
||
| 419 | $texto = 'ID do Evento: '.$this->id; |
||
| 420 | $aFont = array('font'=>$this->fontePadrao, 'size'=>10, 'style'=>''); |
||
| 421 | $this->pdf->textBox($x, $y+15, $w2, 8, $texto, $aFont, 'T', 'L', 0, ''); |
||
| 422 | $tsHora = $this->convertTime($this->dhEvento); |
||
| 423 | $texto = 'Criado em : '. date('d/m/Y H:i:s', $tsHora); |
||
| 424 | $this->pdf->textBox($x, $y+20, $w2, 8, $texto, $aFont, 'T', 'L', 0, ''); |
||
| 425 | $tsHora = $this->convertTime($this->dhRegEvento); |
||
| 426 | $texto = 'Prococolo: '.$this->nProt.' - Registrado na SEFAZ em: '.date('d/m/Y H:i:s', $tsHora); |
||
| 427 | $this->pdf->textBox($x, $y+25, $w2, 8, $texto, $aFont, 'T', 'L', 0, ''); |
||
| 428 | //#################################################### |
||
| 429 | $x = $oldX; |
||
| 430 | $this->pdf->textBox($x, $y1, $maxW, 40); |
||
| 431 | $sY = $y1+40; |
||
| 432 | if ($this->tpEvento=='110110') { |
||
| 433 | $texto = 'De acordo com as determinações legais vigentes, vimos por meio ' |
||
| 434 | . 'desta comunicar-lhe que o Conhecimento de Transporte, abaixo referenciado, ' |
||
| 435 | . 'contêm irregularidades que estão destacadas e suas respectivas ' |
||
| 436 | . 'correções, solicitamos que sejam aplicadas essas correções ao ' |
||
| 437 | . 'executar seus lançamentos fiscais.'; |
||
| 438 | } elseif ($this->tpEvento=='110111') { |
||
| 439 | $texto = 'De acordo com as determinações legais vigentes, vimos por meio ' |
||
| 440 | . 'desta comunicar-lhe que o Conhecimento de Transporte, abaixo referenciado, está ' |
||
| 441 | . 'cancelado, solicitamos que sejam aplicadas essas correções ao ' |
||
| 442 | . 'executar seus lançamentos fiscais.'; |
||
| 443 | } |
||
| 444 | $aFont = array('font'=>$this->fontePadrao, 'size'=>10, 'style'=>''); |
||
| 445 | $this->pdf->textBox($x+5, $y1, $maxW-5, 20, $texto, $aFont, 'T', 'L', 0, '', false); |
||
| 446 | //############################################ |
||
| 447 | $x = $oldX; |
||
| 448 | $y = $y1; |
||
| 449 | $aFont = array('font'=>$this->fontePadrao, 'size'=>12, 'style'=>'B'); |
||
| 450 | $numNF = substr($this->chCTe, 25, 9); |
||
| 451 | $serie = substr($this->chCTe, 22, 3); |
||
| 452 | $numNF = $this->formatField($numNF, "###.###.###"); |
||
| 453 | $texto = "Conhecimento: " . $numNF .' - Série: '.$serie; |
||
| 454 | $this->pdf->textBox($x+2, $y+19, $w2, 8, $texto, $aFont, 'T', 'L', 0, ''); |
||
| 455 | $bW = 87; |
||
| 456 | $bH = 15; |
||
| 457 | $x = 55; |
||
| 458 | $y = $y1+13; |
||
| 459 | $w = $maxW; |
||
| 460 | $this->pdf->setFillColor(0, 0, 0); |
||
| 461 | $this->pdf->code128($x+(($w-$bW)/2), $y+2, $this->chCTe, $bW, $bH); |
||
| 462 | $this->pdf->setFillColor(255, 255, 255); |
||
| 463 | $y1 = $y+2+$bH; |
||
| 464 | $aFont = array('font'=>$this->fontePadrao, 'size'=>10, 'style'=>''); |
||
| 465 | $texto = $this->formatField($this->chCTe, $this->formatoChave); |
||
| 466 | $this->pdf->textBox($x, $y1, $w-2, $h, $texto, $aFont, 'T', 'C', 0, ''); |
||
| 467 | $retVal = $sY+2; |
||
| 468 | if ($this->tpEvento=='110110') { |
||
| 469 | $x = $oldX; |
||
| 470 | $this->pdf->textBox($x, $sY, $maxW, 15); |
||
| 471 | $texto = $this->xCondUso; |
||
| 472 | $aFont = array('font'=>$this->fontePadrao, 'size'=>8, 'style'=>'I'); |
||
| 473 | $this->pdf->textBox($x+2, $sY+2, $maxW-2, 15, $texto, $aFont, 'T', 'L', 0, '', false); |
||
| 474 | $retVal = $sY+2; |
||
| 475 | } |
||
| 476 | if ($this->tpAmb != 1) { |
||
| 477 | $x = 10; |
||
| 478 | if ($this->orientacao == 'P') { |
||
| 479 | $y = round($this->hPrint*2/3, 0); |
||
| 480 | } else { |
||
| 481 | $y = round($this->hPrint/2, 0); |
||
| 482 | } |
||
| 483 | $h = 5; |
||
| 484 | $w = $maxW-(2*$x); |
||
| 485 | $this->pdf->setTextColor(90, 90, 90); |
||
| 486 | $texto = "SEM VALOR FISCAL"; |
||
| 487 | $aFont = array('font'=>$this->fontePadrao, 'size'=>48, 'style'=>'B'); |
||
| 488 | $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, ''); |
||
| 489 | $aFont = array('font'=>$this->fontePadrao, 'size'=>30, 'style'=>'B'); |
||
| 490 | $texto = "AMBIENTE DE HOMOLOGAÇÃO"; |
||
| 491 | $this->pdf->textBox($x, $y+14, $w, $h, $texto, $aFont, 'C', 'C', 0, ''); |
||
| 492 | $this->pdf->setTextColor(0, 0, 0); |
||
| 493 | } |
||
| 494 | return $retVal; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * body |
||
| 499 | * @param integer $x |
||
| 500 | * @param integer $y |
||
| 501 | */ |
||
| 502 | private function body($x, $y) |
||
| 503 | { |
||
| 504 | $maxW = $this->wPrint; |
||
| 505 | if ($this->tpEvento=='110110') { |
||
| 506 | $texto = 'CORREÇÕES A SEREM CONSIDERADAS'; |
||
| 507 | } else { |
||
| 508 | $texto = 'JUSTIFICATIVA DO CANCELAMENTO'; |
||
| 509 | } |
||
| 510 | $aFont = array('font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'); |
||
| 511 | $this->pdf->textBox($x, $y, $maxW, 5, $texto, $aFont, 'T', 'L', 0, '', false); |
||
| 512 | $y += 5; |
||
| 513 | $this->pdf->textBox($x, $y, $maxW, 190); |
||
| 514 | if ($this->tpEvento=='110110') { |
||
| 515 | $texto = $this->xCorrecao; |
||
| 516 | } elseif ($this->tpEvento=='110111') { |
||
| 517 | $texto = $this->xJust; |
||
| 518 | } |
||
| 519 | $aFont = array('font'=>$this->fontePadrao, 'size'=>12, 'style'=>'B'); |
||
| 520 | $this->pdf->textBox($x+2, $y+2, $maxW-2, 150, $texto, $aFont, 'T', 'L', 0, '', false); |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * footer |
||
| 525 | * @param integer $x |
||
| 526 | * @param integer $y |
||
| 527 | */ |
||
| 528 | private function footer($x, $y) |
||
| 529 | { |
||
| 530 | $w = $this->wPrint; |
||
| 531 | if ($this->tpEvento=='110110') { |
||
| 532 | $texto = "Este documento é uma representação gráfica da CCe e foi " |
||
| 533 | . "impresso apenas para sua informação e não possue validade fiscal." |
||
| 534 | . "\n A CCe deve ser recebida e mantida em arquivo eletrônico XML e " |
||
| 535 | . "pode ser consultada através dos Portais das SEFAZ."; |
||
| 536 | } elseif ($this->tpEvento=='110111') { |
||
| 537 | $texto = "Este documento é uma representação gráfica do evento de CTe e foi " |
||
| 538 | . "impresso apenas para sua informação e não possue validade fiscal." |
||
| 539 | . "\n O Evento deve ser recebido e mantido em arquivo eletrônico XML e " |
||
| 540 | . "pode ser consultada através dos Portais das SEFAZ."; |
||
| 541 | } |
||
| 542 | $aFont = array('font'=>$this->fontePadrao, 'size'=>10, 'style'=>'I'); |
||
| 543 | $this->pdf->textBox($x, $y, $w, 20, $texto, $aFont, 'T', 'C', 0, '', false); |
||
| 544 | $y = $this->hPrint -4; |
||
| 545 | $texto = "Impresso em ". date('d/m/Y H:i:s'); |
||
| 546 | $w = $this->wPrint-4; |
||
| 547 | $aFont = array('font'=>$this->fontePadrao, 'size'=>6, 'style'=>'I'); |
||
| 548 | $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'L', 0, ''); |
||
| 549 | $texto = "Daevento ver. " . $this->version |
||
| 550 | . " Powered by NFePHP (GNU/GPLv3 GNU/LGPLv3) © www.nfephp.org"; |
||
| 551 | $aFont = array('font'=>$this->fontePadrao, 'size'=>6, 'style'=>'I'); |
||
| 552 | $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'R', 0, 'http://www.nfephp.org'); |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * printDAEventoCTe |
||
| 557 | * @param string $nome |
||
| 558 | * @param string $destino |
||
| 559 | * @param string $printer |
||
| 560 | * @return type |
||
| 561 | */ |
||
| 562 | public function printDAEventoCTe($nome = '', $destino = 'I', $printer = '') |
||
| 570 | } |
||
| 571 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.