Failed Conditions
Push — master ( 033063...88b424 )
by Alexander
02:58
created

VersionsCommand::execute()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 34
ccs 0
cts 25
cp 0
rs 8.439
cc 5
eloc 19
nc 8
nop 2
crap 30
1
<?php
2
/**
3
 * This file is part of the Jira-CLI library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/jira-cli
9
 */
10
11
namespace ConsoleHelpers\JiraCLI\Command;
12
13
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class VersionsCommand extends AbstractCommand
18
{
19
20
	/**
21
	 * {@inheritdoc}
22
	 */
23
	protected function configure()
24
	{
25
		$this
26
			->setName('versions')
27
			->setDescription('Shows version usage across projects');
28
	}
29
30
	/**
31
	 * {@inheritdoc}
32
	 */
33
	protected function execute(InputInterface $input, OutputInterface $output)
34
	{
35
		$versions = array();
36
37
		$this->io->write('Getting projects ... ');
38
		$projects = $this->jiraApi->getProjects()->getResult();
39
		$this->io->writeln('done (' . count($projects) . ' found)');
40
41
		foreach ( $projects as $index => $project_data ) {
42
			$project_key = $project_data['key'];
43
44
			$this->io->write('Getting project <info>#' . $index . '</info> versions ... ');
45
			$project_versions = $this->jiraApi->getVersions($project_key);
46
			$this->io->writeln('done (' . count($project_versions) . ' found)');
47
48
			foreach ( $project_versions as $project_version_data ) {
0 ignored issues
show
Bug introduced by
The expression $project_versions of type array|object<chobie\Jira\Api\Result>|false 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...
49
				$project_version = $project_version_data['name'];
50
51
				// Interested only in final releases.
52
				if ( strpos($project_version, '-') === false ) {
53
					$versions[$project_version] = true;
54
				}
55
			}
56
		}
57
58
		$versions = array_keys($versions);
59
		usort($versions, 'version_compare');
60
61
		$this->io->writeln(array('', 'Versions:'));
62
63
		foreach ( $versions as $version ) {
64
			$this->io->writeln(' * ' . $version);
65
		}
66
	}
67
68
}
69