Completed
Push — master ( 5971d3...dcc755 )
by
unknown
26s queued 11s
created

AddressingKeyValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 18
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 5 1
A validateType() 18 18 4
A validateValue() 0 7 3

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace WeDevBr\Bankly\Validators\Pix;
4
5
use WeDevBr\Bankly\Types\Pix\AddressingKey;
6
7
class AddressingKeyValidator
8
{
9
    /** @var AddressingKey */
10
    private $addressingKey;
11
12
    /**
13
     * @param AddressingKey $addressingKey
14
     */
15
    public function __construct(AddressingKey $addressingKey)
16
    {
17
        $this->addressingKey = $addressingKey;
18
    }
19
20
    /**
21
     * Validate the attributes of the Addressing Key class
22
     *
23
     * @return void
24
     */
25
    public function validate(): void
26
    {
27
        $this->validateType();
28
        $this->validateValue();
29
    }
30
31
    /**
32
     * This validates a type of pix key
33
     *
34
     * @return void
35
     * @throws InvalidArgumentException
36
     */
37 View Code Duplication
    private function validateType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
38
    {
39
        $type = $this->addressingKey->type;
40
        if (empty($type) || !is_string($type)) {
41
            throw new \InvalidArgumentException('type should be a string');
42
        }
43
44
        $typeList = [
45
            'CPF',
46
            'CNPJ',
47
            'EMAIL',
48
            'PHONE',
49
            'EVP',
50
        ];
51
        if (!in_array($this->addressingKey->type, $typeList)) {
52
            throw new \InvalidArgumentException('this key type is not valid');
53
        }
54
    }
55
56
    /**
57
     * This validates a key value
58
     *
59
     * @return void
60
     * @throws InvalidArgumentException
61
     */
62
    private function validateValue()
63
    {
64
        $value = $this->addressingKey->value;
65
        if (empty($value) || !is_string($value)) {
66
            throw new \InvalidArgumentException('value should be a string');
67
        }
68
    }
69
}
70