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 (#678)
by Henrique
04:28
created

Factory::getReflection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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
use Respect\Validation\Exceptions\InvalidRuleException;
17
use Respect\Validation\Exceptions\RuleNotFoundException;
18
19
/**
20
 * Factory to create rules.
21
 *
22
 * @author Henrique Moody <[email protected]>
23
 *
24
 * @since 0.8.0
25
 */
26
final class Factory
27
{
28
    /**
29
     * @var string[]
30
     */
31
    private $namespaces;
32
33
    /**
34
     * @var ReflectionClass[]
35
     */
36
    private $reflections;
37
38
    /**
39
     * @var self
40
     */
41
    private static $defaultInstance;
42
43
    /**
44
     * Initializes the rule with the defined namespaces.
45
     *
46
     * If the default namespace is not in the array, it will be add to the end
47
     * of the array.
48
     *
49
     * @param array $namespaces
50
     */
51 11
    public function __construct(array $namespaces = [])
52
    {
53 11
        if (!in_array(__NAMESPACE__, $namespaces)) {
54 9
            $namespaces[] = __NAMESPACE__;
55
        }
56
57 11
        $this->namespaces = $namespaces;
58 11
    }
59
60
    /**
61
     * Defines the default instance of the factory.
62
     *
63
     * @param Factory $factory
64
     */
65 1
    public static function setDefaultInstance(self $factory)
66
    {
67 1
        self::$defaultInstance = $factory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $factory of type object<self> is incompatible with the declared type object<Respect\Validation\Factory> of property $defaultInstance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68 1
    }
69
70
    /**
71
     * Returns the default instance of the factory.
72
     *
73
     * @return self
74
     */
75 2
    public static function getDefaultInstance(): self
76
    {
77 2
        if (!self::$defaultInstance instanceof self) {
78 1
            self::$defaultInstance = new self();
79
        }
80
81 2
        return self::$defaultInstance;
82
    }
83
84
    /**
85
     * Returns a list of namespaces.
86
     *
87
     * @return array
88
     */
89 9
    public function getNamespaces(): array
90
    {
91 9
        return $this->namespaces;
92
    }
93
94
    /**
95
     * Creates a rule based on its name with the defined arguments.
96
     *
97
     * @param string $ruleName
98
     * @param array  $arguments
99
     *
100
     * @throws ComponentException When the rule cannot be created
101
     *
102
     * @return Rule
103
     */
104 6
    public function rule(string $ruleName, array $arguments = []): Rule
105
    {
106 6
        foreach ($this->getNamespaces() as $namespace) {
107 6
            $className = rtrim($namespace, '\\').'\\Rules\\'.ucfirst($ruleName);
108 6
            if (!class_exists($className)) {
109 1
                continue;
110
            }
111
112 5
            $reflection = $this->getReflection($className);
113
114 5
            if (!$reflection->isSubclassOf(Rule::class)) {
115 1
                throw new InvalidRuleException(sprintf('"%s" is not a valid rule', $className));
116
            }
117
118 4
            if (!$reflection->isInstantiable()) {
119 1
                throw new InvalidRuleException(sprintf('"%s" is not instantiable', $className));
120
            }
121
122 3
            return $reflection->newInstanceArgs($arguments);
123
        }
124
125 1
        throw new RuleNotFoundException(sprintf('Could not find "%s" rule', $ruleName));
126
    }
127
128
    /**
129
     * Creates a ReflectionClass object based on a class name.
130
     *
131
     * This method always return the same object for a given class name in order
132
     * to improve performance.
133
     *
134
     * @param string $className
135
     *
136
     * @return ReflectionClass
137
     */
138 5
    private function getReflection(string $className): ReflectionClass
139
    {
140 5
        if (!isset($this->reflections[$className])) {
141 5
            $this->reflections[$className] = new ReflectionClass($className);
142
        }
143
144 5
        return $this->reflections[$className];
145
    }
146
}
147