Anchor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 42
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidLinkAnnotation() 0 4 3
A href() 0 14 3
A getMethodUri() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BEAR\Resource\Annotation\Link as LinkAnnotation;
8
use BEAR\Resource\Exception\LinkException;
9
use Override;
10
use Ray\Aop\ReflectionMethod;
11
12
use function array_merge;
13
use function assert;
14
use function is_array;
15
use function ucfirst;
16
use function uri_template;
17
18
/**
19
 * @psalm-import-type MethodUri from Types
20
 * @psalm-import-type Query from Types
21
 */
22
final class Anchor implements AnchorInterface
23
{
24
    /**
25
     * {@inheritDoc}
26
     *
27
     * @throws LinkException
28
     */
29
    #[Override]
30
    public function href(string $rel, AbstractRequest $request, array $query): array
31
    {
32
        $classMethod = 'on' . ucfirst($request->method);
33
        $annotations = (new ReflectionMethod($request->resourceObject::class, $classMethod))->getAnnotations();
34
        foreach ($annotations as $annotation) {
35
            if ($this->isValidLinkAnnotation($annotation, $rel)) {
36
                assert($annotation instanceof LinkAnnotation);
37
38
                return $this->getMethodUri($request, $query, $annotation);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMethodU...t, $query, $annotation) returns the type array<integer,string> which is incompatible with the return type mandated by BEAR\Resource\AnchorInterface::href() of BEAR\Resource\MethodUri.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
39
            }
40
        }
41
42
        throw new LinkException("rel:{$rel} class:" . $request->resourceObject::class, 500);
43
    }
44
45
    private function isValidLinkAnnotation(object $annotation, string $rel): bool
46
    {
47
        /** @psalm-suppress RedundantConditionGivenDocblockType */
48
        return $annotation instanceof LinkAnnotation && $annotation->rel !== null && $annotation->rel === $rel;
49
    }
50
51
    /**
52
     * @param Query $query
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
     *
54
     * @return MethodUri
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\MethodUri was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
     */
56
    private function getMethodUri(AbstractRequest $request, array $query, LinkAnnotation $annotation): array
57
    {
58
        /** @var array|mixed $body */
59
        $body = $request->resourceObject->body;
60
        $query = is_array($body) ? array_merge($body, $query) : [];
61
        $uri = uri_template($annotation->href, $query);
62
63
        return [$annotation->method, $uri];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($annotation->method, $uri) returns the type array<integer,string> which is incompatible with the documented return type BEAR\Resource\MethodUri.
Loading history...
64
    }
65
}
66