Completed
Push — master ( 6ebd1c...df9bae )
by John
10:14
created

GenerateResourceClassesCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\SwaggerBundle\Dev\Command;
9
10
use KleijnWeb\SwaggerBundle\Dev\Generator\ResourceGenerator;
11
use KleijnWeb\SwaggerBundle\Document\DocumentRepository;
12
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Finder\Finder;
18
use Symfony\Component\HttpKernel\KernelInterface;
19
20
/**
21
 * @author John Kleijn <[email protected]>
22
 */
23
class GenerateResourceClassesCommand extends ContainerAwareCommand
24
{
25
    const NAME = 'swagger:generate:resources';
26
27
    /**
28
     * @var ResourceGenerator
29
     */
30
    private $generator;
31
32
    /**
33
     * @var DocumentRepository
34
     */
35
    private $documentRepository;
36
37
    /**
38
     * @param DocumentRepository $documentRepository
39
     * @param ResourceGenerator  $generator
40
     */
41
    public function __construct(DocumentRepository $documentRepository, ResourceGenerator $generator)
42
    {
43
        parent::__construct(self::NAME);
44
45
        $this
46
            ->setDescription('Generate DTO-like classes using the resource schema definitions in a swagger document')
47
            ->setHelp('This is a development tool and will only work with require-dev dependencies included')
48
            ->addArgument('file', InputArgument::REQUIRED, 'File path to the Swagger document')
49
            ->addArgument('bundle', InputArgument::REQUIRED, 'Name of the bundle you want the classes in')
50
            ->addOption(
51
                'namespace',
52
                null,
53
                InputOption::VALUE_REQUIRED,
54
                'Namespace of the classes to generate (relative to the bundle namespace)',
55
                'Model\Resources'
56
            );
57
58
        $this->documentRepository = $documentRepository;
59
        $this->generator = $generator;
60
    }
61
62
63
    /**
64
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
65
     *
66
     * @param InputInterface  $input
67
     * @param OutputInterface $output
68
     *
69
     * @return void
70
     */
71
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73
        /** @var KernelInterface $kernel */
74
        $kernel = $this->getContainer()->get('kernel');
75
        $bundle = $kernel->getBundle($input->getArgument('bundle'));
76
        $document = $this->documentRepository->get($input->getArgument('file'));
77
        $this->generator->setSkeletonDirs(__DIR__ . '/../Resources/skeleton');
0 ignored issues
show
Documentation introduced by
__DIR__ . '/../Resources/skeleton' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        $this->generator->generate($bundle, $document, $input->getOption('namespace'));
0 ignored issues
show
Bug introduced by
It seems like $bundle defined by $kernel->getBundle($input->getArgument('bundle')) on line 75 can also be of type array<integer,object<Sym...undle\BundleInterface>>; however, KleijnWeb\SwaggerBundle\...ceGenerator::generate() does only seem to accept object<Symfony\Component...Bundle\BundleInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
79
    }
80
}
81