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
03:31
created

Factory::getDefaultInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
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 11
 *
22
 * @author Henrique Moody <[email protected]>
23 11
 *
24
 * @since 0.8.0
25
 */
26 5
final class Factory
27
{
28 5
    /**
29 5
     * @var string[]
30
     */
31 5
    private $namespaces;
32
33
    /**
34 3
     * @var self
35
     */
36 3
    private static $defaultInstance;
37 3
38
    /**
39 2
     * Initializes the rule with the defined namespaces.
40
     *
41 2
     * If the default namespace is not in the array, it will be add to the end
42 2
     * of the array.
43
     *
44 6
     * @param array $namespaces
45
     */
46 6
    public function __construct(array $namespaces = [])
47
    {
48
        if (!in_array(__NAMESPACE__, $namespaces)) {
49
            $namespaces[] = __NAMESPACE__;
50 6
        }
51 6
52 6
        $this->namespaces = $namespaces;
53 3
    }
54
55
    /**
56 4
     * Returns a list of namespaces.
57 4
     *
58 1
     * @return array
59
     */
60
    public function getNamespaces(): array
61 3
    {
62
        return $this->namespaces;
63
    }
64 2
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
    public function rule(string $ruleName, array $arguments = []): Rule
76
    {
77
        foreach ($this->getNamespaces() as $namespace) {
78
            $className = rtrim($namespace, '\\').'\\Rules\\'.ucfirst($ruleName);
79
            if (!class_exists($className)) {
80
                continue;
81
            }
82
83
            $reflection = new ReflectionClass($className);
84
            if (!$reflection->isSubclassOf(Rule::class)) {
85
                throw new InvalidRuleException(sprintf('"%s" is not a valid rule', $className));
86
            }
87
88
            if (!$reflection->isInstantiable()) {
89
                throw new InvalidRuleException(sprintf('"%s" is not instantiable', $className));
90
            }
91
92
            return $reflection->newInstanceArgs($arguments);
93
        }
94
95
        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
    public static function setDefaultInstance(self $factory)
104
    {
105
        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
    }
107
108
    /**
109
     * Returns the default instance of the factory.
110
     *
111
     * @return self
112
     */
113
    public static function getDefaultInstance(): self
114
    {
115
        if (!self::$defaultInstance instanceof self) {
116
            self::$defaultInstance = new self();
117
        }
118
119
        return self::$defaultInstance;
120
    }
121
}
122