Test Setup Failed
Push — parse-domain-literal ( 97f1d2 )
by Eduardo Gulias
01:49
created

MultipleValidationWithAnd::addNewError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Egulias\EmailValidator\Validation;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Result\InvalidEmail;
7
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
8
use Egulias\EmailValidator\Result\MultipleErrors;
9
10
class MultipleValidationWithAnd implements EmailValidation
11
{
12
    /**
13
     * If one of validations fails, the remaining validations will be skept.
14
     * This means MultipleErrors will only contain a single error, the first found.
15
     */
16
    const STOP_ON_ERROR = 0;
17
18
    /**
19
     * All of validations will be invoked even if one of them got failure.
20
     * So MultipleErrors will contain all causes.
21
     */
22
    const ALLOW_ALL_ERRORS = 1;
23
24
    /**
25
     * @var EmailValidation[]
26
     */
27
    private $validations = [];
28
29
    /**
30
     * @var array
31
     */
32
    private $warnings = [];
33
34
    /**
35
     * @var MultipleErrors|null
36
     */
37
    private $error;
38
39
    /**
40
     * @var int
41
     */
42
    private $mode;
43
44
    /**
45
     * @param EmailValidation[] $validations The validations.
46
     * @param int               $mode        The validation mode (one of the constants).
47
     */
48
    public function __construct(array $validations, $mode = self::ALLOW_ALL_ERRORS)
49
    {
50
        if (count($validations) == 0) {
51
            throw new EmptyValidationList();
52
        }
53
54
        $this->validations = $validations;
55
        $this->mode = $mode;
56
        $this->error = new MultipleErrors();
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function isValid($email, EmailLexer $emailLexer)
63
    {
64
        $result = true;
65
        $errors = [];
0 ignored issues
show
Unused Code introduced by
$errors is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
        foreach ($this->validations as $validation) {
67
            $emailLexer->reset();
68
            $validationResult = $validation->isValid($email, $emailLexer);
69
            $result = $result && $validationResult;
70
            $this->processValidation($validation);
71
72
            if ($this->shouldStop($result)) {
73
                break;
74
            }
75
        }
76
77
        return $result;
78
    }
79
80
    private function processValidation(EmailValidation $validation)
81
    {
82
        $this->warnings = array_merge($this->warnings, $validation->getWarnings());
83
        if (null !== $validation->getError()) {
84
            $this->error->addError($validation->getError()->reason());
85
        }
86
    }
87
88
    /**
89
     * @param bool $result
90
     *
91
     * @return bool
92
     */
93
    private function shouldStop($result)
94
    {
95
        return !$result && $this->mode === self::STOP_ON_ERROR;
96
    }
97
98
    /**
99
     * Returns the validation errors.
100
     *
101
     */
102
    public function getError() : InvalidEmail
103
    {
104
        return $this->error;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getWarnings()
111
    {
112
        return $this->warnings;
113
    }
114
}
115