• 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

68.97
/src/Problem/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\Problem\Serializer;
15

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

21
/**
22
 * Normalizes errors according to the API Problem spec (RFC 7807).
23
 *
24
 * @see https://tools.ietf.org/html/rfc7807
25
 * @deprecated we will use the ItemNormalizer in 4.x instead
26
 *
27
 * @author Kévin Dunglas <dunglas@gmail.com>
28
 */
29
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
30
{
31
    use ErrorNormalizerTrait;
32
    public const FORMAT = 'jsonproblem';
33
    public const TYPE = 'type';
34
    public const TITLE = 'title';
35
    private array $defaultContext = [
36
        self::TYPE => 'https://tools.ietf.org/html/rfc2616#section-10',
37
        self::TITLE => 'An error occurred',
38
    ];
39

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

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

54
        $data = [
18✔
55
            'type' => $context[self::TYPE] ?? $this->defaultContext[self::TYPE],
18✔
56
            'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
18✔
57
            'detail' => $this->getErrorMessage($object, $context, $this->debug),
18✔
58
        ];
18✔
59

60
        if ($this->debug && null !== $trace = $object->getTrace()) {
18✔
61
            $data['trace'] = $trace;
8✔
62
        }
63

64
        return $data;
18✔
65
    }
66

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

76
        $decoration = $this->itemNormalizer ? $this->itemNormalizer->supportsNormalization($data, $format, $context) : true;
4✔
77

78
        return (self::FORMAT === $format || 'json' === $format) && ($data instanceof \Exception || $data instanceof FlattenException) && $decoration;
4✔
79
    }
80

81
    public function getSupportedTypes($format): array
82
    {
83
        if (self::FORMAT === $format || 'json' === $format) {
38✔
84
            return [
10✔
85
                \Exception::class => false,
10✔
86
                FlattenException::class => false,
10✔
87
            ];
10✔
88
        }
89

90
        return [];
28✔
91
    }
92

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

UNCOV
104
        return false;
×
105
    }
106
}
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