Issues (31)

src/Command/GenerateApiCommand.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Command;
10
11
use Gorynych\Generator\ApiGenerator;
12
use HaydenPierce\ClassFinder\ClassFinder;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
19
final class GenerateApiCommand extends Command
20
{
21
    protected static $defaultName = 'gorynych:generate-api';
22
23
    private ApiGenerator $apiGenerator;
24
25
    public function __construct(ApiGenerator $apiGenerator)
26
    {
27
        parent::__construct();
28
29
        $this->apiGenerator = $apiGenerator;
30
    }
31
32
    protected function configure(): void
33
    {
34
        $this
35
            ->setDescription('Generates basic API for existing resources.')
36
            ->addArgument('resourceNamespace', InputArgument::REQUIRED, 'Namespace of application resources');
37
    }
38
39
    protected function execute(InputInterface $input, OutputInterface $output): int
40
    {
41
        $apiGenerator = $this->apiGenerator;
42
        $resources = ClassFinder::getClassesInNamespace($input->getArgument('resourceNamespace'), ClassFinder::RECURSIVE_MODE);
0 ignored issues
show
It seems like $input->getArgument('resourceNamespace') can also be of type string[]; however, parameter $namespace of HaydenPierce\ClassFinder...getClassesInNamespace() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $resources = ClassFinder::getClassesInNamespace(/** @scrutinizer ignore-type */ $input->getArgument('resourceNamespace'), ClassFinder::RECURSIVE_MODE);
Loading history...
43
44
        $io = new SymfonyStyle($input, $output);
45
        $io->progressStart();
46
47
        array_walk(
48
            $resources,
49
            static function (string $resource) use ($apiGenerator, $io): void {
50
                $resourceReflection = new \ReflectionClass($resource);
51
52
                if (true === $resourceReflection->isInterface() || true === $resourceReflection->isAbstract()) {
53
                    return;
54
                }
55
56
                $apiGenerator->generate($resourceReflection);
57
                $io->progressAdvance();
58
            }
59
        );
60
61
        $io->progressFinish();
62
        $io->success('API generation done');
63
64
        return 0;
65
    }
66
}
67