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 (#899)
by Henrique
24:46 queued 22:26
created

Factory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
dl 0
loc 103
ccs 39
cts 42
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A exception() 0 20 4
A rule() 0 17 4
A getDefaultInstance() 0 7 2
A setDefaultInstance() 0 3 1
A getRulePrefixes() 0 3 1
A filterRulePrefix() 0 6 1
A appendRulePrefix() 0 3 1
A prependRulePrefix() 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;
15
16
use function Respect\Stringifier\stringify;
0 ignored issues
show
introduced by
The function Respect\Stringifier\stringify was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
use ReflectionClass;
18
use ReflectionObject;
19
use Respect\Validation\Exceptions\ComponentException;
20
use Respect\Validation\Exceptions\ValidationException;
21
22
class Factory
23
{
24
    /**
25
     * Default instance of the Factory.
26
     *
27
     * @var Factory
28
     */
29
    protected static $defaultInstance;
30
31
    /**
32
     * @var string[]
33
     */
34
    protected $rulePrefixes = ['Respect\\Validation\\Rules\\'];
35
36
    /**
37
     * Define the default instance of the Factory.
38
     *
39
     * @param Factory $defaultInstance
40
     */
41
    public static function setDefaultInstance(self $defaultInstance): void
42
    {
43
        self::$defaultInstance = $defaultInstance;
44
    }
45
46
    /**
47
     * Returns the default instance of the Factory.
48
     *
49
     * @return Factory
50
     */
51 293
    public static function getDefaultInstance(): self
52
    {
53 293
        if (!self::$defaultInstance instanceof self) {
54 289
            self::$defaultInstance = new static();
55
        }
56
57 293
        return self::$defaultInstance;
58
    }
59
60
    /**
61
     * @return array
62
     */
63 300
    public function getRulePrefixes(): array
64
    {
65 300
        return $this->rulePrefixes;
66
    }
67
68 5
    private function filterRulePrefix(string $rulePrefix): string
69
    {
70 5
        $namespaceSeparator = '\\';
71 5
        $rulePrefix = rtrim($rulePrefix, $namespaceSeparator);
72
73 5
        return $rulePrefix.$namespaceSeparator;
74
    }
75
76 3
    public function appendRulePrefix(string $rulePrefix): void
77
    {
78 3
        array_push($this->rulePrefixes, $this->filterRulePrefix($rulePrefix));
79 3
    }
80
81 2
    public function prependRulePrefix(string $rulePrefix): void
82
    {
83 2
        array_unshift($this->rulePrefixes, $this->filterRulePrefix($rulePrefix));
84 2
    }
85
86 295
    public function rule(string $ruleName, array $arguments = []): Validatable
87
    {
88 295
        foreach ($this->getRulePrefixes() as $prefix) {
89 295
            $className = $prefix.ucfirst($ruleName);
90 295
            if (!class_exists($className)) {
91 3
                continue;
92
            }
93
94 293
            $reflection = new ReflectionClass($className);
95 293
            if (!$reflection->isSubclassOf(Validatable::class)) {
96 1
                throw new ComponentException(sprintf('"%s" is not a valid respect rule', $className));
97
            }
98
99 292
            return $reflection->newInstanceArgs($arguments);
100
        }
101
102 2
        throw new ComponentException(sprintf('"%s" is not a valid rule name', $ruleName));
103
    }
104
105 220
    public function exception(Validatable $validatable, $input, array $extraParams = []): ValidationException
106
    {
107 220
        $reflection = new ReflectionObject($validatable);
108
109 220
        $exceptionName = str_replace('\\Rules\\', '\\Exceptions\\', $reflection->getName()).'Exception';
110 220
        $exception = new $exceptionName();
111
112 220
        $params = ['input' => $input] + $extraParams;
113 220
        foreach ($reflection->getProperties() as $property) {
114 220
            $property->setAccessible(true);
115 220
            $params += [$property->getName() => $property->getValue($validatable)];
116
        }
117
118 220
        $name = $validatable->getName() ?: stringify($input);
119 220
        $exception->configure($name, $params);
120 220
        if (isset($params['template'])) {
121 4
            $exception->setTemplate($params['template']);
122
        }
123
124 220
        return $exception;
125
    }
126
}
127