Passed
Pull Request — master (#222)
by Alexander
04:33 queued 02:13
created

SimpleRuleHandlerContainer::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 5
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 10
c 1
b 1
f 0
cc 3
nc 3
nop 1
crap 3.0416
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