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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

100.0
/src/JsonSchema/DefinitionNameFactory.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\JsonSchema;
15

16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
18
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
19

20
final class DefinitionNameFactory implements DefinitionNameFactoryInterface
21
{
22
    use ResourceClassInfoTrait;
23

24
    private const GLUE = '.';
25
    private array $prefixCache = [];
26

27
    public function __construct(private ?array $distinctFormats)
28
    {
UNCOV
29
    }
857✔
30

31
    public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = []): string
32
    {
UNCOV
33
        if ($operation) {
94✔
UNCOV
34
            $prefix = $operation->getShortName();
69✔
35
        }
36

UNCOV
37
        if (!isset($prefix)) {
94✔
UNCOV
38
            $prefix = $this->createPrefixFromClass($className);
64✔
39
        }
40

UNCOV
41
        if (null !== $inputOrOutputClass && $className !== $inputOrOutputClass) {
94✔
UNCOV
42
            $parts = explode('\\', $inputOrOutputClass);
24✔
UNCOV
43
            $shortName = end($parts);
24✔
UNCOV
44
            $prefix .= self::GLUE.$shortName;
24✔
45
        }
46

UNCOV
47
        if ('json' !== $format && ($this->distinctFormats[$format] ?? false)) {
94✔
48
            // JSON is the default, and so isn't included in the definition name
UNCOV
49
            $prefix .= self::GLUE.$format;
86✔
50
        }
51

UNCOV
52
        $definitionName = $serializerContext[SchemaFactory::OPENAPI_DEFINITION_NAME] ?? null;
94✔
UNCOV
53
        if (null !== $definitionName) {
94✔
UNCOV
54
            $name = \sprintf('%s%s', $prefix, $definitionName ? '-'.$definitionName : $definitionName);
25✔
55
        } else {
UNCOV
56
            $groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []);
90✔
UNCOV
57
            $name = $groups ? \sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix;
90✔
58
        }
59

UNCOV
60
        return $this->encodeDefinitionName($name);
94✔
61
    }
62

63
    private function encodeDefinitionName(string $name): string
64
    {
UNCOV
65
        return preg_replace('/[^a-zA-Z0-9.\-_]/', '.', $name);
94✔
66
    }
67

68
    private function createPrefixFromClass(string $fullyQualifiedClassName, int $namespaceParts = 1): string
69
    {
UNCOV
70
        $parts = explode('\\', $fullyQualifiedClassName);
64✔
UNCOV
71
        $name = implode(self::GLUE, \array_slice($parts, -$namespaceParts));
64✔
72

UNCOV
73
        if (!isset($this->prefixCache[$name])) {
64✔
UNCOV
74
            $this->prefixCache[$name] = $fullyQualifiedClassName;
64✔
75

UNCOV
76
            return $name;
64✔
77
        }
78

UNCOV
79
        if ($this->prefixCache[$name] !== $fullyQualifiedClassName) {
29✔
UNCOV
80
            $name = $this->createPrefixFromClass($fullyQualifiedClassName, ++$namespaceParts);
13✔
81
        }
82

UNCOV
83
        return $name;
29✔
84
    }
85
}
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