Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#957)
by Henrique
02:11
created

AbstractRelated   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 20
dl 0
loc 76
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A decision() 0 5 4
A validate() 0 8 3
A __construct() 0 12 4
A check() 0 8 3
A assert() 0 13 4
A setName() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use Respect\Validation\Exceptions\ValidationException;
17
use Respect\Validation\Validatable;
18
use function is_scalar;
19
20
abstract class AbstractRelated extends AbstractRule
21
{
22
    public $mandatory = true;
23
    public $reference = '';
24
    public $validator;
25
26
    abstract public function hasReference($input);
27
28
    abstract public function getReferenceValue($input);
29
30 25
    public function __construct($reference, Validatable $validator = null, $mandatory = true)
31
    {
32 25
        if (is_scalar($reference)) {
33 25
            $this->setName((string) $reference);
34 25
            if ($validator && !$validator->getName()) {
35 20
                $validator->setName((string) $reference);
36
            }
37
        }
38
39 25
        $this->reference = $reference;
40 25
        $this->validator = $validator;
41 25
        $this->mandatory = $mandatory;
42 25
    }
43
44 18
    public function setName(string $name): Validatable
45
    {
46 18
        parent::setName($name);
47
48 18
        if ($this->validator instanceof Validatable) {
49
            $this->validator->setName($name);
50
        }
51
52 18
        return $this;
53
    }
54
55 16
    private function decision($type, $hasReference, $input)
56
    {
57 16
        return (!$this->mandatory && !$hasReference)
58 16
            || (is_null($this->validator)
59 16
                || $this->validator->$type($this->getReferenceValue($input)));
60
    }
61
62 12
    public function assert($input): void
63
    {
64 12
        $hasReference = $this->hasReference($input);
65 12
        if ($this->mandatory && !$hasReference) {
66 6
            throw $this->reportError($input, ['hasReference' => false]);
67
        }
68
69
        try {
70 12
            $this->decision('assert', $hasReference, $input);
71 9
        } catch (ValidationException $e) {
72
            throw $this
73 9
                ->reportError($this->reference, ['hasReference' => true])
74 9
                ->addRelated($e);
0 ignored issues
show
Bug introduced by
The method addRelated() does not exist on Respect\Validation\Exceptions\ValidationException. It seems like you code against a sub-type of Respect\Validation\Exceptions\ValidationException such as Respect\Validation\Excep...stedValidationException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
                ->/** @scrutinizer ignore-call */ addRelated($e);
Loading history...
75
        }
76 6
    }
77
78 2
    public function check($input): void
79
    {
80 2
        $hasReference = $this->hasReference($input);
81 2
        if ($this->mandatory && !$hasReference) {
82
            throw $this->reportError($input, ['hasReference' => false]);
83
        }
84
85 2
        $this->decision('check', $hasReference, $input);
86 1
    }
87
88 3
    public function validate($input): bool
89
    {
90 3
        $hasReference = $this->hasReference($input);
91 3
        if ($this->mandatory && !$hasReference) {
92 2
            return false;
93
        }
94
95 2
        return $this->decision('validate', $hasReference, $input);
96
    }
97
}
98