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

src/Console/SDLAnalyzeCommand.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\File;
14
use Railt\Lexer\TokenInterface;
15
use Railt\SDL\Compiler;
16
use Railt\SDL\Console\Language\JsonLanguage;
17
18
/**
19
 * Class SDLAnalyzeCommand
20
 */
21
class SDLAnalyzeCommand extends Command
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $signature = 'sdl:analyze 
27
        {schema : GraphQL SDL file}
28
        {--type= : Render only given type name}';
29
30
    /**
31
     * @var string
32
     */
33
    protected $description = 'Parse and render GraphQL SDL structure in JSON format';
34
35
    /**
36
     * @throws \InvalidArgumentException
37
     * @throws \Railt\Io\Exception\ExternalFileException
38
     * @throws \Railt\Io\Exception\NotReadableException
39
     * @throws \Railt\Lexer\Exception\BadLexemeException
40
     * @throws \Railt\Reflection\Exception\TypeConflictException
41
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
42
     */
43
    public function handle(): void
44
    {
45
        $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...
46
47
        $document   = (new Compiler())->compile($schema);
48
        $dictionary = $document->getDictionary();
49
50
        if ($name = $this->option('type')) {
51
            if ($type = $dictionary->find(\trim($name))) {
52
                $this->render($type);
53
            } else {
54
                $this->error(\vsprintf('Type "%s" not found in file %s or could not be loaded', [
55
                    \trim($name),
56
                    $schema->getPathname(),
57
                ]));
58
            }
59
60
            return;
61
        }
62
63
        foreach ($dictionary->all() as $def) {
64
            if ($def->isBuiltin()) {
65
                continue;
66
            }
67
68
            $position = $def->getFile()->getPathname() . ':' . $def->getLine();
69
70
            $this->writeln('<fg=white;bg=green> ' . \str_repeat(' ', \strlen($position)) . ' </>');
71
            $this->writeln('<fg=white;bg=green> ' . $position . ' </>');
72
            $this->writeln('<fg=white;bg=green> ' . \str_repeat(' ', \strlen($position)) . ' </>');
73
74
            $this->render($def);
75
            $this->writeln("\n\n");
76
        }
77
    }
78
79
    /**
80
     * @param $value
81
     * @throws \InvalidArgumentException
82
     * @throws \Railt\Lexer\Exception\BadLexemeException
83
     */
84
    private function render($value): void
85
    {
86
        $string = \json_encode($value, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE);
87
        $json   = new JsonLanguage();
88
89
        /** @var TokenInterface $token */
90
        foreach ($json->lex(File::fromSources($string, 'output.json')) as $token) {
91
            $this->write($json->highlight($token->getName(), $token->getValue()));
92
        }
93
    }
94
}
95