Passed
Pull Request — master (#26)
by Maciej
03:01
created

bin/Commands/LanguagesCommand.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Highlighter
7
 *
8
 * Copyright (C) 2016, Some right reserved.
9
 *
10
 * @author Kacper "Kadet" Donat <[email protected]>
11
 *
12
 * Contact with author:
13
 * Xmpp: [email protected]
14
 * E-mail: [email protected]
15
 *
16
 * From Kadet with love.
17
 */
18
19
namespace Kadet\Highlighter\bin\Commands;
20
21
use Kadet\Highlighter\KeyLighter;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Helper\Table;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
29
class LanguagesCommand extends Command
30
{
31
    protected $types = ['name', 'mime', 'filename'];
32
33
    protected function configure()
34
    {
35
        $this->setName('languages')
36
            ->addArgument('by', InputArgument::OPTIONAL, 'Alias type, one of ' . implode(', ', array_map(function ($f) {
37
                return "<info>{$f}</info>";
38
            }, $this->types)), 'name')
39
            ->addOption('no-group', 'g', InputOption::VALUE_NONE, 'Do not group languages by type')
40
            ->addOption('classes', 'c', InputOption::VALUE_NONE, 'Return fully qualified class names instead of identifiers')
41
            ->addOption('headerless', 'l', InputOption::VALUE_NONE, 'Output table without headers, useful for parsing')
42
            ->setDescription('Lists available languages')
43
        ;
44
    }
45
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $languages = KeyLighter::get()->registeredLanguages($input->getArgument('by'), $input->getOption('classes'));
49
50
        $table = new Table($output);
51
52
        if (!$input->getOption('headerless')) {
53
            $table->setHeaders([ucfirst($input->getArgument('by')), $input->getOption('classes') ? 'Class name' : 'Language']);
0 ignored issues
show
It seems like $input->getArgument('by') can also be of type string[]; however, parameter $str of ucfirst() 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

53
            $table->setHeaders([ucfirst(/** @scrutinizer ignore-type */ $input->getArgument('by')), $input->getOption('classes') ? 'Class name' : 'Language']);
Loading history...
54
        }
55
56
        $table->setRows(array_map(function ($language) {
57
            return [
58
                implode(', ', array_map(function ($f) {
59
                    return "<comment>{$f}</comment>";
60
                }, $language['aliases'])),
61
                $language['class']
62
            ];
63
        }, $input->getOption('no-group') ? $this->processNonGrouped($languages) : $this->processGrouped($languages)));
64
65
        $table->setStyle($input->getOption('headerless') ? 'compact' : 'borderless');
66
        $table->render();
67
    }
68
69
    protected function processGrouped($languages)
70
    {
71
        $result = [];
72
        foreach ($languages as $alias => $class) {
73
            if (!isset($result[$class])) {
74
                $result[$class] = ['aliases' => [], 'class' => $class];
75
            }
76
77
            $result[$class]['aliases'][] = $alias;
78
        }
79
80
        return $result;
81
    }
82
83
    protected function processNonGrouped($languages)
84
    {
85
        $result = [];
86
        foreach ($languages as $alias => $class) {
87
            $result[] = ['aliases' => [$alias], 'class' => $class];
88
        }
89
90
        return $result;
91
    }
92
}
93