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 ( 344b00...1b8447 )
by Henrique
03:34
created

Not::getNegatedRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 array_shift;
19
use function count;
20
use function current;
21
22
/**
23
 * @author Alexandre Gomes Gaigalas <[email protected]>
24
 * @author Caio César Tavares <[email protected]>
25
 * @author Henrique Moody <[email protected]>
26
 */
27
final class Not extends AbstractRule
28
{
29
    /**
30
     * @var Validatable
31
     */
32
    private $rule;
33
34 157
    public function __construct(Validatable $rule)
35
    {
36 157
        $this->rule = $this->extractNegatedRule($rule);
37 157
    }
38
39 6
    public function getNegatedRule(): Validatable
40
    {
41 6
        return $this->rule;
42
    }
43
44 6
    public function setName(string $name): Validatable
45
    {
46 6
        $this->rule->setName($name);
47
48 6
        return parent::setName($name);
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54 152
    public function validate($input): bool
55
    {
56 152
        return $this->rule->validate($input) === false;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 151
    public function assert($input): void
63
    {
64 151
        if ($this->validate($input)) {
65 3
            return;
66
        }
67
68 149
        $rule = $this->rule;
69 149
        if ($rule instanceof AllOf) {
70 5
            $rule = $this->absorbAllOf($rule, $input);
71
        }
72
73 149
        $exception = $rule->reportError($input);
74 149
        $exception->updateMode(ValidationException::MODE_NEGATIVE);
75
76 149
        throw $exception;
77
    }
78
79
    /**
80
     * @param mixed $input
81
     */
82 5
    private function absorbAllOf(AllOf $rule, $input): Validatable
83
    {
84 5
        $rules = $rule->getRules();
85 5
        while (($current = array_shift($rules))) {
86 5
            $rule = $current;
87 5
            if (!$rule instanceof AllOf) {
88 5
                continue;
89
            }
90
91 2
            if (!$rule->validate($input)) {
92
                continue;
93
            }
94
95 2
            $rules = $rule->getRules();
96
        }
97
98 5
        return $rule;
99
    }
100
101 157
    private function extractNegatedRule(Validatable $rule): Validatable
102
    {
103 157
        if ($rule instanceof self && $rule->getNegatedRule() instanceof self) {
104 2
            return $this->extractNegatedRule($rule->getNegatedRule()->getNegatedRule());
105
        }
106
107 157
        if (!$rule instanceof AllOf) {
108 151
            return $rule;
109
        }
110
111 153
        $rules = $rule->getRules();
112 153
        if (count($rules) === 1) {
113 150
            return $this->extractNegatedRule(current($rules));
114
        }
115
116 7
        return $rule;
117
    }
118
}
119