Completed
Push — master ( c4d705...5571d6 )
by Filipe
03:31
created

ControllerDispatchInflectorSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 33.9 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 20
loc 59
rs 10
c 0
b 0
f 0

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
/**
4
 * This file is part of slick/web_stack package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace spec\Slick\WebStack\Dispatcher;
11
12
use Aura\Router\Route;
13
use PhpSpec\Exception\Example\FailureException;
14
use Slick\WebStack\Controller\ControllerMethods;
15
use Slick\WebStack\ControllerInterface;
16
use Slick\WebStack\Dispatcher\ControllerDispatch;
17
use Slick\WebStack\Dispatcher\ControllerDispatchInflector;
18
use PhpSpec\ObjectBehavior;
19
use Slick\WebStack\Dispatcher\ControllerDispatchInflectorInterface;
20
21
/**
22
 * ControllerDispatchInflectorSpec specs
23
 *
24
 * @package spec\Slick\WebStack\Dispatcher
25
 */
26
class ControllerDispatchInflectorSpec extends ObjectBehavior
27
{
28
29
    function its_a_controller_inflector()
30
    {
31
        $this->shouldBeAnInstanceOf(ControllerDispatchInflectorInterface::class);
32
    }
33
34
    function it_is_initializable()
35
    {
36
        $this->shouldHaveType(ControllerDispatchInflector::class);
37
    }
38
39
    function it_inflects_controller_dispatch_from_route_attributes()
40
    {
41
        $route = new Route();
42
        $route->attributes([
43
            'namespace' => 'spec\Slick\WebStack\Dispatcher',
44
            'controller' => 'pages',
45
            'action' => 'index',
46
            'args' => [123, 'test']
47
        ]);
48
        $this->inflect($route)->shouldBeAnInstanceOf(ControllerDispatch::class);
49
    }
50
    function it_converts_dashed_url_controller_param_into_controller_class_names()
51
    {
52
        $route = new Route();
53
        $route->attributes([
54
            'namespace' => 'spec\Slick\WebStack\Dispatcher',
55
            'controller' => 'yellow-pages',
56
            'action' => 'index'
57
        ]);
58
        $this->inflect($route)->shouldHaveControllerNamed('spec\Slick\WebStack\Dispatcher\YellowPages');
59
    }
60
    function it_converts_underscored_url_controller_param_into_controller_class_names()
61
    {
62
        $route = new Route();
63
        $route->attributes([
64
            'namespace' => 'spec\Slick\WebStack\Dispatcher',
65
            'controller' => 'yellow_pages',
66
            'action' => 'index'
67
        ]);
68
        $this->inflect($route)->shouldHaveControllerNamed('spec\Slick\WebStack\Dispatcher\YellowPages');
69
    }
70
    public function getMatchers()
71
    {
72
        return [
73
            'haveControllerNamed' => function (ControllerDispatch $subject, $name) {
74
                if ($subject->controllerClass()->getName() !== $name) {
75
                    throw new FailureException(
76
                        "Expected controller name to be '{$name}', ".
77
                        "but got {$subject->controllerClass()->getName()}"
78
                    );
79
                }
80
                return true;
81
            }
82
        ];
83
    }
84
}
85
86
class Pages implements ControllerInterface
87
{
88
    use ControllerMethods;
89
90
    public function index()
91
    {
92
93
    }
94
}
95
96
class YellowPages extends Pages
97
{
98
}