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 (#1082)
by Henrique
04:13
created

AbstractComposite::getAllThrownExceptions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 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\NestedValidationException;
17
use Respect\Validation\Exceptions\ValidationException;
18
use Respect\Validation\Validatable;
19
use function array_filter;
20
use function array_map;
21
22
/**
23
 * Abstract class for rules that are composed by other rules.
24
 *
25
 * @author Alexandre Gomes Gaigalas <[email protected]>
26
 * @author Henrique Moody <[email protected]>
27
 */
28
abstract class AbstractComposite extends AbstractRule
29
{
30
    /**
31
     * @var Validatable[]
32
     */
33
    private $rules = [];
34
35
    /**
36
     * Initializes the rule adding other rules to the stack.
37
     *
38
     * @param Validatable ...$rules
39
     */
40 224
    public function __construct(Validatable ...$rules)
41
    {
42 224
        $this->rules = $rules;
43 224
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 34
    public function setName(string $name): Validatable
49
    {
50 34
        $parentName = $this->getName();
51 34
        foreach ($this->rules as $rule) {
52 33
            $ruleName = $rule->getName();
53 33
            if ($ruleName && $parentName !== $ruleName) {
54 9
                continue;
55
            }
56
57 31
            $rule->setName($name);
58
        }
59
60 34
        return parent::setName($name);
61
    }
62
63
    /**
64
     * Append a rule into the stack of rules.
65
     *
66
     * @param Validatable $rule
67
     *
68
     * @return AbstractComposite
69
     */
70 223
    public function addRule(Validatable $rule): self
71
    {
72 223
        if ($this->shouldHaveNameOverwritten($rule)) {
73 2
            $rule->setName($this->getName());
74
        }
75
76 223
        $this->rules[] = $rule;
77
78 223
        return $this;
79
    }
80
81
    /**
82
     * Returns all the rules in the stack.
83
     *
84
     * @return Validatable[]
85
     */
86 218
    public function getRules(): array
87
    {
88 218
        return $this->rules;
89
    }
90
91
    /**
92
     * Returns all the exceptions throw when asserting all rules.
93
     *
94
     * @param mixed $input
95
     *
96
     * @return ValidationException[]
97
     */
98 165
    protected function getAllThrownExceptions($input): array
99
    {
100 165
        return array_filter(
101 165
            array_map(
102
                function (Validatable $rule) use ($input): ?ValidationException {
103
                    try {
104 165
                        $rule->assert($input);
105 144
                    } catch (ValidationException $exception) {
106 144
                        $this->updateExceptionTemplate($exception);
107
108 144
                        return $exception;
109
                    }
110
111 29
                    return null;
112 165
                },
113 165
                $this->getRules()
114
            )
115
        );
116
    }
117
118 223
    private function shouldHaveNameOverwritten(Validatable $rule): bool
119
    {
120 223
        return $this->hasName($this) && !$this->hasName($rule);
121
    }
122
123 223
    private function hasName(Validatable $rule): bool
124
    {
125 223
        return null !== $rule->getName();
126
    }
127
128 144
    private function updateExceptionTemplate(ValidationException $exception): void
129
    {
130 144
        if (null === $this->template || $exception->hasCustomTemplate()) {
131 141
            return;
132
        }
133
134 4
        $exception->updateTemplate($this->template);
135
136 4
        if (!$exception instanceof NestedValidationException) {
137 1
            return;
138
        }
139
140 3
        foreach ($exception->getChildren() as $childException) {
141
            $this->updateExceptionTemplate($childException);
142
        }
143 3
    }
144
}
145