Passed
Pull Request — master (#222)
by Alexander
04:31 queued 02:12
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\Rule\RuleHandlerInterface;
9
10
final class SimpleRuleHandlerContainer implements RuleHandlerResolverInterface
11
{
12
    private array $instances = [];
13
14 27
    public function resolve(string $className): RuleHandlerInterface
15
    {
16 27
        if (!array_key_exists($className, $this->instances)) {
17 27
            if (!is_subclass_of($className, RuleHandlerInterface::class)) {
18
                throw new RuleHandlerInterfaceNotImplementedException($className);
19
            }
20 27
            return $this->instances[$className] = new $className();
21
        }
22
23 6
        return $this->instances[$className];
24
    }
25
}
26