Passed
Pull Request — master (#320)
by Dmitriy
12:15 queued 09:25
created

SimpleRuleHandlerContainer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 21
ccs 9
cts 9
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Validator\Exception\RuleHandlerInterfaceNotImplementedException;
8
use Yiisoft\Validator\Exception\RuleHandlerNotFoundException;
9
10
use function array_key_exists;
11
12
final class SimpleRuleHandlerContainer implements RuleHandlerResolverInterface
13
{
14
    private array $instances = [];
15
16 115
    public function resolve(string $className): RuleHandlerInterface
17
    {
18 115
        if (!class_exists($className)) {
19 1
            throw new RuleHandlerNotFoundException($className);
20
        }
21
22 114
        if (array_key_exists($className, $this->instances)) {
23 35
            return $this->instances[$className];
24
        }
25
26 101
        $classInstance = new $className();
27
28 101
        if (!$classInstance instanceof RuleHandlerInterface) {
29 1
            throw new RuleHandlerInterfaceNotImplementedException($className);
30
        }
31
32 100
        return $this->instances[$className] = $classInstance;
33
    }
34
}
35