Completed
Push — master ( a1da24...44a686 )
by Antoine
18s queued 11s
created

ObjectNormalizer::hasCacheableSupportsMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Hal\Serializer;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use Symfony\Component\Serializer\Exception\LogicException;
18
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
19
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
22
/**
23
 * Decorates the output with JSON HAL metadata when appropriate, but otherwise
24
 * just passes through to the decorated normalizer.
25
 */
26
final class ObjectNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
27
{
28
    public const FORMAT = 'jsonhal';
29
30
    private $decorated;
31
    private $iriConverter;
32
33
    public function __construct(NormalizerInterface $decorated, IriConverterInterface $iriConverter)
34
    {
35
        $this->decorated = $decorated;
36
        $this->iriConverter = $iriConverter;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function supportsNormalization($data, $format = null, array $context = []): bool
43
    {
44
        return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\Serial...supportsNormalization() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        return self::FORMAT === $format && $this->decorated->/** @scrutinizer ignore-call */ supportsNormalization($data, $format, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function hasCacheableSupportsMethod(): bool
51
    {
52
        return $this->decorated->hasCacheableSupportsMethod();
0 ignored issues
show
Bug introduced by
The method hasCacheableSupportsMethod() does not exist on Symfony\Component\Serial...zer\NormalizerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Serial...er\SerializerNormalizer or ApiPlatform\Core\Tests\F...DocumentationNormalizer or Symfony\Component\Serial...rmalizer\TestNormalizer or Symfony\Component\Serial...sonSerializerNormalizer or Symfony\Component\Serial...wareNormalizerInterface or Symfony\Component\Serial...ectSerializerNormalizer or Symfony\Component\Serializer\Serializer. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        return $this->decorated->/** @scrutinizer ignore-call */ hasCacheableSupportsMethod();
Loading history...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function normalize($object, $format = null, array $context = [])
59
    {
60
        if (isset($context['api_resource'])) {
61
            $originalResource = $context['api_resource'];
62
            unset($context['api_resource']);
63
        }
64
65
        $data = $this->decorated->normalize($object, $format, $context);
66
        if (!\is_array($data)) {
67
            return $data;
68
        }
69
70
        if (!isset($originalResource)) {
71
            return $data;
72
        }
73
74
        $metadata = [
75
            '_links' => [
76
                'self' => [
77
                    'href' => $this->iriConverter->getIriFromItem($originalResource),
78
                ],
79
            ],
80
        ];
81
82
        return $metadata + $data;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
89
    {
90
        // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
91
        return self::FORMAT === $format;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     *
97
     * @throws LogicException
98
     */
99
    public function denormalize($data, $class, $format = null, array $context = [])
100
    {
101
        throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT));
102
    }
103
}
104