Passed
Push — main ( 659659...7cf3e5 )
by Jeroen
09:29
created

ClassReflectorRouteLoader::addDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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
    public function __construct(private ClassReflectorFactory $reflectorFactory)
21
    {
22
    }
23
24
    public function getDirectories() : array
25
    {
26
        return $this->directories;
27
    }
28
29
    public function addDirectory(string ...$directories) : RouteLoaderInterface
30
    {
31
        $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
        return $this;
34
    }
35
36
    public function getRoutes() : Generator
37
    {
38
        $reflector = $this->reflectorFactory->create()->addDirectory(...$this->directories);
39
40
        foreach ($reflector->reflect()->getClasses() as $class) {
41
            foreach ($class->getMethods() as $method) {
42
                foreach ($method->getAttributes(Route::class) as $attribute) {
43
                    /** @var Route $route */
44
                    $route = $attribute->newInstance();
45
46
                    yield new LoadedRoute($class->getName(), $method->getName(), $route);
47
                }
48
            }
49
        }
50
    }
51
}
52