Failed Conditions
Push — master ( 5433f0...117895 )
by Alexander
01:52
created

AbstractCommand::isValidIssueKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 chobie\Jira\Api;
15
use chobie\Jira\Api\Result;
16
use ConsoleHelpers\ConsoleKit\Config\ConfigEditor;
17
use ConsoleHelpers\ConsoleKit\Command\AbstractCommand as BaseCommand;
18
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
19
20
/**
21
 * Base command class.
22
 */
23
abstract class AbstractCommand extends BaseCommand
24
{
25
26
	/**
27
	 * Config editor.
28
	 *
29
	 * @var ConfigEditor
30
	 */
31
	private $_configEditor;
32
33
	/**
34
	 * Jira REST client.
35
	 *
36
	 * @var Api
37
	 */
38
	protected $jiraApi;
39
40
	/**
41
	 * Prepare dependencies.
42
	 *
43
	 * @return void
44
	 */
45
	protected function prepareDependencies()
46
	{
47
		$container = $this->getContainer();
48
49
		$this->_configEditor = $container['config_editor'];
50
		$this->jiraApi = $container['jira_api'];
51
	}
52
53
	/**
54
	 * Returns command setting value.
55
	 *
56
	 * @param string $name Name.
57
	 *
58
	 * @return mixed
59
	 */
60
	protected function getSetting($name)
61
	{
62
		return $this->_configEditor->get($name);
63
	}
64
65
	/**
66
	 * Checks if an issue key is valid.
67
	 *
68
	 * @param string $issue_key Issue key.
69
	 *
70
	 * @return boolean
71
	 */
72
	public function isValidIssueKey($issue_key)
73
	{
74
		return preg_match('/^([A-Z]+-[0-9]+)$/', $issue_key);
75
	}
76
77
	/**
78
	 * Return possible values for the named argument
79
	 *
80
	 * @param string            $argumentName Argument name.
81
	 * @param CompletionContext $context      Completion context.
82
	 *
83
	 * @return array
84
	 */
85
	public function completeArgumentValues($argumentName, CompletionContext $context)
86
	{
87
		$ret = parent::completeArgumentValues($argumentName, $context);
88
89
		if ( $argumentName === 'project_key' ) {
90
			return $this->getProjectKeys();
91
		}
92
93
		return $ret;
94
	}
95
96
	/**
97
	 * Returns possible link names.
98
	 *
99
	 * @return array
100
	 */
101
	protected function getProjectKeys()
102
	{
103
		$ret = array();
104
		$response = $this->jiraApi->getProjects();
105
106
		if ( $response instanceof Result ) {
107
			$response = $response->getResult();
108
		}
109
110
		foreach ( $response as $project_data ) {
1 ignored issue
show
Bug introduced by
The expression $response 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...
111
			$ret[] = $project_data['key'];
112
		}
113
114
		return $ret;
115
	}
116
117
}
118