• 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

95.56
/src/Symfony/Bundle/Command/DebugResourceCommand.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\Symfony\Bundle\Command;
15

16
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
17
use Symfony\Component\Console\Attribute\AsCommand;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Helper\QuestionHelper;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Question\ChoiceQuestion;
24
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
25

26
#[AsCommand(name: 'debug:api-resource')]
27
final class DebugResourceCommand extends Command
28
{
29
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ClonerInterface $cloner, private $dumper)
30
    {
UNCOV
31
        parent::__construct();
4✔
32
    }
33

34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function configure(): void
38
    {
UNCOV
39
        $this
4✔
UNCOV
40
            ->setDescription('Debug API Platform resources')
4✔
UNCOV
41
            ->addArgument('class', InputArgument::REQUIRED, 'The class you want to debug');
4✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output): int
48
    {
UNCOV
49
        $resourceClass = $input->getArgument('class');
4✔
50

UNCOV
51
        $resourceCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
4✔
52

UNCOV
53
        if (0 === \count($resourceCollection)) {
3✔
54
            $output->writeln(\sprintf('<error>No resources found for class %s</error>', $resourceClass));
×
55

56
            return \defined(Command::class.'::INVALID') ? Command::INVALID : 2;
×
57
        }
58

UNCOV
59
        $shortName = (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass;
3✔
60

61
        /** @var QuestionHelper $helper */
UNCOV
62
        $helper = $this->getHelper('question');
3✔
63

UNCOV
64
        $resources = [];
3✔
UNCOV
65
        foreach ($resourceCollection as $resource) {
3✔
UNCOV
66
            if ($resource->getUriTemplate()) {
3✔
UNCOV
67
                $resources[] = ($resource->getRoutePrefix() ?? '').$resource->getUriTemplate();
2✔
UNCOV
68
                continue;
2✔
69
            }
70

UNCOV
71
            foreach ($resource->getOperations() as $operation) {
3✔
UNCOV
72
                if ($operation->getUriTemplate()) {
3✔
UNCOV
73
                    $resources[] = ($resource->getRoutePrefix() ?? '').$operation->getUriTemplate();
1✔
UNCOV
74
                    break;
1✔
75
                }
76
            }
77
        }
78

UNCOV
79
        if (\count($resourceCollection) > 1) {
3✔
UNCOV
80
            $questionResource = new ChoiceQuestion(
2✔
UNCOV
81
                \sprintf('There are %d resources declared on the class %s, which one do you want to debug ? ', \count($resourceCollection), $shortName).\PHP_EOL,
2✔
UNCOV
82
                $resources
2✔
UNCOV
83
            );
2✔
84

UNCOV
85
            $answerResource = $helper->ask($input, $output, $questionResource);
2✔
UNCOV
86
            $resourceIndex = array_search($answerResource, $resources, true);
2✔
UNCOV
87
            $selectedResource = $resourceCollection[$resourceIndex];
2✔
88
        } else {
UNCOV
89
            $selectedResource = $resourceCollection[0];
1✔
UNCOV
90
            $output->writeln(\sprintf('Class %s declares 1 resource.', $shortName).\PHP_EOL);
1✔
91
        }
92

UNCOV
93
        $operations = ['Debug the resource itself'];
3✔
UNCOV
94
        foreach ($selectedResource->getOperations() as $operationName => $operation) {
3✔
UNCOV
95
            $operations[] = $operationName;
3✔
96
        }
97

UNCOV
98
        $questionOperation = new ChoiceQuestion(
3✔
UNCOV
99
            \sprintf('There are %d operation%s declared on the resource, which one do you want to debug ? ', $selectedResource->getOperations()->count(), $selectedResource->getOperations()->count() > 1 ? 's' : '').\PHP_EOL,
3✔
UNCOV
100
            $operations
3✔
UNCOV
101
        );
3✔
102

UNCOV
103
        $answerOperation = $helper->ask($input, $output, $questionOperation);
3✔
UNCOV
104
        if ('Debug the resource itself' === $answerOperation) {
3✔
UNCOV
105
            $this->dumper->dump($this->cloner->cloneVar($selectedResource));
1✔
UNCOV
106
            $output->writeln('Successfully dumped the selected resource');
1✔
107

UNCOV
108
            return \defined(Command::class.'::SUCCESS') ? Command::SUCCESS : 0;
1✔
109
        }
110

UNCOV
111
        $this->dumper->dump($this->cloner->cloneVar($resourceCollection->getOperation($answerOperation)));
2✔
UNCOV
112
        $output->writeln('Successfully dumped the selected operation');
2✔
113

UNCOV
114
        return \defined(Command::class.'::SUCCESS') ? Command::SUCCESS : 0;
2✔
115
    }
116
}
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