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

api-platform / core / 11182119487

04 Oct 2024 02:50PM UTC coverage: 7.837% (+0.4%) from 7.441%
11182119487

push

github

soyuka
Merge 4.0

0 of 266 new or added lines in 25 files covered. (0.0%)

9900 existing lines in 360 files now uncovered.

12939 of 165109 relevant lines covered (7.84%)

27.02 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
    }
2,343✔
30

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

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

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

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

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

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

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

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

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

UNCOV
76
            return $name;
183✔
77
        }
78

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

UNCOV
83
        return $name;
72✔
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