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
Push — 2.0 ( e6a123...4184ab )
by Henrique
03:41
created

Factory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
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 self
35
     */
36
    private static $defaultInstance;
37
38
    /**
39
     * Initializes the rule with the defined namespaces.
40
     *
41
     * If the default namespace is not in the array, it will be add to the end
42
     * of the array.
43
     *
44
     * @param array $namespaces
45
     */
46 11
    public function __construct(array $namespaces = [])
47
    {
48 11
        if (!in_array(__NAMESPACE__, $namespaces)) {
49 10
            $namespaces[] = __NAMESPACE__;
50
        }
51
52 11
        $this->namespaces = $namespaces;
53 11
    }
54
55
    /**
56
     * Returns a list of namespaces.
57
     *
58
     * @return array
59
     */
60 9
    public function getNamespaces(): array
61
    {
62 9
        return $this->namespaces;
63
    }
64
65
    /**
66
     * Creates a rule based on its name with the defined arguments.
67
     *
68
     * @param string $ruleName
69
     * @param array  $arguments
70
     *
71
     * @throws ComponentException When the rule cannot be created
72
     *
73
     * @return Rule
74
     */
75 6
    public function rule(string $ruleName, array $arguments = []): Rule
76
    {
77 6
        foreach ($this->getNamespaces() as $namespace) {
78 6
            $className = rtrim($namespace, '\\').'\\Rules\\'.ucfirst($ruleName);
79 6
            if (!class_exists($className)) {
80 1
                continue;
81
            }
82
83 5
            $reflection = new ReflectionClass($className);
84 5
            if (!$reflection->isSubclassOf(Rule::class)) {
85 1
                throw new InvalidRuleException(sprintf('"%s" is not a valid rule', $className));
86
            }
87
88 4
            if (!$reflection->isInstantiable()) {
89 1
                throw new InvalidRuleException(sprintf('"%s" is not instantiable', $className));
90
            }
91
92 3
            return $reflection->newInstanceArgs($arguments);
93
        }
94
95 1
        throw new RuleNotFoundException(sprintf('Could not find "%s" rule', $ruleName));
96
    }
97
98
    /**
99
     * Defines the default instance of the factory.
100
     *
101
     * @param Factory $factory
102
     */
103 1
    public static function setDefaultInstance(self $factory)
104
    {
105 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...
106 1
    }
107
108
    /**
109
     * Returns the default instance of the factory.
110
     *
111
     * @return self
112
     */
113 2
    public static function getDefaultInstance(): self
114
    {
115 2
        if (!self::$defaultInstance instanceof self) {
116 1
            self::$defaultInstance = new self();
117
        }
118
119 2
        return self::$defaultInstance;
120
    }
121
}
122