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
|
|||
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
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
53 | * |
||
54 | * @return MethodUri |
||
0 ignored issues
–
show
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
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
|
|||
64 | } |
||
65 | } |
||
66 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: