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 (#910)
by Henrique
02:24
created

AbstractRelated::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 3
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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
19
abstract class AbstractRelated extends AbstractRule
20
{
21
    public $mandatory = true;
22
    public $reference = '';
23
    public $validator;
24
25
    abstract public function hasReference($input);
26
27
    abstract public function getReferenceValue($input);
28
29 10
    public function __construct($reference, Validatable $validator = null, $mandatory = true)
30
    {
31 10
        $this->setName($reference);
32 10
        if ($validator && !$validator->getName()) {
33 5
            $validator->setName($reference);
34
        }
35
36 10
        $this->reference = $reference;
37 10
        $this->validator = $validator;
38 10
        $this->mandatory = $mandatory;
39 10
    }
40
41 3
    public function setName($name)
42
    {
43 3
        parent::setName($name);
44
45 3
        if ($this->validator instanceof Validatable) {
46
            $this->validator->setName($name);
47
        }
48
49 3
        return $this;
50
    }
51
52 1
    private function decision($type, $hasReference, $input)
53
    {
54 1
        return (!$this->mandatory && !$hasReference)
55 1
            || (is_null($this->validator)
56 1
                || $this->validator->$type($this->getReferenceValue($input)));
57
    }
58
59
    public function assert($input): void
60
    {
61
        $hasReference = $this->hasReference($input);
62
        if ($this->mandatory && !$hasReference) {
63
            throw $this->reportError($input, ['hasReference' => false]);
64
        }
65
66
        try {
67
            $this->decision('assert', $hasReference, $input);
68
        } catch (ValidationException $e) {
69
            throw $this
70
                ->reportError($this->reference, ['hasReference' => true])
71
                ->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

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