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 (#857)
by
unknown
03:04
created

Factory::appendRulePrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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
namespace Respect\Validation;
13
14
use ReflectionClass;
15
use Respect\Validation\Exceptions\ComponentException;
16
17
class Factory
18
{
19
    /** @var string[] */
20
    protected $rulePrefixes = ['Respect\\Validation\\Rules\\'];
21
22
    /**
23
     * @return string[]
24
     */
25 11
    public function getRulePrefixes(): array
26
    {
27 11
        return $this->rulePrefixes;
28
    }
29
30
    /**
31
     * @param string $rulePrefix
32
     *
33
     * @return string
34
     */
35 5
    private function filterRulePrefix(string $rulePrefix): string
36
    {
37 5
        $namespaceSeparator = '\\';
38 5
        $rulePrefix = rtrim($rulePrefix, $namespaceSeparator);
39
40 5
        return $rulePrefix.$namespaceSeparator;
41
    }
42
43
    /**
44
     * @param string $rulePrefix
45
     *
46
     * @return void
47
     */
48 3
    public function appendRulePrefix(string $rulePrefix)
49
    {
50 3
        array_push($this->rulePrefixes, $this->filterRulePrefix($rulePrefix));
51 3
    }
52
53
    /**
54
     * @param string $rulePrefix
55
     *
56
     * @return void
57
     */
58 2
    public function prependRulePrefix(string $rulePrefix)
59
    {
60 2
        array_unshift($this->rulePrefixes, $this->filterRulePrefix($rulePrefix));
61 2
    }
62
63
    /**
64
     * @param string|Validatable $ruleName
65
     * @param array              $arguments = []
66
     *
67
     * @return Validatable
68
     *
69
     * @throws ComponentException
70
     */
71 6
    public function rule($ruleName, array $arguments = []): Validatable
72
    {
73 6
        if ($ruleName instanceof Validatable) {
74
            return $ruleName;
75
        }
76
77 6
        foreach ($this->getRulePrefixes() as $prefix) {
78 6
            $className = $prefix.ucfirst($ruleName);
79 6
            if (!class_exists($className)) {
80 3
                continue;
81
            }
82
83 4
            $reflection = new ReflectionClass($className);
84 4
            if (!$reflection->isSubclassOf(Validatable::class)) {
85 1
                throw new ComponentException(sprintf('"%s" is not a valid respect rule', $className));
86
            }
87
88
            /** @var Validatable $validator */
89 3
            $validator = $reflection->newInstanceArgs($arguments);
90
91 3
            return $validator;
92
        }
93
94 2
        throw new ComponentException(sprintf('"%s" is not a valid rule name', $ruleName));
95
    }
96
}
97