CNAB400Conv7Processor::processarHeaderArquivo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 2
Metric Value
c 6
b 1
f 2
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Convenio\Processor;
4
5
use Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Convenio\DetailConvenio;
6
use Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Convenio\HeaderConvenio;
7
8
/**
9
 * Classe para leitura de arquivos de retorno de cobranças no padrão CNAB400/CBR643 com convênio de 7 posições.<br/>
10
 * Layout Padrão CNAB/Febraban 400 posições<br/>.
11
 * Baseado na documentação para "Layout de Arquivo Retorno para convênios na
12
 * faixa numérica entre 1.000.000 a 9.999.999 (Convênios de 7 posições). Versão Set/09"
13
 * do Banco do Brasil (arquivo Doc2628CBR643Pos7.pdf),
14
 * disponível em http://www.bb.com.br/docs/pub/emp/empl/dwn/Doc2628CBR643Pos7.pdf
15
 * @author Ítalo Lelis de Vietro <[email protected]>
16
 */
17
class CNAB400Conv7Processor extends AbstractCNAB400Processor
18
{
19
    /**
20
     * @property int DETALHE Define o valor que identifica uma coluna do tipo DETALHE 
21
     */
22
    const DETALHE = 7;
23
24
    public function createHeader()
25
    {
26
        return new HeaderConvenio();
27
    }
28
29
    public function createDetail()
30
    {
31
        return new DetailConvenio();
32
    }
33
34
    /**
35
     * Processa a linha header do arquivo
36
     * @param string $linha Linha do header de arquivo processado
37
     * @return string Retorna um vetor contendo os dados dos campos do header do arquivo. 
38
     */
39
    protected function processarHeaderArquivo($linha)
40
    {
41
        $header = parent::processarHeaderArquivo($linha);
42
        $header
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 setConvenio() does only exist in the following sub-classes of Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Header: Umbrella\Ya\RetornoBolet...Convenio\HeaderConvenio. 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...
43
            ->addComplemento($linha->substr(108, 42)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
44
            ->setConvenio($linha->substr(150, 7)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
45
            ->addComplemento($linha->substr(108, 42)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
46
        ;
47
48
        return $header;
49
    }
50
51
    /**
52
     * Processa uma linha detalhe do arquivo.
53
     * @param string $linha Linha detalhe do arquivo processado
54
     * @return string Retorna um vetor contendo os dados dos campos da linha detalhe. 
55
     */
56
    protected function processarDetalhe($linha)
57
    {
58
        $detail = parent::processarDetalhe($linha);
59
        $detail
60
            ->setConvenio($linha->substr(32, 7)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
61
            ->setControle($linha->substr(38, 25)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
62
            ->setNossoNumero($linha->substr(64, 17)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
63
            ->setTipoCobranca($linha->substr(81, 1)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
64
            ->setTipoCobrancaCmd72($linha->substr(82, 1)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
65
            ->setDiasCalculo($linha->substr(83, 4)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
66
            ->setNatureza($linha->substr(87, 2)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
67
            ->setPrefixoTitulo($linha->substr(89, 3)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
68
            ->setVariacaoCarteira($linha->substr(92, 3)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
69
            ->setContaCaucao($linha->substr(95, 1)->trim())
0 ignored issues
show
Bug introduced by
The method substr cannot be called on $linha (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
70
        ;
71
        return $detail;
72
    }
73
}
74