SimpleCollator::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Budgegeria\IntlSort\ComparatorFactory;
6
7
use Budgegeria\IntlSort\Collator\Collator;
8
use Budgegeria\IntlSort\Comparator\CollatorConstructor;
9
use Budgegeria\IntlSort\Comparator\Comparable;
10
use Budgegeria\IntlSort\Exception\IntlSortException;
11
12
use function class_exists;
13
use function class_implements;
14
use function in_array;
15
16
class SimpleCollator implements Factory
17
{
18
    /** @var class-string<CollatorConstructor> $classname */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<CollatorConstructor> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<CollatorConstructor>.
Loading history...
19
    private $classname;
20
21
    /** @psalm-param class-string<CollatorConstructor> $classname */
22 3
    public function __construct(string $classname)
23
    {
24 3
        if (! class_exists($classname)) {
25 1
            throw IntlSortException::classDoesNotExist($classname);
26
        }
27
28
        /** @var array<string> $interfaces */
29 2
        $interfaces = class_implements($classname);
30
31 2
        if (! in_array(CollatorConstructor::class, $interfaces, true)) {
32 1
            throw IntlSortException::doesNotImplementComparable($classname);
33
        }
34
35 1
        $this->classname = $classname;
36
    }
37
38 1
    public function create(Collator $collator): Comparable
39
    {
40 1
        return new $this->classname($collator);
41
    }
42
}
43