Passed
Push — master ( 5f9e30...9e7e6b )
by Dedipyaman
02:07
created

AbstractValidator::failure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Phypes\Validator;
4
5
use Phypes\Exception\PrematureErrorCallException;
6
use Phypes\ErrorCode\Result;
0 ignored issues
show
Bug introduced by
The type Phypes\ErrorCode\Result was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
abstract class AbstractValidator implements Validator
9
{
10
    /**
11
     * Has this validator been used before?
12
     * if no, getErrorMessage() will throw an exception
13
     * @var bool $validated
14
     */
15
    protected $validated = false;
16
    /**
17
     * Stores the error message on validation failure
18
     * @var string|null $error
19
     */
20
    protected $error;
21
    /**
22
     * Error code to allow for search with int rather than string
23
     * @var integer $errorCode
24
     */
25
    protected $errorCode;
26
    /**
27
     * Return the error message upon validation, null returned if no errors are set
28
     * @return string $error
29
     * @throws PrematureErrorCallException
30
     */
31
    public function getErrorMessage(): ?string
32
    {
33
        /**
34
         * Extra check to make sure someone doesn't call this method before actually calling isValid()
35
         */
36
        if ($this->validated == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
37
            throw new PrematureErrorCallException();
38
        }
39
40
        return $this->error;
41
    }
42
43
    /**
44
     * Probably doesn't need to be included in the interface
45
     * @return int|null
46
     * @throws PrematureErrorCallException
47
     */
48
    public function getErrorCode() : ?int
49
    {
50
        if ($this->validated == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
51
            throw new PrematureErrorCallException();
52
        }
53
54
        return $this->errorCode;
55
    }
56
57
    protected function success()
58
    {
59
        return new Result(true);
60
    }
61
    protected function failure($errors = []) {
62
        return new Result(false, $errors);
63
    }
64
}