Passed
Push — main ( d2fd20...7a2f93 )
by Jeroen
02:09
created

ClassReflectorRouteLoader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 45
ccs 18
cts 20
cp 0.9
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDirectories() 0 3 1
A __construct() 0 2 1
A addDirectory() 0 5 1
A getRoutes() 0 22 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jerowork\RouteAttributeProvider\RouteLoader\ClassReflector;
6
7
use Generator;
8
use Jerowork\FileClassReflector\ClassReflectorFactory;
9
use Jerowork\RouteAttributeProvider\Api\Route;
10
use Jerowork\RouteAttributeProvider\RouteLoader\LoadedRoute;
11
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface;
12
13
final class ClassReflectorRouteLoader implements RouteLoaderInterface
14
{
15
    /**
16
     * @var string[]
17
     */
18
    private array $directories = [];
19
20 4
    public function __construct(private ClassReflectorFactory $reflectorFactory)
21
    {
22 4
    }
23
24
    public function getDirectories() : array
25
    {
26
        return $this->directories;
27
    }
28
29 2
    public function addDirectory(string ...$directories) : RouteLoaderInterface
30
    {
31 2
        $this->directories = [...$this->directories, ...$directories];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->directories, $directories) of type array<integer,array|array<integer,string>> is incompatible with the declared type string[] of property $directories.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
33 2
        return $this;
34
    }
35
36 2
    public function getRoutes() : Generator
37
    {
38 2
        $reflector = $this->reflectorFactory->create()->addDirectory(...$this->directories);
39 2
        foreach ($reflector->reflect()->getClasses() as $class) {
40 2
            $hasClassAttribute = false;
41
42 2
            foreach ($class->getAttributes(Route::class) as $attribute) {
43 2
                $hasClassAttribute = true;
44
45
                /** @var Route $route */
46 2
                $route = $attribute->newInstance();
47
48 2
                yield new LoadedRoute($class->getName(), '__invoke', $route);
49
            }
50
51 2
            if (!$hasClassAttribute) {
52 2
                foreach ($class->getMethods() as $method) {
53 2
                    foreach ($method->getAttributes(Route::class) as $attribute) {
54
                        /** @var Route $route */
55 2
                        $route = $attribute->newInstance();
56
57 2
                        yield new LoadedRoute($class->getName(), $method->getName(), $route);
58
                    }
59
                }
60
            }
61
        }
62
    }
63
}
64