Completed
Push — master ( b2a65d...63a169 )
by Kirill
02:18
created

src/Console/SDLCompileCommand.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\SDL\Console;
11
12
use Railt\Console\Command;
13
use Railt\Io\Exception\NotReadableException;
14
use Railt\Io\File;
15
use Railt\SDL\Compiler;
16
17
/**
18
 * Class SDLCompileCommand
19
 */
20
class SDLCompileCommand extends Command
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $signature = 'sdl:compile 
26
        {schema : GraphQL SDL file}
27
        {--out= : Output directory}';
28
29
    /**
30
     * @var string
31
     */
32
    protected $description = 'Parse and compile GraphQL SDL file';
33
34
    /**
35
     * @throws \Railt\Io\Exception\ExternalFileException
36
     * @throws \Railt\Io\Exception\NotReadableException
37
     * @throws \Railt\Reflection\Exception\TypeConflictException
38
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
39
     */
40
    public function handle(): void
41
    {
42
        $schema = File::fromPathname($this->argument('schema'));
0 ignored issues
show
It seems like $this->argument('schema') targeting Railt\Console\Command::argument() can also be of type array<integer,string>; however, Railt\Io\File::fromPathname() does only seem to accept string, 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...
43
44
        $document   = (new Compiler())->compile($schema);
45
        $dictionary = $document->getDictionary();
46
47
        $result = [];
48
49
        foreach ($dictionary->all() as $type) {
50
            $result[] = $type;
51
        }
52
53
        $output = $this->option('out') ?: \dirname($schema->getPathname());
54
55
        if (! \is_dir($output)) {
56
            throw new NotReadableException('Output directory "' . $output . '" not exists');
57
        }
58
59
        $outputPathname = $output . '/' . \basename($schema->getPathname()) . '.json';
60
61
        \file_put_contents($outputPathname, \json_encode($result));
62
    }
63
}
64