AddressingAccountValidator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 21.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 16
loc 76
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 6 1
A validateBranch() 0 7 4
A validateNumber() 0 7 4
A validateType() 16 16 4

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\AddressingAccount;
6
7
/**
8
 * AddressingAccountValidator class
9
 *
10
 * PHP version 7.3|7.4|8.0
11
 *
12
 * @author    WeDev Brasil Team <[email protected]>
13
 * @author    Rafael Teixeira <[email protected]>
14
 * @copyright 2021 We Dev Tecnologia Ltda
15
 * @link      https://github.com/wedevBr/bankly-laravel/
16
 */
17
class AddressingAccountValidator
18
{
19
    /** @var AddressingAccount */
20
    private $addressingAccount;
21
22
    /**
23
     * @param AddressingAccount $addressingAccount
24
     */
25
    public function __construct(AddressingAccount $addressingAccount)
26
    {
27
        $this->addressingAccount = $addressingAccount;
28
    }
29
30
    /**
31
     * Validate the attributes of the Addressing Account class
32
     *
33
     * @return void
34
     */
35
    public function validate(): void
36
    {
37
        $this->validateBranch();
38
        $this->validateNumber();
39
        $this->validateType();
40
    }
41
42
    /**
43
     * This validates a branch account
44
     *
45
     * @return void
46
     * @throws \InvalidArgumentException
47
     */
48
    private function validateBranch()
49
    {
50
        $branch = $this->addressingAccount->branch;
51
        if (empty($branch) || !is_string($branch) || !is_numeric($branch)) {
52
            throw new \InvalidArgumentException('branch should be a numeric string');
53
        }
54
    }
55
56
    /**
57
     * This validates a number account
58
     *
59
     * @return void
60
     * @throws \InvalidArgumentException
61
     */
62
    private function validateNumber()
63
    {
64
        $number = $this->addressingAccount->number;
65
        if (empty($number) || !is_string($number) || !is_numeric($number)) {
66
            throw new \InvalidArgumentException('number account should be a numeric string');
67
        }
68
    }
69
70
    /**
71
     * This validates a type account
72
     *
73
     * @return void
74
     * @throws \InvalidArgumentException
75
     */
76 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...
77
    {
78
        $type = $this->addressingAccount->type;
79
        if (empty($type) || !is_string($type)) {
80
            throw new \InvalidArgumentException('type account should be a string');
81
        }
82
83
        $typeList = [
84
            'CHECKING',
85
            'SAVINGS',
86
            'SALARY',
87
        ];
88
        if (!in_array($this->addressingAccount->type, $typeList)) {
89
            throw new \InvalidArgumentException('this account type is not valid');
90
        }
91
    }
92
}
93