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
Push — master ( b7043b...985b6f )
by Henrique
03:04
created

AbstractRelated::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
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\NestedValidationException;
17
use Respect\Validation\Exceptions\ValidationException;
18
use Respect\Validation\Validatable;
19
use function is_scalar;
20
21
/**
22
 * @author Alexandre Gomes Gaigalas <[email protected]>
23
 * @author Emmerson Siqueira <[email protected]>
24
 * @author Henrique Moody <[email protected]>
25
 * @author Nick Lombard <[email protected]>
26
 */
27
abstract class AbstractRelated extends AbstractRule
28
{
29
    public $mandatory = true;
30
    public $reference = '';
31
    public $validator;
32
33
    abstract public function hasReference($input): bool;
34
35
    abstract public function getReferenceValue($input);
36
37 26
    public function __construct($reference, Validatable $validator = null, $mandatory = true)
38
    {
39 26
        if (is_scalar($reference)) {
40 26
            $this->setName((string) $reference);
41 26
            if ($validator && !$validator->getName()) {
42 21
                $validator->setName((string) $reference);
43
            }
44
        }
45
46 26
        $this->reference = $reference;
47 26
        $this->validator = $validator;
48 26
        $this->mandatory = $mandatory;
49 26
    }
50
51 19
    public function setName(string $name): Validatable
52
    {
53 19
        parent::setName($name);
54
55 19
        if ($this->validator instanceof Validatable) {
56
            $this->validator->setName($name);
57
        }
58
59 19
        return $this;
60
    }
61
62 13
    public function assert($input): void
63
    {
64 13
        $hasReference = $this->hasReference($input);
65 13
        if ($this->mandatory && !$hasReference) {
66 7
            throw $this->reportError($input, ['hasReference' => false]);
67
        }
68
69
        try {
70 13
            $this->decision('assert', $hasReference, $input);
71 11
        } catch (ValidationException $validationException) {
72
            /** @var NestedValidationException $nestedValidationException */
73 11
            $nestedValidationException = $this->reportError($this->reference, ['hasReference' => true]);
74 11
            $nestedValidationException->addChild($validationException);
75
76 11
            throw $nestedValidationException;
77
        }
78 6
    }
79
80 2
    public function check($input): void
81
    {
82 2
        $hasReference = $this->hasReference($input);
83 2
        if ($this->mandatory && !$hasReference) {
84
            throw $this->reportError($input, ['hasReference' => false]);
85
        }
86
87 2
        $this->decision('check', $hasReference, $input);
88 1
    }
89
90 3
    public function validate($input): bool
91
    {
92 3
        $hasReference = $this->hasReference($input);
93 3
        if ($this->mandatory && !$hasReference) {
94 2
            return false;
95
        }
96
97 2
        return $this->decision('validate', $hasReference, $input);
98
    }
99
100 17
    private function decision(string $type, bool $hasReference, $input)
101
    {
102 17
        return (!$this->mandatory && !$hasReference)
103 17
            || (is_null($this->validator)
104 17
                || $this->validator->$type($this->getReferenceValue($input)));
105
    }
106
}
107