CNAB400BradescoProcessor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 0
cbo 9
dl 0
loc 132
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createHeader() 0 4 1
A createDetail() 0 4 1
A createTrailer() 0 4 1
B processarHeaderArquivo() 0 33 1
A processarDetalhe() 0 55 1
A processarLinha() 0 4 1
1
<?php
2
3
namespace Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Bradesco\Processor;
4
5
use Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Bradesco\DetailBradesco;
6
use Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Bradesco\HeaderBradesco;
7
use Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Convenio\Processor\AbstractCNAB400Processor;
8
use Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Trailer;
9
use Umbrella\Ya\RetornoBoleto\Model\Banco;
10
use Umbrella\Ya\RetornoBoleto\Model\Cedente;
11
use Umbrella\Ya\RetornoBoleto\Model\Empresa;
12
13
/**
14
 * Classe abstrata para leitura de arquivos de retorno de cobranças no padrão CNAB400/CBR643.<br/>
15
 * Layout Padrão CNAB/Febraban 400 posições<br/>.
16
 * Baseado na documentação para "Layout de Arquivo Retorno para Convênios
17
 * na faixa numérica entre 000.001 a 999.999 (Convênios de até 6 posições). Versão Set/09" e
18
 * "Layout de Arquivo Retorno para convênios na faixa numérica entre 1.000.000 a 9.999.999
19
 * (Convênios de 7 posições). Versão Set/09" do Banco do Brasil 
20
 * (arquivos Doc8826BR643Pos6.pdf e Doc2628CBR643Pos7.pdf)
21
 * @author Ítalo Lelis de Vietro <[email protected]>
22
 */
