Completed
Pull Request — master (#80)
by Tom
03:00
created

GenerateCommand::execute()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.6257
c 0
b 0
f 0
cc 6
nc 32
nop 2
1
<?php
2
3
namespace Spatie\SchemaOrg\Generator\Console;
4
5
use Spatie\SchemaOrg\Generator\Definitions;
6
use Symfony\Component\Console\Command\Command;
7
use Spatie\SchemaOrg\Generator\PackageGenerator;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class GenerateCommand extends Command
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('generate')
18
            ->setDescription('Generate the package code from the schema.org docs')
19
            ->addOption('local', 'l', InputOption::VALUE_NONE, 'Use a cached version of the source')
20
            ->addOption('rdfa', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Link to RDFA file which extends schema.org to include')
21
            ->addOption('with-auto', null, InputOption::VALUE_NONE, 'Include the https://auto.schema.org extension')
22
            ->addOption('with-bib', null, InputOption::VALUE_NONE, 'Include the https://bib.schema.org extension')
23
            ->addOption('with-health-lifesci', null, InputOption::VALUE_NONE, 'Include the https://health-lifesci.schema.org extension');
24
    }
25
26
    /**
27
     * @param \Symfony\Component\Console\Input\InputInterface $input
28
     * @param \Symfony\Component\Console\Output\OutputInterface $output
29
     *
30
     * @return int
31
     */
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        $output->writeln('Generating package code...');
35
36
        $generator = new PackageGenerator();
37
38
        $sources = [
39
            'core' => 'https://raw.githubusercontent.com/schemaorg/schemaorg/master/data/schema.rdfa',
40
        ];
41
42
        if ($input->getOption('with-auto')) {
43
            $sources['auto'] = 'https://raw.githubusercontent.com/schemaorg/schemaorg/master/data/ext/auto/auto.rdfa';
44
        }
45
46
        if ($input->getOption('with-bib')) {
47
            $sources['bsdo'] = 'https://raw.githubusercontent.com/schemaorg/schemaorg/master/data/ext/bib/bsdo-1.0.rdfa';
48
            $sources['comics'] = 'https://raw.githubusercontent.com/schemaorg/schemaorg/master/data/ext/bib/comics.rdfa';
49
        }
50
51
        if ($input->getOption('with-health-lifesci')) {
52
            $sources['med-health-core'] = 'https://raw.githubusercontent.com/schemaorg/schemaorg/master/data/ext/health-lifesci/med-health-core.rdfa';
53
            $sources['physical-activity-and-exercise'] = 'https://raw.githubusercontent.com/schemaorg/schemaorg/master/data/ext/health-lifesci/physical-activity-and-exercise.rdfa';
54
        }
55
56
        foreach ($input->getOption('rdfa') as $rdfa) {
0 ignored issues
show
Bug introduced by
The expression $input->getOption('rdfa') of type string|array<integer,string>|boolean|null 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...
57
            $sources[pathinfo($rdfa, PATHINFO_FILENAME)] = $rdfa;
58
        }
59
60
        $sources = array_unique($sources);
61
62
        $definitions = new Definitions($sources);
63
64
        if (! $input->getOption('local')) {
65
            $definitions->preload();
66
        }
67
68
        $generator->generate($definitions);
69
70
        $output->writeln('Done!');
71
72
        return 0;
73
    }
74
}
75