Test Setup Failed
Push — master ( 415317...7450ca )
by Gabriel
02:29 queued 10s
created

HasGeneratorTraitTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 80.7 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 46
loc 57
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetGenerator() 0 5 1
A testAssemble() 23 23 1
A testGenerate() 23 23 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Nip\Router\Tests\Router\Traits;
4
5
use Nip\Router\Generator\UrlGenerator;
6
use Nip\Router\Route\Route;
7
use Nip\Router\RouteFactory;
8
use Nip\Router\Router;
9
use Nip\Router\Tests\AbstractTest;
10
use Nip\Router\Tests\Fixtures\Application\Library\Router\Route\StandardRoute;
11
12
/**
13
 * Class HasGeneratorTraitTest
14
 * @package Nip\Router\Tests\Router\Traits
15
 */
16
class HasGeneratorTraitTest extends AbstractTest
17
{
18
19
    public function testGetGenerator()
20
    {
21
        $router = new Router();
22
        self::assertInstanceOf(UrlGenerator::class, $router->getGenerator());
23
    }
24
25 View Code Duplication
    public function testAssemble()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        $router = new Router();
28
        $collection = $router->getRoutes();
29
30
        RouteFactory::generateLiteralRoute($collection, "admin.index", Route::class, "/admin", "/index");
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 28 can also be of type array<integer,object<Nip\Router\Route\Route>>; however, Nip\Router\RouteFactory::generateLiteralRoute() does only seem to accept object<Nip\Router\RouteCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
31
        RouteFactory::generateStandardRoute($collection, "api.index", StandardRoute::class, "/api");
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 28 can also be of type array<integer,object<Nip\Router\Route\Route>>; however, Nip\Router\RouteFactory::generateStandardRoute() does only seem to accept object<Nip\Router\RouteCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32
33
        self::assertSame(
34
            '/admin/index?controller=posts',
35
            $router->assemble('admin.index', ['controller' => 'posts'])
36
        );
37
38
        self::assertSame(
39
            '/api/posts',
40
            $router->assemble('api.index', ['controller' => 'posts'])
41
        );
42
43
        self::assertSame(
44
            '/api/posts/create?test=true',
45
            $router->assemble('api.index', ['controller' => 'posts', 'action' => 'create', 'test' => 'true'])
46
        );
47
    }
48
49 View Code Duplication
    public function testGenerate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $router = new Router();
52
        $collection = $router->getRoutes();
53
54
        RouteFactory::generateLiteralRoute($collection, "admin.index", Route::class, "/admin", "/index");
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 52 can also be of type array<integer,object<Nip\Router\Route\Route>>; however, Nip\Router\RouteFactory::generateLiteralRoute() does only seem to accept object<Nip\Router\RouteCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55
        RouteFactory::generateStandardRoute($collection, "api.index", StandardRoute::class, "/api");
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 52 can also be of type array<integer,object<Nip\Router\Route\Route>>; however, Nip\Router\RouteFactory::generateStandardRoute() does only seem to accept object<Nip\Router\RouteCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56
57
        self::assertSame(
58
            '/admin/index?controller=posts',
59
            $router->generate('admin.index', ['controller' => 'posts'])
60
        );
61
62
        self::assertSame(
63
            '/api/posts',
64
            $router->generate('api.index', ['controller' => 'posts'])
65
        );
66
67
        self::assertSame(
68
            '/api/posts/create?param=test',
69
            $router->generate('api.index', ['controller' => 'posts','action' => 'create', 'param' => 'test'])
70
        );
71
    }
72
}
73