Failed Conditions
Push — master ( 2cd9b5...479acf )
by Alexander
01:46
created

VersionsCommand::getProjectVersionsRaw()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 6
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 ConsoleHelpers\JiraCLI\JiraApi;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class VersionsCommand extends AbstractCommand
19
{
20
21
	/**
22
	 * {@inheritdoc}
23
	 */
24
	protected function configure()
25
	{
26
		$this
27
			->setName('versions')
28
			->setDescription('Shows version usage across projects');
29
	}
30
31
	/**
32
	 * {@inheritdoc}
33
	 */
34
	protected function execute(InputInterface $input, OutputInterface $output)
35
	{
36
		$versions = array();
37
38
		$this->io->write('Getting projects ... ');
39
		$project_keys = $this->jiraApi->getProjectKeys();
40
		$this->io->writeln('done (' . count($project_keys) . ' found)');
41
42
		foreach ( $project_keys as $project_key ) {
43
			$this->io->write('Getting project <info>' . $project_key . '</info> versions ... ');
44
			$project_versions = $this->jiraApi->getVersions($project_key, JiraApi::CACHE_DURATION_ONE_MONTH);
45
			$this->io->writeln('done (' . count($project_versions) . ' found)');
46
47
			foreach ( $project_versions as $project_version_data ) {
0 ignored issues
show
Bug introduced by
The expression $project_versions of type array|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...
48
				$project_version = $project_version_data['name'];
49
50
				// Interested only in final releases.
51
				if ( strpos($project_version, '-') === false ) {
52
					$versions[$project_version] = true;
53
				}
54
			}
55
		}
56
57
		$versions = array_keys($versions);
58
		usort($versions, 'version_compare');
59
60
		$this->io->writeln(array('', 'Versions:'));
61
62
		foreach ( $versions as $version ) {
63
			$this->io->writeln(' * ' . $version);
64
		}
65
	}
66
67
}
68