Passed
Pull Request — master (#222)
by Rustam
02:25
created

SimpleRuleHandlerContainer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 3
eloc 7
dl 0
loc 14
ccs 5
cts 6
cp 0.8333
rs 10
c 2
b 2
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\Exception\RuleHandlerNotFoundException;
9
use Yiisoft\Validator\Rule\RuleHandlerInterface;
10
11
final class SimpleRuleHandlerContainer implements RuleHandlerResolverInterface
12
{
13
    private array $instances = [];
14
15 27
    public function resolve(string $className): RuleHandlerInterface
16
    {
17 27
        if (!array_key_exists($className, $this->instances)) {
18 27
            if (!is_subclass_of($className, RuleHandlerInterface::class)) {
19
                throw new RuleHandlerInterfaceNotImplementedException($className);
20
            }
21 27
            return $this->instances[$className] = new $className();
22
        }
23
24 6
        return $this->instances[$className];
25
    }
26
}
27