GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

LinkFactoryTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 26.55 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 10
dl 30
loc 113
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A test() 0 12 1
A testRoute() 11 11 1
A testExpressions() 0 12 1
A testParametersExpression() 0 11 1
A testParametersDeepArrayExpression() 19 19 1
A testRouteRequiresGenerator() 0 14 1
A testRouteParamatersNotArray() 0 14 1
A createLinkFactory() 0 10 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 Hateoas\Tests\Factory;
4
5
use Hateoas\Configuration\Relation;
6
use Hateoas\Configuration\Route;
7
use Hateoas\Expression\ExpressionEvaluator;
8
use Hateoas\Factory\LinkFactory;
9
use Hateoas\Tests\TestCase;
10
use Hateoas\UrlGenerator\CallableUrlGenerator;
11
use Hateoas\UrlGenerator\UrlGeneratorRegistry;
12
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
13
14
class LinkFactoryTest extends TestCase
15
{
16
    public function test()
17
    {
18
        $link = $this->createLinkFactory()->createLink(
19
            new TestedObject(),
20
            new Relation('foo', '/bar', null, array('templated' => false))
21
        );
22
23
        $this->assertInstanceOf('Hateoas\Model\Link', $link);
24
        $this->assertSame('foo', $link->getRel());
25
        $this->assertSame('/bar', $link->getHref());
26
        $this->assertSame(['templated' => false], $link->getAttributes());
27
    }
28
29 View Code Duplication
    public function testRoute()
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...
30
    {
31
        $link = $this->createLinkFactory()->createLink(
32
            new TestedObject(),
33
            new Relation('foo', new Route('/route', array('foo' => 'bar')))
34
        );
35
36
        $this->assertInstanceOf('Hateoas\Model\Link', $link);
37
        $this->assertSame('foo', $link->getRel());
38
        $this->assertSame('/route?foo=bar', $link->getHref());
39
    }
40
41
    public function testExpressions()
42
    {
43
        $link = $this->createLinkFactory()->createLink(
44
            new TestedObject(),
45
            new Relation('expr(object.getRel())', 'expr(object.getUrl())', null, array('expr(object.getRel())' => 'expr(object.getUrl())'))
46
        );
47
48
        $this->assertInstanceOf('Hateoas\Model\Link', $link);
49
        $this->assertSame('tested-rel', $link->getRel());
50
        $this->assertSame('/tested-url', $link->getHref());
51
        $this->assertSame(['tested-rel' => '/tested-url'], $link->getAttributes());
52
    }
53
54
    public function testParametersExpression()
55
    {
56
        $link = $this->createLinkFactory()->createLink(
57
            new TestedObject(),
58
            new Relation('foo', new Route('/route', 'expr(object.getParameters())'))
59
        );
60
61
        $this->assertInstanceOf('Hateoas\Model\Link', $link);
62
        $this->assertSame('foo', $link->getRel());
63
        $this->assertSame('/route?a=b', $link->getHref());
64
    }
65
66 View Code Duplication
    public function testParametersDeepArrayExpression()
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...
67
    {
68
        $link = $this->createLinkFactory()->createLink(
69
            new TestedObject(),
70
            new Relation(
71
                'foo',
72
                new Route(
73
                    '/route',
74
                    array(
75
                        'expr(object.getRel())' => array('expr(object.getRel())')
76
                    )
77
                )
78
            )
79
        );
80
81
        $this->assertInstanceOf('Hateoas\Model\Link', $link);
82
        $this->assertSame('foo', $link->getRel());
83
        $this->assertSame('/route?tested-rel%5B0%5D=tested-rel', $link->getHref());
84
    }
85
86
    public function testRouteRequiresGenerator()
87
    {
88
        $expressionEvaluator = new ExpressionEvaluator(new ExpressionLanguage());
89
        $urlGeneratorRegistry = new UrlGeneratorRegistry();
90
91
        $linkFactory = new LinkFactory($expressionEvaluator, $urlGeneratorRegistry);
92
93
        $this->setExpectedException('RuntimeException', 'You cannot use a route without an url generator.');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
94
95
        $linkFactory->createLink(
96
            new TestedObject(),
97
            new Relation('foo', new Route('/route', array('foo' => 'bar')))
98
        );
99
    }
100
101
    public function testRouteParamatersNotArray()
102
    {
103
        $linkFactory = $this->createLinkFactory();
104
105
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
106
            'RuntimeException',
107
            'The route parameters should be an array, string given. Maybe you forgot to wrap the expression in expr(...).'
108
        );
109
110
        $linkFactory->createLink(
111
            new TestedObject(),
112
            new Relation('foo', new Route('/route', 'yolo'))
113
        );
114
    }
115
116
    private function createLinkFactory()
117
    {
118
        $defaultUrlGenerator = new CallableUrlGenerator(function ($route, $parameters) {
119
            return $route . '?' . http_build_query($parameters);
120
        });
121
        $expressionEvaluator = new ExpressionEvaluator(new ExpressionLanguage());
122
        $urlGeneratorRegistry = new UrlGeneratorRegistry($defaultUrlGenerator);
123
124
        return new LinkFactory($expressionEvaluator, $urlGeneratorRegistry);
125
    }
126
}
127
128
class TestedObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
129
{
130
    public function getRel()
131
    {
132
        return 'tested-rel';
133
    }
134
135
    public function getUrl()
136
    {
137
        return '/tested-url';
138
    }
139
140
    public function getParameters()
141
    {
142
        return array(
143
            'a' => 'b',
144
        );
145
    }
146
}
147