YamlLoaderTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 112
Duplicated Lines 57.14 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 64
loc 112
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testLoadFileNoCacheParse() 10 10 1
A testLoadFileNoCacheNoFile() 10 10 1
A testLoadFileNoCache() 15 15 1
A testLoadFileCache() 14 14 1
A testLoadFilesNoCache() 15 15 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
 * This file is part of the DS Framework.
4
 *
5
 * (c) Dan Smith <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Tests\Ds\Router\Loaders;
11
12
use Ds\Router\Exceptions\RouterException;
13
use Ds\Router\Interfaces\RouteCollectionInterface;
14
use Ds\Router\Interfaces\RouterInterface;
15
use Ds\Router\Loaders\YamlLoader;
16
use Ds\Router\RouteCollection;
17
18
/**
19
 * Class YamlLoaderTest
20
 * @package Tests\Ds\Router\Loaders
21
 */
22
class YamlLoaderTest extends \PHPUnit_Framework_TestCase
23
{
24
25
    /**
26
     * @var \PHPUnit_Framework_MockObject_MockObject|RouterInterface
27
     */
28
    public $router;
29
30
    /**
31
     * @var YamlLoader
32
     */
33
    public $loader;
34
35
    /**
36
     * @var string
37
     */
38
    public $file;
39
40
    /**
41
     *
42
     */
43
    public function setUp()
44
    {
45
        $this->router = $this->getMockBuilder(RouterInterface::class)->getMock();
46
        $this->loader = new YamlLoader($this->router);
47
        $this->file = __DIR__ . '/Files/FileLoaderRoutes.yml';
48
    }
49
50
    /**
51
     *
52
     */
53 View Code Duplication
    public function testLoadFileNoCacheParse()
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...
54
    {
55
        $this->setExpectedException(RouterException::class);
56
57
58
        $this->router->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\RouterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
59
            ->method('isCached')
60
            ->willReturn(false);
61
        $this->loader->loadFile(__DIR__ . '/Files/malformed.yml');
62
    }
63
64
    /**
65
     *
66
     */
67 View Code Duplication
    public function testLoadFileNoCacheNoFile()
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...
68
    {
69
        $this->setExpectedException(RouterException::class);
70
71
        $this->router->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\RouterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
72
            ->method('isCached')
73
            ->willReturn(false);
74
75
        $this->loader->loadFile(__DIR__ . '/RouteFileNone.yml');
76
    }
77
78
    /**
79
     *
80
     */
81 View Code Duplication
    public function testLoadFileNoCache()
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...
82
    {
83
        $this->router->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\RouterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
            ->method('isCached')
85
            ->willReturn(false);
86
87
        $collection = $this->loader->loadFile($this->file);
88
        $route = $collection->current();
89
90
        $expected = 'myHandler::myMethod';
91
        $actual = $route->getHandler();
92
93
        $this->assertInstanceOf(RouteCollectionInterface::class, $collection);
94
        $this->assertEquals($expected, $actual);
95
    }
96
97
    /**
98
     *
99
     */
100 View Code Duplication
    public function testLoadFileCache()
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...
101
    {
102
        $this->router->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\RouterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
103
            ->method('isCached')
104
            ->willReturn(true);
105
106
        $this->router->expects($this->once())
107
            ->method('getCollection')
108
            ->willReturn(new RouteCollection());
109
110
        $collection = $this->loader->loadFile($this->file);
111
112
        $this->assertInstanceOf(RouteCollection::class, $collection);
113
    }
114
115
    /**
116
     *
117
     */
118 View Code Duplication
    public function testLoadFilesNoCache()
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...
119
    {
120
        $this->router->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\RouterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
121
            ->method('isCached')
122
            ->willReturn(false);
123
124
        $this->router->expects($this->any())
125
            ->method('mergeCollection')
126
            ->willReturn($this->router);
127
128
        $this->loader->loadFiles([
129
            $this->file,
130
            __DIR__ . '/Files/FileLoaderRoutesAlt.yml'
131
        ]);
132
    }
133
}
134