Issues (105)

src/ServiceProvider/Traits/RouterTrait.php (2 issues)

1
<?php
2
3
namespace Nip\Router\ServiceProvider\Traits;
4
5
use Nip\Http\Request;
6
use Nip\Router\RequestContext;
7
use Nip\Router\Router;
8
use Symfony\Component\Routing\RouterInterface;
9
10
/**
11
 * Trait RouterTrait
12
 * @package Nip\Router\ServiceProvider\Traits
13
 */
14
trait RouterTrait
15
{
16 2
    public function registerRouter()
17
    {
18
        $this->getContainer()->share('router', function () {
0 ignored issues
show
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

18
        $this->/** @scrutinizer ignore-call */ 
19
               getContainer()->share('router', function () {
Loading history...
19 2
            return $this->newRouter();
20 2
        });
21
22 2
        $this->getContainer()->add(RouterInterface::class, Router::class);
23
24
        $this->getContainer()->share(Router::class, function () {
25
            return $this->getContainer()->get('router');
26 2
        });
27 2
    }
28
29
    /**
30
     * @return Router
31
     */
32 2
    public function newRouter()
33
    {
34 2
        $app = $this->getContainer()->get('app');
35
36
        /** @var Router $router */
37 2
        $router = $this->getContainer()->make(
38 2
            RouterInterface::class,
39
            [
40 2
                'loader' => $this->getContainer()->get('routing.loader'),
41 2
                'resource' => 'routes.php',
42
                'options' => [
43 2
                    'cache_dir' => $app->getCachedRoutesPath(),
44
                ]
45
            ]
46
        );
47
48 2
        $request = Request::instance();
49 2
        if ($request instanceof Request) {
0 ignored issues
show
$request is always a sub-type of Nip\Http\Request.
Loading history...
50 2
            $router->setContext((new RequestContext())->fromRequest($request));
51
        }
52 2
        return $router;
53
    }
54
}
55