Completed
Pull Request — master (#89)
by Maxime
11:43
created

DumpJsEnumsCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 4
dl 0
loc 79
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A configure() 0 9 1
B execute() 0 41 5
1
<?php
2
3
/*
4
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum\Bridge\Symfony\Console\Command;
12
13
use Elao\Enum\JsDumper\JsDumper;
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
use Symfony\Component\Console\Style\SymfonyStyle;
20
21
class DumpJsEnumsCommand extends Command
22
{
23
    protected static $defaultName = 'elao:enum:dump-js';
24
25
    /**
26
     * @var array<class-string<EnumInterface>, string> Paths indexed by enum FQCN
27
     */
28
    private $enums;
29
30
    /** @var string|null */
31
    private $baseDir;
32
33
    /** @var string|null */
34
    private $libPath;
35
36
    /** @var JsDumper */
37
    private $dumper;
38
39
    public function __construct(array $enums = [], string $baseDir = null, string $libPath = null)
40
    {
41
        $this->enums = $enums;
42
        $this->baseDir = $baseDir;
43
        $this->libPath = $libPath;
44
45
        parent::__construct();
46
    }
47
48
    protected function configure()
49
    {
50
        $this
51
            ->setDescription('Generate javascript enums')
52
            ->addArgument('enums', InputArgument::IS_ARRAY, 'The enums & paths of the files where to generate the javascript enums. Format: "enum FQCN:path"')
53
            ->addOption('base-dir', null, InputOption::VALUE_REQUIRED, 'A prefixed dir used for relative paths supplied for each of the generated enums and library path', $this->baseDir)
54
            ->addOption('lib-path', null, InputOption::VALUE_REQUIRED, 'The path of the file were to place the javascript library sources used by the dumped enums.', $this->libPath)
55
        ;
56
    }
57
58
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        $io = new SymfonyStyle($input, $output);
61
62
        $io->title('Elao Enums Javascript generator');
63
64
        $io->note(<<<TXT
65
This command is not meant to be used as part of an automatic process updating your code.
66
There is no BC promise on the generated code. Once generated, the code belongs to you.
67
TXT
68
        );
69
70
        $libPath = $input->getOption('lib-path');
71
72
        if (!$libPath) {
73
            throw new \InvalidArgumentException('Please provide the "--lib-path" option');
74
        }
75
76
        $enums = $this->enums;
77
        if ($enumArgs = $input->getArgument('enums')) {
78
            $enums = [];
79
            foreach ($enumArgs as $arg) {
0 ignored issues
show
Bug introduced by
The expression $enumArgs of type string|array<integer,string> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
80
                list($fqcn, $path) = explode(':', $arg, 2);
81
                $enums[$fqcn] = $path;
82
            }
83
        }
84
85
        $this->dumper = new JsDumper($libPath, $input->getOption('base-dir'));
86
87
        $io->comment("Generating library sources at path <info>{$this->dumper->normalizePath($libPath)}</info>");
88
89
        $this->dumper->dumpLibrarySources();
90
91
        foreach ($enums as $fqcn => $path) {
92
            $shortName = (new \ReflectionClass($fqcn))->getShortName();
93
            $io->comment("Generating <info>$shortName</info> enum at path <info>{$this->dumper->normalizePath($path)}</info>");
94
            $this->dumper->dumpEnumToFile($fqcn, $path);
95
        }
96
97
        return 0;
98
    }
99
}
100