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.

LinkFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B createLink() 0 38 5
1
<?php
2
3
namespace Hateoas\Factory;
4
5
use Hateoas\Configuration\Relation;
6
use Hateoas\Configuration\Route;
7
use Hateoas\Expression\ExpressionEvaluator;
8
use Hateoas\Model\Link;
9
use Hateoas\UrlGenerator\UrlGeneratorRegistry;
10
11
/**
12
 * @author Adrien Brault <[email protected]>
13
 */
14
class LinkFactory
15
{
16
    /**
17
     * @var ExpressionEvaluator
18
     */
19
    private $expressionEvaluator;
20
21
    /**
22
     * @var UrlGeneratorRegistry
23
     */
24
    private $urlGeneratorRegistry;
25
26
    /**
27
     * @param ExpressionEvaluator  $expressionEvaluator
28
     * @param UrlGeneratorRegistry $urlGeneratorRegistry
29
     */
30
    public function __construct(ExpressionEvaluator $expressionEvaluator, UrlGeneratorRegistry $urlGeneratorRegistry)
31
    {
32
        $this->expressionEvaluator  = $expressionEvaluator;
33
        $this->urlGeneratorRegistry = $urlGeneratorRegistry;
34
    }
35
36
    /**
37
     * @param object   $object
38
     * @param Relation $relation
39
     *
40
     * @return Link
41
     */
42
    public function createLink($object, Relation $relation)
43
    {
44
        $rel =  $this->expressionEvaluator->evaluate($relation->getName(), $object);
45
        $href = $relation->getHref();
46
47
        if ($href instanceof Route) {
48
            if (!$this->urlGeneratorRegistry->hasGenerators()) {
49
                throw new \RuntimeException('You cannot use a route without an url generator.');
50
            }
51
52
            $name       = $this->expressionEvaluator->evaluate($href->getName(), $object);
53
            $parameters = is_array($href->getParameters())
54
                ? $this->expressionEvaluator->evaluateArray($href->getParameters(), $object)
55
                : $this->expressionEvaluator->evaluate($href->getParameters(), $object)
56
            ;
57
            $isAbsolute = $this->expressionEvaluator->evaluate($href->isAbsolute(), $object);
0 ignored issues
show
Documentation introduced by William DURAND
$href->isAbsolute() is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
59
            if (!is_array($parameters)) {
60
                throw new \RuntimeException(
61
                    sprintf(
62
                        'The route parameters should be an array, %s given. Maybe you forgot to wrap the expression in expr(...).',
63
                        gettype($parameters)
64
                    )
65
                );
66
            }
67
68
            $href = $this->urlGeneratorRegistry
69
                ->get($href->getGenerator())
70
                ->generate($name, $parameters, $isAbsolute)
0 ignored issues
show
Documentation introduced by William DURAND
$isAbsolute is of type array|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by William DURAND
It seems like $name defined by $this->expressionEvaluat...ef->getName(), $object) on line 52 can also be of type array; however, Hateoas\UrlGenerator\Url...orInterface::generate() does only seem to accept string, 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...
71
            ;
72
        } else {
73
            $href = $this->expressionEvaluator->evaluate($href, $object);
74
        }
75
76
        $attributes = $this->expressionEvaluator->evaluateArray($relation->getAttributes(), $object);
77
78
        return new Link($rel, $href, $attributes);
0 ignored issues
show
Bug introduced by Adrien Brault
It seems like $rel defined by $this->expressionEvaluat...on->getName(), $object) on line 44 can also be of type array; however, Hateoas\Model\Link::__construct() does only seem to accept string, 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...
Bug introduced by Adrien Brault
It seems like $href defined by $this->expressionEvaluat...valuate($href, $object) on line 73 can also be of type array; however, Hateoas\Model\Link::__construct() does only seem to accept string, 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...
79
    }
80
}
81