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:46
created

Factory::chooseTemplate()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 9.2
cc 4
eloc 9
nc 6
nop 3
crap 20
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\SimpleAnnotationReader;
15
use ReflectionClass;
16
use Respect\Validation\Annotations\Template;
17
use Respect\Validation\Annotations\Templates;
18
use Respect\Validation\Exceptions\ComponentException;
19
use Respect\Validation\Exceptions\InvalidRuleException;
20
use Respect\Validation\Exceptions\RuleNotFoundException;
21 11
use Respect\Validation\Exceptions\ValidationException;
22
use Respect\Validation\Message\Formatter;
23 11
24
/**
25
 * Factory to create rules.
26 5
 *
27
 * @author Henrique Moody <[email protected]>
28 5
 *
29 5
 * @since 0.8.0
30
 */
31 5
final class Factory
32
{
33
    /**
34 3
     * @var string[]
35
     */
36 3
    private $namespaces;
37 3
38
    /**
39 2
     * @var ReflectionClass[]
40
     */
41 2
    private $reflections;
42 2
43
    /**
44 6
     * @var self
45
     */
46 6
    private static $defaultInstance;
47
48
    /**
49
     * Initializes the rule with the defined namespaces.
50 6
     *
51 6
     * If the default namespace is not in the array, it will be add to the end
52 6
     * of the array.
53 3
     *
54
     * @param array $namespaces
55
     */
56 4
    public function __construct(array $namespaces = [])
57 4
    {
58 1
        if (!in_array(__NAMESPACE__, $namespaces)) {
59
            $namespaces[] = __NAMESPACE__;
60
        }
61 3
62
        $this->namespaces = $namespaces;
63
    }
64 2
65
    /**
66
     * Defines the default instance of the factory.
67
     *
68
     * @param Factory $factory
69
     */
70
    public static function setDefaultInstance(self $factory)
71
    {
72
        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...
73
    }
74
75
    /**
76
     * Returns the default instance of the factory.
77
     *
78
     * @return self
79
     */
80
    public static function getDefaultInstance(): self
81
    {
82
        if (!self::$defaultInstance instanceof self) {
83
            self::$defaultInstance = new self();
84
        }
85
86
        return self::$defaultInstance;
87
    }
88
89
    /**
90
     * Returns a list of namespaces.
91
     *
92
     * @return array
93
     */
94
    public function getNamespaces(): array
95
    {
96
        return $this->namespaces;
97
    }
98
99
    /**
100
     * Creates a rule based on its name with the defined arguments.
101
     *
102
     * @param string $ruleName
103
     * @param array  $arguments
104
     *
105
     * @throws ComponentException When the rule cannot be created
106
     *
107
     * @return Rule
108
     */
109
    public function rule(string $ruleName, array $arguments = []): Rule
110
    {
111
        foreach ($this->getNamespaces() as $namespace) {
112
            $className = rtrim($namespace, '\\').'\\Rules\\'.ucfirst($ruleName);
113
            if (!class_exists($className)) {
114
                continue;
115
            }
116
117
            $reflection = $this->getReflection($className);
118
119
            if (!$reflection->isSubclassOf(Rule::class)) {
120
                throw new InvalidRuleException(sprintf('"%s" is not a valid rule', $className));
121
            }
122
123
            if (!$reflection->isInstantiable()) {
124
                throw new InvalidRuleException(sprintf('"%s" is not instantiable', $className));
125
            }
126
127
            return $reflection->newInstanceArgs($arguments);
128
        }
129
130
        throw new RuleNotFoundException(sprintf('Could not find "%s" rule', $ruleName));
131
    }
132
133
    private function getMessage(ReflectionClass $reflection, Result $result): string
134
    {
135
        $reader = new SimpleAnnotationReader();
136
        $reader->addNamespace('Respect\\Validation\\Annotations');
137
138
        $templates = $reader->getClassAnnotation($reflection, Templates::class);
139
140
        $properties = $result->getProperties();
141
        $template = $this->chooseTemplate(
142
            $templates,
0 ignored issues
show
Documentation introduced by
$templates is of type object|null, but the function expects a object<Respect\Validation\Annotations\Templates>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
143
            $result->isInverted(),
144
            $properties['templateId'] ?? 'standard'
145
        );
146
147
        $formatter = new Formatter(2, 3);
148
149
        return $formatter->create($result->getInput(), $result->getProperties(), $template->message);
150
    }
151
152
    public function exception(Result $result): ValidationException
153
    {
154
        $exceptionName = str_replace('Rule', 'Exception', get_class($result->getRule())).'Exception';
155
156
        $reflection = $this->getReflection($exceptionName);
157
        $message = $this->getMessage($reflection, $result);
158
159
        return $reflection->newInstance($message);
160
    }
161
162
    private function chooseTemplate(Templates $templates, bool $isInverted, string $templateId): Template
163
    {
164
        $templatesList = $templates->regular;
165
        if ($isInverted) {
166
            $templatesList = $templates->inverted;
167
        }
168
169
        foreach ($templatesList as $template) {
170
            if ($template->id != $templateId) {
171
                continue;
172
            }
173
174
            return $template;
175
        }
176
177
        return current($templateLists);
0 ignored issues
show
Bug introduced by
The variable $templateLists does not exist. Did you mean $templatesList?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
178
    }
179
180
    /**
181
     * Creates a ReflectionClass object based on a class name.
182
     *
183
     * This method always return the same object for a given class name in order
184
     * to improve performance.
185
     *
186
     * @param string $className
187
     *
188
     * @return ReflectionClass
189
     */
190
    private function getReflection(string $className): ReflectionClass
191
    {
192
        if (!isset($this->reflections[$className])) {
193
            $this->reflections[$className] = new ReflectionClass($className);
194
        }
195
196
        return $this->reflections[$className];
197
    }
198
}
199