Test Setup Failed
Push — master ( 89d4a6...423952 )
by Gabriel
01:49
created

UrlGeneratorTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerUrlGenerator() 0 13 1
A getRequest() 0 7 2
1
<?php
2
3
namespace Nip\Router\ServiceProvider\Traits;
4
5
use Nip\Request;
6
use Nip\Router\Generator\UrlGenerator;
7
use Nip\Router\RequestContext;
8
9
/**
10
 * Trait UrlGeneratorTrait
11
 * @package Nip\Router\ServiceProvider\Traits
12
 */
13
trait UrlGeneratorTrait
14
{
15
16
    /**
17
     * Register the URL generator service.
18
     *
19
     * @return void
20
     */
21 1
    public function registerUrlGenerator()
22
    {
23
        $this->getContainer()->singleton('url', function () {
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
24 1
            $routes = $this->getContainer()->get('routes');
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25 1
            $request = $this->getRequest();
26
27 1
            $url = new UrlGenerator(
28 1
                $routes,
29 1
                (new RequestContext())->fromRequest($request)
30
            );
31 1
            return $url;
32 1
        });
33 1
    }
34
35
    /**
36
     * @return Request
37
     */
38 1
    protected function getRequest()
39
    {
40 1
        if (function_exists('request')) {
41
            return request();
42
        }
43 1
        return new Request();
44
    }
45
}