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

api-platform / core / 20894952430

11 Jan 2026 12:14PM UTC coverage: 20.145% (-8.7%) from 28.875%
20894952430

Pull #7667

github

VincentLanglet
Add failing test
Pull Request #7667: POC NameConverterAwareInterface

11577 of 57467 relevant lines covered (20.15%)

27.48 hits per line

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

88.24
/src/Metadata/Resource/Factory/ObjectMapperMetadataCollectionFactory.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\Metadata\Resource\Factory;
15

16
use ApiPlatform\Doctrine\Odm\State\Options as OdmOptions;
17
use ApiPlatform\Doctrine\Orm\State\Options;
18
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
19
use Symfony\Component\ObjectMapper\Metadata\Mapping;
20
use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface;
21

22
class ObjectMapperMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
23
{
24
    public function __construct(
25
        private readonly ResourceMetadataCollectionFactoryInterface $decorated,
26
        private readonly ObjectMapperMetadataFactoryInterface $objectMapperMetadata,
27
    ) {
28
    }
826✔
29

30
    public function create(string $resourceClass): ResourceMetadataCollection
31
    {
32
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
33✔
33

34
        foreach ($resourceMetadataCollection as $key => $resourceMetadata) {
33✔
35
            $operations = $resourceMetadata->getOperations();
30✔
36

37
            if (!$operations) {
30✔
38
                continue;
×
39
            }
40

41
            foreach ($operations as $operationKey => $operation) {
30✔
42
                if (null !== $operation->canMap()) {
30✔
43
                    continue;
1✔
44
                }
45

46
                $entityClass = null;
30✔
47
                if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
30✔
48
                    $entityClass = $options->getEntityClass();
3✔
49
                }
50

51
                if (($options = $operation->getStateOptions()) && $options instanceof OdmOptions && $options->getDocumentClass()) {
30✔
52
                    $entityClass = $options->getDocumentClass();
1✔
53
                }
54

55
                $class = $operation->getInput()['class'] ?? $operation->getClass();
30✔
56
                $outputClass = $operation->getOutput()['class'] ?? null;
30✔
57
                $entityMap = null;
30✔
58

59
                // Look for Mapping metadata
60
                if ($this->canBeMapped($class) || ($outputClass && $this->canBeMapped($outputClass)) || ($entityClass && ($entityMap = $this->canBeMapped($entityClass)))) {
30✔
61
                    $found = true;
2✔
62
                    if ($entityMap) {
2✔
63
                        foreach ($entityMap as $mapping) {
1✔
64
                            if ($found = ($mapping->source === $operation->getClass() || $mapping->target === $operation->getClass())) {
1✔
65
                                break;
×
66
                            }
67
                        }
68
                    }
69

70
                    if (!$found) {
2✔
71
                        continue;
1✔
72
                    }
73

74
                    $operations->add($operationKey, $operation->withMap(true));
2✔
75
                }
76
            }
77

78
            $resourceMetadataCollection[$key] = $resourceMetadata->withOperations($operations);
30✔
79
        }
80

81
        return $resourceMetadataCollection;
33✔
82
    }
83

84
    /**
85
     * @return bool|list<Mapping>
86
     */
87
    private function canBeMapped(string $class): bool|array
88
    {
89
        try {
90
            $r = new \ReflectionClass($class);
30✔
91
            if (!$r->isInstantiable() || !($mapping = $this->objectMapperMetadata->create($r->newInstanceWithoutConstructor(), null, ['_api_check_can_be_mapped' => true]))) {
30✔
92
                return false;
30✔
93
            }
94
        } catch (\ReflectionException $e) {
×
95
            return false;
×
96
        }
97

98
        return $mapping;
2✔
99
    }
100
}
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

© 2026 Coveralls, Inc