Passed
Pull Request — master (#17)
by Aleksei
02:31
created

ReaderFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 2
b 0
f 0
dl 0
loc 32
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Annotated;
6
7
use Doctrine\Common\Annotations\Reader as DoctrineReader;
8
use Spiral\Attributes\AnnotationReader;
9
use Spiral\Attributes\AttributeReader;
10
use Spiral\Attributes\Composite\SelectiveReader;
11
use Spiral\Attributes\ReaderInterface;
12
13
final class ReaderFactory
14
{
15
    /**
16
     * @param object<ReaderInterface|DoctrineReader>|null $reader
17
     *
18
     * @return ReaderInterface
19
     *
20
     * @psalm-type ReaderType = ReaderInterface | DoctrineAnnotationReader | null
21
     * @psalm-param ReaderType $reader
22
     */
23
    public static function create(object $reader = null): ReaderInterface
24
    {
25
        switch (true) {
26
            case $reader instanceof ReaderInterface:
27
                return $reader;
28
29
            case $reader instanceof DoctrineReader:
30
                return new AnnotationReader($reader);
31
32
            case $reader === null:
33
                return new SelectiveReader([
34
                    new AttributeReader(),
35
                    new AnnotationReader(),
36
                ]);
37
38
            default:
39
                throw new \InvalidArgumentException(
40
                    sprintf(
41
                        'Argument $reader must be an instance of %s or %s, but %s passed.',
42
                        ReaderInterface::class,
43
                        DoctrineReader::class,
44
                        'instance of ' . explode("\0", get_class($reader))[0]
45
                    )
46
                );
47
        }
48
    }
49
}
50