Test Setup Failed
Push — master ( 7450ca...e4bb3e )
by Gabriel
05:50 queued 15s
created

HasMatcherTraitTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 56
loc 56
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testNotFound() 8 8 1
A testRouteLiteral() 20 20 1
A testRouteDynamic() 22 22 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\Request;
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
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
12
13
/**
14
 * Class HasMatcherTraitTest
15
 * @package Nip\Router\Tests\Router\Traits
16
 */
17 View Code Duplication
class HasMatcherTraitTest extends AbstractTest
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
20
    public function testNotFound()
21
    {
22
        $router = new Router();
23
        self::expectException(ResourceNotFoundException::class);
24
25
        $request = Request::create('/404');
26
        $router->matchRequest($request);
27
    }
28
29
    public function testRouteLiteral()
30
    {
31
        $router = new Router();
32
        $collection = $router->getRoutes();
33
34
        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 32 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...
35
        RouteFactory::generateLiteralRoute($collection, "api.index", Route::class, "/api", "/index");
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 32 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...
36
37
        $request = Request::create('/api/index');
38
        $params = $router->matchRequest($request);
39
        self::assertEquals(['_route' => 'api.index'], $params);
40
41
        $currentRoute = $router->getCurrent();
42
        self::assertInstanceOf(Route::class, $currentRoute);
43
        self::assertEquals('api.index', $currentRoute->getName());
44
45
        $request = Request::create('/admin/index');
46
        $router->matchRequest($request);
47
        self::assertEquals('admin.index', $router->getCurrent()->getName());
48
    }
49
50
    public function testRouteDynamic()
51
    {
52
        $router = new Router();
53
        $collection = $router->getRoutes();
54
55
        RouteFactory::generateStandardRoute($collection, "admin.standard", StandardRoute::class, "/admin",
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 53 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
            '/:controller/:action', ['module' => 'admin']);
57
        RouteFactory::generateStandardRoute($collection, "api.standard", StandardRoute::class, "/api",
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 53 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...
58
            '/:controller/:action', ['module' => 'api']);
59
60
        $request = Request::create('/api/pages/delete');
61
        self::assertEquals(
62
            ['module' => 'api', 'controller' => 'pages', 'action' => 'delete', '_route' => 'api.standard'],
63
            $router->matchRequest($request)
64
        );
65
66
        $request = Request::create('/admin/pages/delete');
67
        self::assertEquals(
68
            ['module' => 'admin', 'controller' => 'pages', 'action' => 'delete', '_route' => 'admin.standard'],
69
            $router->matchRequest($request)
70
        );
71
    }
72
}
73