Completed
Push — 1.x ( 64141c...67746d )
by Akihito
14s queued 11s
created

src/Provide/Representation/HalLink.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of the BEAR.Package package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package\Provide\Representation;
8
9
use BEAR\Resource\Annotation\Link;
10
use BEAR\Sunday\Extension\Router\RouterInterface;
11
use Nocarrier\Hal;
12
13
final class HalLink
14
{
15
    /**
16
     * @var RouterInterface
17
     */
18
    private $router;
19
20 4
    public function __construct(RouterInterface $router)
21
    {
22 4
        $this->router = $router;
23 4
    }
24
25 12
    public function getReverseLink($uri) : string
26
    {
27 12
        $urlParts = parse_url($uri);
28 12
        $routeName = $urlParts['path'];
29 12
        isset($urlParts['query']) ? parse_str($urlParts['query'], $value) : $value = [];
30 12
        if ($value === []) {
31 5
            return $uri;
32
        }
33 9
        $reverseUri = $this->router->generate($routeName, $value);
0 ignored issues
show
It seems like $value can also be of type null; however, BEAR\Sunday\Extension\Ro...erInterface::generate() does only seem to accept array, 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...
34 9
        if (\is_string($reverseUri)) {
35 2
            return $reverseUri;
36
        }
37
38 7
        return $uri;
39
    }
40
41 12
    public function addHalLink(array $body, array $methodAnnotations, Hal $hal) : Hal
42
    {
43 12
        if (! empty($methodAnnotations)) {
44 8
            $hal = $this->linkAnnotation($body, $methodAnnotations, $hal);
45
        }
46 12
        if (isset($body['_links'])) {
47 4
            $hal = $this->bodyLink($body, $hal);
48
        }
49
50 12
        return $hal;
51
    }
52
53 8
    private function linkAnnotation(array $body, array $methodAnnotations, Hal $hal) : Hal
54
    {
55 8
        foreach ($methodAnnotations as $annotation) {
56 8
            if (! $annotation instanceof Link) {
57 3
                continue;
58
            }
59 6
            $uri = uri_template($annotation->href, $body);
60 6
            $reverseUri = $this->getReverseLink($uri);
61 6
            $hal->addLink($annotation->rel, $reverseUri);
62
        }
63
64 8
        return $hal;
65
    }
66
67 4
    private function bodyLink(array $body, Hal $hal) : Hal
68
    {
69 4
        foreach ((array) $body['_links'] as $rel => $link) {
70 4
            $attr = $link;
71 4
            unset($attr['href']);
72 4
            $hal->addLink($rel, $link['href'], $attr);
73
        }
74
75 4
        return $hal;
76
    }
77
}
78