Passed
Pull Request — master (#237)
by Rustam
13:25
created

SimpleRuleHandlerResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 7
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

1 Method

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