RoaveRouterLoader   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 63
ccs 22
cts 24
cp 0.9167
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createRoaveReflectorForFile() 0 3 1
A addDirectory() 0 5 1
A getDirectories() 0 3 1
B getRoutes() 0 26 7
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jerowork\RouteAttributeProvider\RouteLoader\Roave;
6
7
use Generator;
8
use Jerowork\RouteAttributeProvider\Api\Route;
9
use Jerowork\RouteAttributeProvider\RouteLoader\LoadedRoute;
10
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface;
11
use ReflectionClass;
12
use Roave\BetterReflection\BetterReflection;
13
use Roave\BetterReflection\Reflector\DefaultReflector;
14
use Roave\BetterReflection\Reflector\Reflector;
15
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
16
17
final class RoaveRouterLoader implements RouteLoaderInterface
18
{
19
    /**
20
     * @var string[]
21
     */
22
    private array $directories = [];
23
24 4
    public function __construct(
25
        private readonly FileFinder $fileFinder,
26
    ) {
27 4
    }
28
29
    public function getDirectories() : array
30
    {
31
        return $this->directories;
32
    }
33
34 2
    public function addDirectory(string ...$directories) : RouteLoaderInterface
35
    {
36 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...
37
38 2
        return $this;
39
    }
40
41 2
    public function getRoutes() : Generator
42
    {
43 2
        foreach ($this->fileFinder->getFiles(...$this->directories) as $file) {
44
            /** @var non-empty-string $file */
45 2
            $reflector = $this->createRoaveReflectorForFile($file);
46
47 2
            foreach ($reflector->reflectAllClasses() as $reflectorClass) {
48 2
                $hasClassAttribute = false;
49 2
                $class             = new ReflectionClass($reflectorClass->getName());
50
51 2
                foreach ($class->getAttributes(Route::class) as $attribute) {
52 2
                    $hasClassAttribute = true;
53
54
                    /** @var Route $route */
55 2
                    $route = $attribute->newInstance();
56
57 2
                    yield new LoadedRoute($class->getName(), '__invoke', $route);
58
                }
59
60 2
                if (!$hasClassAttribute) {
61 2
                    foreach ($class->getMethods() as $method) {
62 2
                        foreach ($method->getAttributes(Route::class) as $attribute) {
63
                            /** @var Route $route */
64 2
                            $route = $attribute->newInstance();
65
66 2
                            yield new LoadedRoute($class->getName(), $method->getName(), $route);
67
                        }
68
                    }
69
                }
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param non-empty-string $file
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
76
     */
77 2
    public function createRoaveReflectorForFile(string $file) : Reflector
78
    {
79 2
        return new DefaultReflector(new SingleFileSourceLocator($file, (new BetterReflection())->astLocator()));
80
    }
81
}
82