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 — 1.1 ( a00928...120d92 )
by Henrique
04:43 queued 01:40
created

AbstractComposite::addRules()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 10.5

Importance

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