RouteCompiler::compile()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 18
c 2
b 1
f 0
nc 2
nop 0
dl 0
loc 31
ccs 20
cts 20
cp 1
crap 3
rs 9.6666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiistack\Routing;
6
7
use Doctrine\Common\Annotations\AnnotationRegistry;
8
use Yiisoft\Router\Group;
9
use Yiisoft\Router\Route;
10
use Yiistack\Annotated\AnnotationLoader;
11
use Yiistack\Routing\Annotation\Controller;
12
use Yiistack\Routing\Annotation\RouteAnnotationInterface;
13
14
class RouteCompiler
15
{
16
    private AnnotationLoader $loader;
17
18 1
    public function __construct(AnnotationLoader $loader)
19
    {
20 1
        $this->loader = $loader;
21 1
    }
22
23
    /**
24
     * @psalm-suppress DeprecatedMethod
25
     *
26
     * @return array
27
     */
28 1
    public function compile(): array
29
    {
30
        // autoload
31 1
        AnnotationRegistry::registerUniqueLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...:registerUniqueLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

31
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerUniqueLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
32
33 1
        $compiledRoutes = [];
34 1
        $controllers = $this->loader->findClasses(Controller::class);
35 1
        foreach ($controllers as $annotatedClass) {
36 1
            $controllerClass = $annotatedClass->getClass();
37 1
            $controller = $annotatedClass->getAnnotation();
38 1
            $controllerRoutes = $this->loader->findMethods(RouteAnnotationInterface::class, $controllerClass);
39 1
            $compiledRoutes[] = Group::create(
40 1
                $controller->getRoute(),
41 1
                static function (Group $group) use ($controllerRoutes) {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type Yiisoft\Middleware\Dispa...ddlewareDispatcher|null expected by parameter $dispatcher of Yiisoft\Router\Group::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
                /** @scrutinizer ignore-type */ static function (Group $group) use ($controllerRoutes) {
Loading history...
42 1
                    foreach ($controllerRoutes as $controllerAction) {
43 1
                        $group->addRoute(
0 ignored issues
show
Bug introduced by
The method addRoute() does not exist on Yiisoft\Router\Group. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
                        $group->/** @scrutinizer ignore-call */ 
44
                                addRoute(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44 1
                            Route::methods(
45 1
                                $controllerAction->getAnnotation()->getMethods(),
46 1
                                $controllerAction->getAnnotation()->getRoute(),
47
                                [
0 ignored issues
show
Bug introduced by
array($controllerAction-...thod()->getShortName()) of type array<integer,string> is incompatible with the type Yiisoft\Middleware\Dispa...ddlewareDispatcher|null expected by parameter $dispatcher of Yiisoft\Router\Route::methods(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
                                /** @scrutinizer ignore-type */ [
Loading history...
48 1
                                    $controllerAction->getClass()->getName(),
49 1
                                    $controllerAction->getMethod()->getShortName(),
50
                                ]
51
                            )
52
                        );
53
                    }
54 1
                }
55
            );
56
        }
57
58 1
        return $compiledRoutes;
59
    }
60
}
61