23
class CNAB400BradescoProcessor extends AbstractCNAB400Processor
24
{
25
    /**
26
     * @property int HEADER_ARQUIVO Define o valor que identifica uma coluna do tipo HEADER DE ARQUIVO 
27
     */
28
    const HEADER_ARQUIVO = 0;
29
30
    /**
31
     * @property int TRAILER_ARQUIVO Define o valor que identifica uma coluna do tipo TRAILER DE ARQUIVO 
32
     */
33
    const TRAILER_ARQUIVO = 9;
34
35
    public function createHeader()
36
    {
37
        return new HeaderBradesco();
38
    }
39
40
    public function createDetail()
41
    {
42
        return new DetailBradesco();
43
    }
44
45
    public function createTrailer()
46
    {
47
        return new Trailer();
48
    }
49
50
    /**
51
     * Processa a linha header do arquivo
52
     * @param string $linha Linha do header de arquivo processado
53
     * @return string Retorna um vetor contendo os dados dos campos do header do arquivo. 
54
     */
55
    protected function processarHeaderArquivo($linha)
56
    {
57
        $header = $this->createHeader();
58
        //X = ALFANUMÉRICO 9 = NUMÉRICO V = VÍRGULA DECIMAL ASSUMIDA
59
        $header->setRegistro(substr($linha, 1, 1))
60
            ->setTipoOperacao(substr($linha, 2, 1))
61
            ->setIdTipoOperacao(substr($linha, 3, 7))
62
            ->setIdTipoServico(substr($linha, 10, 2))
63
            ->setTipoServico(substr($linha, 12, 15));
64
65
        $empresa = new Empresa();
66
        $empresa->setCod(substr($linha, 27, 20))
67
            ->setNome(substr($linha, 47, 30));
68
69
        $banco = new Banco();
70
        $banco
71
            ->setCod(substr($linha, 77, 3))
72
            ->setNome(substr($linha, 80, 15));
73
74
75
        $cedente = new Cedente();
76
        $cedente->setBanco($banco)
77
            ->setNome(substr($linha, 47, 30));
78
79
        $header->setEmpresa($empresa)
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Header as the method setDensidadeGravacao() does only exist in the following sub-classes of Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Header: Umbrella\Ya\RetornoBolet...Bradesco\HeaderBradesco. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
80
            ->setCedente($cedente)
81
            ->setDataGravacao($this->createDate(substr($linha, 95, 6)))
82
            ->setDensidadeGravacao(substr($linha, 101, 8))
83
            ->setNumAvisoCredito(substr($linha, 109, 5))
84
            ->setSequencialReg(substr($linha, 395, 6));
85
86
        return $header;
87
    }
88
89
    /**
90
     * Processa uma linha detalhe do arquivo.
91
     * @param string $linha Linha detalhe do arquivo processado
92
     * @return string Retorna um vetor contendo os dados dos campos da linha detalhe. 
93
     */
94
    protected function processarDetalhe($linha)
95
    {
96
        $detail = $this->createDetail();
97
98
        $bancoEmissor = new Banco();
99
        $bancoEmissor
100
            ->setAgencia(substr($linha, 22, 1))
101
            ->setDvAgencia(substr($linha, 31, 1))
102
            ->setConta(substr($linha, 23, 8))
103
            ->setDvConta(substr($linha, 31, 1));
104
105
        $bancoRecebedor = new Banco();
106
        $bancoRecebedor
107
            ->setCod(substr($linha, 166, 3))
108
            ->setAgencia(substr($linha, 169, 4))
109
            ->setDvAgencia(substr($linha, 173, 1));
110
111
        $bancoEmpresa = new Banco();
112
        $bancoEmpresa->setCod(substr($linha, 21, 17));
113
        $empresa = new Empresa();
114
        $empresa
115
            ->setBanco($bancoEmpresa)
116
            ->setTipoInscricao(substr($linha, 2, 2))
117
            ->setNumInscricao(substr($linha, 4, 14))
118
            ->addReservado(substr($linha, 38, 25));
0 ignored issues
show
Documentation introduced by
substr($linha, 38, 25) is of type string, but the function expects a object<Stringy\Stringy>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
120
        $detail
0 ignored issues
show
Bug introduced by
The method setNumOcorrencia() does not seem to exist on object<Umbrella\Ya\Retor...to\Cnab\Cnab400\Detail>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
            ->setBancoEmissor($bancoEmissor)
122
            ->setBancoRecebedor($bancoRecebedor)
123
            ->setRegistro(substr($linha, 1, 1))
124
            ->setNumOcorrencia(substr($linha, 117, 10))
125
            ->setDataVencimento(substr($linha, 147, 6))
126
            ->setValor($this->formataNumero(substr($linha, 153, 13)))
127
            ->setDespCobranca(substr($linha, 176, 2))
128
            ->setOutrasDespesas($this->formataNumero(substr($linha, 189, 13)))
129
            ->setJurosAtraso($this->formataNumero(substr($linha, 202, 13)))
130
            ->setTaxaIof($this->formataNumero(substr($linha, 215, 13)))
131
            ->setDescontoConcedido($this->formataNumero(substr($linha, 241, 13)))
132
            ->setValorRecebido($this->formataNumero(substr($linha, 254, 13)))
133
            ->setJurosMora($this->formataNumero(substr($linha, 267, 13)))
134
            ->setOutrosRecebimentos($this->formataNumero(substr($linha, 280, 13)))
135
            ->setValorAbatimento($this->formataNumero(substr($linha, 228, 13)))
136
            ->setValorLancamento($this->formataNumero(substr($linha, 306, 13)))
137
            ->setIndicativoDc(substr($linha, 319, 1))
138
            ->setIndicadorValor(substr($linha, 320, 1))
139
            ->setValorAjuste($this->formataNumero(substr($linha, 321, 12)))
140
            ->setSequencial(substr($linha, 395, 6))
141
            ->setMotivoCodOcorrencia(substr($linha, 319, 10))
142
            ->setNumCartorio(substr($linha, 369, 2))
143
            ->setNumCartorio(substr($linha, 369, 2))
144
            ->setNumCartorio(substr($linha, 369, 2))
145
        ;
146
147
        return $detail;
148
    }
149
150
    public function processarLinha($numLn, $linha)
151
    {
152
        
153
    }
154
}
155