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
05:02
created

Factory::rule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
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 Doctrine\Common\Annotations\Reader;
15
use Doctrine\Common\Annotations\SimpleAnnotationReader;
16
use ReflectionClass;
17
use Respect\Validation\Exceptions\ExceptionNotFoundException;
18
use Respect\Validation\Exceptions\InvalidClassException;
19
use Respect\Validation\Exceptions\RuleNotFoundException;
20
use Respect\Validation\Exceptions\ValidationException;
21
use Respect\Validation\Message\Template;
22
use Respect\Validation\Message\Templates;
23
24
/**
25
 * Factory to create rules.
26
 *
27
 * @author Henrique Moody <[email protected]>
28
 *
29
 * @since 0.8.0
30
 */
31
final class Factory
32
{
33
    /**
34
     * @var string[]
35
     */
36
    private $namespaces;
37
38
    /**
39
     * @var ReflectionClass[]
40
     */
41
    private $reflections;
42
43
    /**
44
     * @var Reader
45
     */
46
    private $annotationReader;
47
48
    /**
49
     * @var self
50
     */
51
    private static $defaultInstance;
52
53
    /**
54
     * Initializes the rule with the defined namespaces.
55
     *
56
     * If the default namespace is not in the array, it will be add to the end
57
     * of the array.
58
     *
59
     * @param array $namespaces
60
     */
61 11
    public function __construct(array $namespaces = [])
62
    {
63 11
        if (!in_array(__NAMESPACE__, $namespaces)) {
64 9
            $namespaces[] = __NAMESPACE__;
65
        }
66
67 11
        $this->namespaces = $namespaces;
68 11
        $this->annotationReader = new SimpleAnnotationReader();
69 11
        foreach ($namespaces as $namespace) {
70 11
            $this->annotationReader->addNamespace(rtrim($namespace, '\\').'\\Message');
71
        }
72 11
    }
73
74
    /**
75
     * Defines the default instance of the factory.
76
     *
77
     * @param Factory $factory
78
     */
79 1
    public static function setDefaultInstance(self $factory)
80
    {
81 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...
82 1
    }
83
84
    /**
85
     * Returns the default instance of the factory.
86
     *
87
     * @return self
88
     */
89 2
    public static function getDefaultInstance(): self
90
    {
91 2
        if (!self::$defaultInstance instanceof self) {
92 1
            self::$defaultInstance = new self();
93
        }
94
95 2
        return self::$defaultInstance;
96
    }
97
98
    /**
99
     * Returns a list of namespaces.
100
     *
101
     * @return array
102
     */
103 9
    public function getNamespaces(): array
104
    {
105 9
        return $this->namespaces;
106
    }
107
108
    /**
109
     * Creates a rule based on its name with the defined arguments.
110
     *
111
     * @param string $ruleName
112
     * @param array  $arguments
113
     *
114
     * @throws InvalidClassException
115
     * @throws RuleNotFoundException
116
     *
117
     * @return Rule
118
     */
119 6
    public function rule(string $ruleName, array $arguments = []): Rule
120
    {
121 6
        return $this->getRuleReflection($ruleName)->newInstanceArgs($arguments);
122
    }
123
124
    public function exception(Message $message): ValidationException
125
    {
126
        $reflection = $this->getExceptionReflection($message->getRuleName());
127
128
        return $reflection->newInstance($message->__toString());
129
    }
130
131
    public function message(Result $result, Template $template = null): Message
132
    {
133
        $ruleName = ltrim(strrchr(get_class($result->getRule()), '\\'), '\\');
134
        $template = $template ?: $this->template($result);
135
136
        return new Message($ruleName, $template->message, $result->getInput(), $result->getProperties());
137
    }
138
139
    public function template(Result $result): Template
140
    {
141
        $reflection = $this->getReflection(get_class($result->getRule()), Rule::class);
142
143
        /* @var Templates $templates */
144
        $templates = $this->annotationReader->getClassAnnotation($reflection, Templates::class)
145
            ?? Templates::getDefault();
146
147
        $templateId = $result->getProperties()[Result::TEMPLATE_KEY] ?? Template::DEFAULT_ID_VALUE;
148
        $templateList = $result->isInverted() ? $templates->inverted : $templates->regular;
149
150
        /* @var Template $template */
151
        foreach ($templateList as $template) {
152
            if ($template->id !== $templateId) {
153
                continue;
154
            }
155
156
            return $template;
157
        }
158
159
        return current($templateList);
160
    }
161
162
    /**
163
     * Returns a reflection of a rule based on its name.
164
     *
165
     *
166
     * @param string $ruleName
167
     *
168
     * @throws RuleNotFoundException
169
     *
170
     * @return ReflectionClass
171
     */
172 6
    private function getRuleReflection(string $ruleName): ReflectionClass
173
    {
174 6
        foreach ($this->getNamespaces() as $namespace) {
175 6
            $className = rtrim($namespace, '\\').'\\Rules\\'.ucfirst($ruleName);
176 6
            if (!class_exists($className)) {
177 1
                continue;
178
            }
179
180 5
            return $this->getReflection($className, Rule::class);
181
        }
182
183 1
        throw new RuleNotFoundException(sprintf('Could not find "%s" rule', $ruleName));
184
    }
185
186
    /**
187
     * Returns a reflection of an exception based on a rule name.
188
     *
189
     *
190
     * @param string $ruleName
191
     *
192
     * @throws ExceptionNotFoundException
193
     *
194
     * @return ReflectionClass
195
     */
196
    private function getExceptionReflection(string $ruleName): ReflectionClass
197
    {
198
        foreach ($this->getNamespaces() as $namespace) {
199
            $className = sprintf('%s\\Exceptions\\%sException', rtrim($namespace, '\\'), $ruleName);
200
            if (!class_exists($className)) {
201
                continue;
202
            }
203
204
            return $this->getReflection($className, ValidationException::class);
205
        }
206
207
        throw new ExceptionNotFoundException(sprintf('Could not find "%s" exception', $ruleName));
208
    }
209
210
    /**
211
     * Creates a ReflectionClass object based on a class name.
212
     *
213
     * This method always return the same object for a given class name in order
214
     * to improve performance. Also checks if the reflection is child of $parentClass
215
     * and if it is instantiable.
216
     *
217
     * @param string $className
218
     * @param string $parentClassName
219
     *
220
     * @throws InvalidClassException When not valid.
221
     *
222
     * @return ReflectionClass
223
     */
224 5
    private function getReflection(string $className, string $parentClassName): ReflectionClass
225
    {
226 5
        if (!isset($this->reflections[$className])) {
227 5
            $this->reflections[$className] = new ReflectionClass($className);
228
229 5
            if (!$this->reflections[$className]->isInstantiable()) {
230 1
                throw new InvalidClassException(sprintf('"%s" is not instantiable', $className));
231
            }
232
        }
233
234 4
        if (!$this->reflections[$className]->isSubclassOf($parentClassName)) {
235 1
            throw new InvalidClassException(sprintf('"%s" is not subclass of "%s"', $className, $parentClassName));
236
        }
237
238 3
        return $this->reflections[$className];
239
    }
240
}
241