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
Push — master ( 34cbed...314542 )
by Henrique
02:04
created

library/Rules/AbstractComposite.php (1 issue)

Labels
Severity
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 file
9
 * 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
 * @author Wojciech Frącz <[email protected]>
28
 */
29
abstract class AbstractComposite extends AbstractRule
30
{
31
    /**
32
     * @var Validatable[]
33
     */
34
    private $rules = [];
35
36
    /**
37
     * Initializes the rule adding other rules to the stack.
38
     */
39 191
    public function __construct(Validatable ...$rules)
40
    {
41 191
        $this->rules = $rules;
42 191
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47 31
    public function setName(string $name): Validatable
48
    {
49 31
        $parentName = $this->getName();
50 31
        foreach ($this->rules as $rule) {
51 30
            $ruleName = $rule->getName();
52 30
            if ($ruleName && $parentName !== $ruleName) {
53 9
                continue;
54
            }
55
56 28
            $rule->setName($name);
57
        }
58
59 31
        return parent::setName($name);
60
    }
61
62
    /**
63
     * Append a rule into the stack of rules.
64
     *
65
     * @return AbstractComposite
66
     */
67 190
    public function addRule(Validatable $rule): self
68
    {
69 190
        if ($this->shouldHaveNameOverwritten($rule)) {
70 2
            $rule->setName($this->getName());
0 ignored issues
show
It seems like $this->getName() can also be of type null; however, parameter $name of Respect\Validation\Validatable::setName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            $rule->setName(/** @scrutinizer ignore-type */ $this->getName());
Loading history...
71
        }
72
73 190
        $this->rules[] = $rule;
74
75 190
        return $this;
76
    }
77
78
    /**
79
     * Returns all the rules in the stack.
80
     *
81
     * @return Validatable[]
82
     */
83 185
    public function getRules(): array
84
    {
85 185
        return $this->rules;
86
    }
87
88
    /**
89
     * Returns all the exceptions throw when asserting all rules.
90
     *
91
     * @param mixed $input
92
     *
93
     * @return ValidationException[]
94
     */
95 170
    protected function getAllThrownExceptions($input): array
96
    {
97 170
        return array_filter(
98 170
            array_map(
99
                function (Validatable $rule) use ($input): ?ValidationException {
100
                    try {
101 170
                        $rule->assert($input);
102 166
                    } catch (ValidationException $exception) {
103 166
                        $this->updateExceptionTemplate($exception);
104
105 166
                        return $exception;
106
                    }
107
108 12
                    return null;
109 170
                },
110 170
                $this->getRules()
111
            )
112
        );
113
    }
114
115 190
    private function shouldHaveNameOverwritten(Validatable $rule): bool
116
    {
117 190
        return $this->hasName($this) && !$this->hasName($rule);
118
    }
119
120 190
    private function hasName(Validatable $rule): bool
121
    {
122 190
        return $rule->getName() !== null;
123
    }
124
125 166
    private function updateExceptionTemplate(ValidationException $exception): void
126
    {
127 166
        if ($this->template === null || $exception->hasCustomTemplate()) {
128 163
            return;
129
        }
130
131 4
        $exception->updateTemplate($this->template);
132
133 4
        if (!$exception instanceof NestedValidationException) {
134 1
            return;
135
        }
136
137 3
        foreach ($exception->getChildren() as $childException) {
138
            $this->updateExceptionTemplate($childException);
139
        }
140 3
    }
141
}
142