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 — 2.0 ( e6a123 )
by Henrique
05:00
created

AbstractComposite::validateRules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 12
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
namespace Respect\Validation\Rules;
13
14
use Respect\Validation\Exceptions\ValidationException;
15
use Respect\Validation\Validatable;
16
use Respect\Validation\Validator;
17
18
abstract class AbstractComposite extends AbstractRule
19
{
20
    protected $rules = [];
21
22
    public function __construct(...$rule)
23
    {
24
        $this->addRules($rule);
25
    }
26
27
    public function setName($name)
28
    {
29
        $parentName = $this->getName();
30
        foreach ($this->rules as $rule) {
31
            $ruleName = $rule->getName();
32
            if ($ruleName && $parentName !== $ruleName) {
33
                continue;
34
            }
35
36
            $rule->setName($name);
37
        }
38
39
        return parent::setName($name);
40
    }
41
42
    public function addRule($validator, $arguments = [])
43
    {
44
        if (!$validator instanceof Validatable) {
45
            $this->appendRule(Validator::buildRule($validator, $arguments));
46
        } else {
47
            $this->appendRule($validator);
48
        }
49
50
        return $this;
51
    }
52
53
    public function removeRules()
54
    {
55
        $this->rules = [];
56
    }
57
58
    public function addRules(array $validators)
59
    {
60
        foreach ($validators as $key => $spec) {
61
            if ($spec instanceof Validatable) {
62
                $this->appendRule($spec);
63
            } elseif (is_numeric($key) && is_array($spec)) {
64
                $this->addRules($spec);
65
            } elseif (is_array($spec)) {
66
                $this->addRule($key, $spec);
67
            } else {
68
                $this->addRule($spec);
69
            }
70
        }
71
72
        return $this;
73
    }
74
75
    public function getRules()
76
    {
77
        return $this->rules;
78
    }
79
80
    public function hasRule($validator)
81
    {
82
        if (empty($this->rules)) {
83
            return false;
84
        }
85
86
        if ($validator instanceof Validatable) {
87
            return isset($this->rules[spl_object_hash($validator)]);
88
        }
89
90
        if (is_string($validator)) {
91
            foreach ($this->rules as $rule) {
92
                if (get_class($rule) == __NAMESPACE__.'\\'.$validator) {
93
                    return true;
94
                }
95
            }
96
        }
97
98
        return false;
99
    }
100
101
    protected function appendRule(Validatable $validator)
102
    {
103
        if (!$validator->getName() && $this->getName()) {
104
            $validator->setName($this->getName());
105
        }
106
107
        $this->rules[spl_object_hash($validator)] = $validator;
108
    }
109
110
    protected function validateRules($input)
111
    {
112
        $validators = $this->getRules();
113
        $exceptions = [];
114
        foreach ($validators as $v) {
115
            try {
116
                $v->assert($input);
117
            } catch (ValidationException $e) {
118
                $exceptions[] = $e;
119
            }
120
        }
121
122
        return $exceptions;
123
    }
124
}
125