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

Completed
Pull Request — master (#957)
by Henrique
02:18
created

AbstractRelated   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 57.89%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A assert() 0 13 4
A check() 0 8 3
A decision() 0 5 4
A validate() 0 8 3
A __construct() 0 12 4
A setId() 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 10
    public function __construct($reference, Validatable $validator = null, $mandatory = true)
31
    {
32 10
        if (is_scalar($reference)) {
33 10
            $this->setId($reference);
34 10
            if ($validator && !$validator->getId()) {
35 5
                $validator->setId($reference);
36
            }
37
        }
38
39 10
        $this->reference = $reference;
40 10
        $this->validator = $validator;
41 10
        $this->mandatory = $mandatory;
42 10
    }
43
44 3
    public function setId($name)
45
    {
46 3
        parent::setId($name);
47
48 3
        if ($this->validator instanceof Validatable) {
49
            $this->validator->setId($name);
50
        }
51
52 3
        return $this;
53
    }
54
55 1
    private function decision($type, $hasReference, $input)
56
    {
57 1
        return (!$this->mandatory && !$hasReference)
58 1
            || (is_null($this->validator)
59 1
                || $this->validator->$type($this->getReferenceValue($input)));
60
    }
61
62
    public function assert($input): void
63
    {
64
        $hasReference = $this->hasReference($input);
65
        if ($this->mandatory && !$hasReference) {
66
            throw $this->reportError($input, ['hasReference' => false]);
67
        }
68
69
        try {
70
            $this->decision('assert', $hasReference, $input);
71
        } catch (ValidationException $e) {
72
            throw $this
73
                ->reportError($this->reference, ['hasReference' => true])
74
                ->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
    }
77
78
    public function check($input): void
79
    {
80
        $hasReference = $this->hasReference($input);
81
        if ($this->mandatory && !$hasReference) {
82
            throw $this->reportError($input, ['hasReference' => false]);
83
        }
84
85
        $this->decision('check', $hasReference, $input);
86
    }
87
88 2
    public function validate($input): bool
89
    {
90 2
        $hasReference = $this->hasReference($input);
91 2
        if ($this->mandatory && !$hasReference) {
92 1
            return false;
93
        }
94
95 1
        return $this->decision('validate', $hasReference, $input);
96
    }
97
}
98