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 (#1056)
by Henrique
03:43
created

AbstractComposite::hasRule()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.2222
cc 6
nc 5
nop 1
crap 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A AbstractComposite::getRules() 0 3 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_filter;
19
use function array_map;
20
21
/**
22
 * Abstract class for rules that are composed by other rules.
23
 *
24
 * @author Alexandre Gomes Gaigalas <[email protected]>
25
 * @author Henrique Moody <[email protected]>
26
 */
27
abstract class AbstractComposite extends AbstractRule
28
{
29
    /**
30
     * @var Validatable[]
31
     */
32
    private $rules = [];
33
34
    /**
35
     * Initializes rule.
36
     *
37
     * @param Validatable ...$rules
38
     */
39 222
    public function __construct(Validatable ...$rules)
40
    {
41 222
        $this->rules = $rules;
42 222
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 33
    public function setName(string $name): Validatable
48
    {
49 33
        $parentName = $this->getName();
50 33
        foreach ($this->rules as $rule) {
51 32
            $ruleName = $rule->getName();
52 32
            if ($ruleName && $parentName !== $ruleName) {
53 9
                continue;
54
            }
55
56 30
            $rule->setName($name);
57
        }
58
59 33
        return parent::setName($name);
60
    }
61
62
    /**
63
     * Append a rule into the stack of rules.
64
     *
65
     * @param Validatable $rule
66
     *
67
     * @return AbstractComposite
68
     */
69 221
    public function addRule(Validatable $rule): self
70
    {
71 221
        if ($this->shouldHaveNameOverwritten($rule)) {
72 2
            $rule->setName($this->getName());
73
        }
74
75 221
        $this->rules[] = $rule;
76
77 221
        return $this;
78
    }
79
80
    /**
81
     * @return Validatable[]
82
     */
83 216
    public function getRules(): array
84
    {
85 216
        return $this->rules;
86
    }
87
88
    /**
89
     * Validate the whole stack of rule.
90
     *
91
     * @param mixed $input
92
     *
93
     * @return ValidationException[]
94
     */
95 161
    protected function validateRules($input): array
96
    {
97 161
        return array_filter(
98 161
            array_map(
99 161
                function (Validatable $rule) use ($input) {
100
                    try {
101 161
                        $rule->assert($input);
102 138
                    } catch (ValidationException $exception) {
103 138
                        return $exception;
104
                    }
105
106 31
                    return null;
107 161
                },
108 161
                $this->getRules()
109
            )
110
        );
111
    }
112
113 221
    private function shouldHaveNameOverwritten(Validatable $rule): bool
114
    {
115 221
        return null !== $this->getName() && null === $rule->getName();
116
    }
117
}
118