• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 6867212600

14 Nov 2023 05:15PM UTC coverage: 37.474% (-0.01%) from 37.484%
6867212600

push

github

soyuka
chore: phpstan and deprecations

3 of 3 new or added lines in 2 files covered. (100.0%)

90 existing lines in 7 files now uncovered.

10323 of 27547 relevant lines covered (37.47%)

13.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/src/JsonApi/Serializer/ErrorNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\JsonApi\Serializer;
15

16
use ApiPlatform\Problem\Serializer\ErrorNormalizerTrait;
17
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
18
use Symfony\Component\ErrorHandler\Exception\FlattenException;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\Serializer\Serializer;
21

22
/**
23
 * Converts {@see \Exception} or {@see FlattenException} or to a JSON API error representation.
24
 *
25
 * @deprecated we will use the ItemNormalizer in 4.x instead
26
 *
27
 * @author Héctor Hurtarte <hectorh30@gmail.com>
28
 */
29
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
30
{
31
    use ErrorNormalizerTrait;
32

33
    public const FORMAT = 'jsonapi';
34
    public const TITLE = 'title';
35
    private array $defaultContext = [
36
        self::TITLE => 'An error occurred',
37
    ];
38

39
    public function __construct(private readonly bool $debug = false, array $defaultContext = [], private readonly ?NormalizerInterface $itemNormalizer = null)
40
    {
41
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
76✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function normalize(mixed $object, string $format = null, array $context = []): array
48
    {
49
        if ($this->itemNormalizer) {
16✔
UNCOV
50
            return $this->itemNormalizer->normalize($object, $format, $context);
×
51
        }
52

53
        $data = [
16✔
54
            'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
16✔
55
            'description' => $this->getErrorMessage($object, $context, $this->debug),
16✔
56
        ];
16✔
57

58
        if (null !== $errorCode = $this->getErrorCode($object)) {
16✔
59
            $data['code'] = $errorCode;
4✔
60
        }
61

62
        if ($this->debug && null !== $trace = $object->getTrace()) {
16✔
63
            $data['trace'] = $trace;
6✔
64
        }
65

66
        return $data;
16✔
67
    }
68

69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
73
    {
74
        if ($context['skip_deprecated_exception_normalizers'] ?? false) {
2✔
UNCOV
75
            return false;
×
76
        }
77

78
        $decoration = $this->itemNormalizer ? $this->itemNormalizer->supportsNormalization($data, $format, $context) : true;
2✔
79

80
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException) && $decoration;
2✔
81
    }
82

83
    public function getSupportedTypes($format): array
84
    {
85
        if (self::FORMAT === $format) {
50✔
86
            return [
2✔
87
                \Exception::class => false,
2✔
88
                FlattenException::class => false,
2✔
89
            ];
2✔
90
        }
91

92
        return [];
50✔
93
    }
94

95
    public function hasCacheableSupportsMethod(): bool
96
    {
UNCOV
97
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
UNCOV
98
            trigger_deprecation(
×
99
                'api-platform/core',
×
100
                '3.1',
×
101
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
102
                __METHOD__
×
103
            );
×
104
        }
105

UNCOV
106
        return false;
×
107
    }
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc