Completed
Branch master (600230)
by Andy
03:12 queued 59s
created

AbstractTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 29.17 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 7
dl 28
loc 96
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouter() 0 18 2
A getFooRouteCollection() 0 9 1
A getFooRequest() 14 14 3
A getBazRequest() 14 14 3
A configProvider() 0 13 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 Palmtree\CanonicalUrlBundle\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Symfony\Component\Config\Loader\LoaderInterface;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\Routing\RequestContext;
9
use Symfony\Component\Routing\Route;
10
use Symfony\Component\Routing\RouteCollection;
11
use Symfony\Component\Routing\Router;
12
13
abstract class AbstractTest extends TestCase
14
{
15
    /**
16
     * @param RouteCollection|null $routeCollection
17
     *
18
     * @return Router
19
     */
20
    protected function getRouter($routeCollection = null)
21
    {
22
        if (!$routeCollection) {
23
            $routeCollection = $this->getFooRouteCollection();
24
        }
25
26
        $loader = $this->createMock(LoaderInterface::class);
27
        $loader->method('load')->willReturn($routeCollection);
28
29
        /** @var LoaderInterface $loader */
30
        $context = new RequestContext();
31
        $context->setScheme('https')->setHost('example.org');
32
33
        $router = new Router($loader, '');
34
        $router->setContext($context);
35
36
        return $router;
37
    }
38
39
    /**
40
     * @return RouteCollection
41
     */
42
    protected function getFooRouteCollection()
43
    {
44
        $routeCollection = new RouteCollection();
45
        $routeCollection->add('foo', new Route('/foo'));
46
        $routeCollection->add('baz', new Route('/baz/'));
47
        $routeCollection->setHost('example.org');
48
49
        return $routeCollection;
50
    }
51
52
    /**
53
     * @param bool $secure
54
     * @param bool $trailingSlash
55
     * @return Request
56
     */
57 View Code Duplication
    protected function getFooRequest($secure = true, $trailingSlash = true)
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...
58
    {
59
        $scheme = ($secure) ? 'https' : 'http';
60
        $uri    = "$scheme://example.org/foo";
61
62
        if ($trailingSlash) {
63
            $uri .= '/';
64
        }
65
66
        $request = Request::create($uri);
67
        $request->attributes->set('_route', 'foo');
68
69
        return $request;
70
    }
71
72
    /**
73
     * @param bool $secure
74
     * @param bool $trailingSlash
75
     * @return Request
76
     */
77 View Code Duplication
    protected function getBazRequest($secure = true, $trailingSlash = true)
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...
78
    {
79
        $scheme = ($secure) ? 'https' : 'http';
80
        $uri    = "$scheme://example.org/baz";
81
82
        if ($trailingSlash) {
83
            $uri .= '/';
84
        }
85
86
        $request = Request::create($uri);
87
        $request->attributes->set('_route', 'baz');
88
89
        return $request;
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function configProvider()
96
    {
97
        return [
98
            'config' => [
99
                [
100
                    'site_url'       => 'https://example.org',
101
                    'redirect'       => true,
102
                    'redirect_code'  => 302,
103
                    'trailing_slash' => false,
104
                ]
105
            ]
106
        ];
107
    }
108
}
109