CompileCommand::execute()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 9.4222
c 0
b 0
f 0
cc 5
nc 8
nop 2
crap 30
1
<?php
2
/**
3
 * This file is part of Railt 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
declare(strict_types=1);
9
10
namespace Railt\Compiler\Console;
11
12
use Railt\Compiler\Compiler;
13
use Railt\Io\File;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Class CompileCommand
22
 */
23
class CompileCommand extends Command
24
{
25
    /**
26
     * @param InputInterface $in
27
     * @param OutputInterface $out
28
     * @throws \Railt\Io\Exception\ExternalFileException
29
     * @throws \Railt\Io\Exception\NotReadableException
30
     * @throws \Throwable
31
     */
32
    public function execute(InputInterface $in, OutputInterface $out): void
33
    {
34
        $compiler = Compiler::load(File::fromPathname($in->getArgument('grammar')));
0 ignored issues
show
Bug introduced by
It seems like \Railt\Io\File::fromPath...getArgument('grammar')) targeting Railt\Io\File::fromPathname() can also be of type object<Railt\Io\File>; however, Railt\Compiler\Compiler::load() does only seem to accept object<Railt\Io\Readable>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
35
36
        if ($in->getOption('namespace')) {
37
            $compiler->setNamespace($in->getOption('namespace'));
38
        }
39
40
        if ($in->getOption('class')) {
41
            $compiler->setClassName($in->getOption('class'));
42
        }
43
44
        $cwd = \getcwd() ?: __DIR__ . '/..';
45
46
        $compiler->saveTo($in->getOption('dir') ?: $cwd);
47
    }
48
49
    /**
50
     * @return void
51
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
52
     * @throws \Symfony\Component\Console\Exception\LogicException
53
     */
54
    protected function configure(): void
55
    {
56
        $this->setName('compiler:compile');
57
        $this->setDescription('Builds a parser from pp2 grammar file.');
58
59
        $this->addArgument('grammar', InputArgument::REQUIRED,
60
            'Path to pp2 grammar file');
61
62
        $this->addOption('dir', 'd', InputOption::VALUE_OPTIONAL,
63
            'Output parser directory');
64
65
        $this->addOption('class', 'c', InputOption::VALUE_OPTIONAL,
66
            'Output parser class name');
67
68
        $this->addOption('namespace', 'ns', InputOption::VALUE_OPTIONAL,
69
            'Output parser namespace');
70
    }
71
}
72