InfoCommand::execute()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.3946
c 0
b 0
f 0
cc 7
nc 12
nop 2
1
<?php
2
3
/*
4
 * This file is part of Bowerphp.
5
 *
6
 * (c) Massimiliano Arione <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bowerphp\Command;
13
14
use Bowerphp\Package\Package;
15
use Bowerphp\Util\PackageNameVersionExtractor;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Info
22
 */
23
class InfoCommand extends Command
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('info')
32
            ->setDescription('Displays overall information of a package or of a particular version')
33
            ->addArgument('package', InputArgument::REQUIRED, 'Choose a package.')
34
            ->addArgument('property', InputArgument::OPTIONAL, 'A property present in bower.json.')
35
            ->setHelp(<<<'EOT'
36
The <info>%command.name%</info> command displays overall information of a package or of a particular version.
37
If you pass a property present in bower.json, you can get the correspondent value.
38
39
  <info>php %command.full_name% package</info>
40
EOT
41
            )
42
        ;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $this->setGithubToken($output);
51
52
        $packageName = $input->getArgument('package');
53
        $property = $input->getArgument('property');
54
55
        $packageNameVersion = PackageNameVersionExtractor::fromString($packageName);
56
57
        $package = new Package($packageNameVersion->name, $packageNameVersion->version);
58
        $bowerphp = $this->getBowerphp($output);
59
60
        $bowerJsonFile = $bowerphp->getPackageBowerFile($package);
61
        if ('*' == $packageNameVersion->version) {
62
            $versions = $bowerphp->getPackageInfo($package, 'versions');
63
        }
64
        if (!is_null($property)) {
65
            $bowerArray = json_decode($bowerJsonFile, true);
66
            $propertyValue = isset($bowerArray[$property]) ? $bowerArray[$property] : '';
67
            $this->consoleOutput->writelnJsonText($propertyValue);
68
69
            return;
70
        }
71
        $this->consoleOutput->writelnJson($bowerJsonFile);
72
        if ('*' != $packageNameVersion->version) {
73
            return;
74
        }
75
        $output->writeln('');
76
        if (empty($versions)) {
77
            $output->writeln('No versions available.');
78
        } else {
79
            $output->writeln('<fg=cyan>Available versions:</fg=cyan>');
80
            foreach ($versions as $vrs) {
0 ignored issues
show
Bug introduced by
The expression $versions of type string|array 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...
81
                $output->writeln("- $vrs");
82
            }
83
        }
84
    }
85
}
86