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

api-platform / core / 10729306835

05 Sep 2024 10:46PM UTC coverage: 7.655% (-0.01%) from 7.665%
10729306835

push

github

web-flow
Merge pull request #6586 from soyuka/merge-342

Merge 3.4

0 of 54 new or added lines in 12 files covered. (0.0%)

8760 existing lines in 277 files now uncovered.

12505 of 163357 relevant lines covered (7.66%)

22.84 hits per line

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

94.44
/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
    public function __construct(private ?array $distinctFormats)
25
    {
UNCOV
26
    }
1,847✔
27

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

UNCOV
34
        if (!isset($prefix)) {
36✔
UNCOV
35
            $prefix = (new \ReflectionClass($className))->getShortName();
36✔
36
        }
37

UNCOV
38
        if (null !== $inputOrOutputClass && $className !== $inputOrOutputClass) {
36✔
UNCOV
39
            $parts = explode('\\', $inputOrOutputClass);
36✔
UNCOV
40
            $shortName = end($parts);
36✔
UNCOV
41
            $prefix .= '.'.$shortName;
36✔
42
        }
43

UNCOV
44
        if ('json' !== $format && ($this->distinctFormats[$format] ?? false)) {
36✔
45
            // JSON is the default, and so isn't included in the definition name
UNCOV
46
            $prefix .= '.'.$format;
36✔
47
        }
48

UNCOV
49
        $definitionName = $serializerContext[SchemaFactory::OPENAPI_DEFINITION_NAME] ?? null;
36✔
UNCOV
50
        if ($definitionName) {
36✔
51
            $name = \sprintf('%s-%s', $prefix, $definitionName);
×
52
        } else {
UNCOV
53
            $groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []);
36✔
UNCOV
54
            $name = $groups ? \sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix;
36✔
55
        }
56

UNCOV
57
        return $this->encodeDefinitionName($name);
36✔
58
    }
59

60
    private function encodeDefinitionName(string $name): string
61
    {
UNCOV
62
        return preg_replace('/[^a-zA-Z0-9.\-_]/', '.', $name);
36✔
63
    }
64
}
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