Passed
Push — master ( f13f78...5c1b24 )
by Ismayil
04:22
created

ConsoleInteractions::dumpRegisters()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 5
nop 0
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Elgg\Cli;
7
8
use Symfony\Component\Console\Question\Question;
9
10
trait ConsoleInteractions {
11
12
	/**
13
	 * @var InputInterface
14
	 */
15
	protected $input;
16
17
	/**
18
	 * @var OutputInterface
19
	 */
20
	protected $output;
21
22
	/**
23
	 * Ask a question
24
	 *
25
	 * @param string $question Question to ask
26
	 * @param mixed  $default  Default value
27
	 * @param bool   $hidden   Hide response
28
	 * @param bool   $required User input is required
29
	 *
30
	 * @return mixed
31
	 */
32
	public function ask($question, $default = null, $hidden = false, $required = true) {
33
34
		$helper = $this->getHelper('question');
0 ignored issues
show
Bug introduced by
It seems like getHelper() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35
36
		$q = new Question($question, $default);
37
38
		if ($hidden) {
39
			$q->setHidden(true);
40
			$q->setHiddenFallback(false);
41
		}
42
43
		if ($required) {
44
			$q->setValidator([
45
				$this,
46
				'assertNotEmpty'
47
			]);
48
			$q->setMaxAttempts(2);
49
		}
50
51
		return $helper->ask($this->input, $this->output, $q);
52
	}
53
54
	/**
55
	 * Write messages to output buffer
56
	 *
57
	 * @param string|array $messages Messages
58
	 *
59
	 * @return void
60
	 */
61
	public function write($messages) {
62
		$this->output->writeln($messages);
63
	}
64
65
	/**
66
	 * Returns option value
67
	 *
68
	 * @param string $name Option name
69
	 *
70
	 * @return mixed
71
	 */
72
	public function option($name) {
73
		return $this->input->getOption($name);
74
	}
75
76
	/**
77
	 * Returns argument value
78
	 *
79
	 * @param string $name Argument name
80
	 *
81
	 * @return string
82
	 */
83
	public function argument($name) {
84
		return $this->input->getArgument($name);
85
	}
86
87
	/**
88
	 * Question validator for required user response
89
	 *
90
	 * @param mixed $answer User answer
91
	 *
92
	 * @return bool
93
	 */
94
	public function assertNotEmpty($answer) {
95
		if (empty($answer)) {
96
			throw new \RuntimeException('Please enter a required answer');
97
		}
98
99
		return $answer;
100
	}
101
102
	/**
103
	 * Dump and output system and error messages
104
	 * @return void
105
	 */
106
	protected function dumpRegisters() {
107
		$registers = _elgg_services()->systemMessages->loadRegisters();
108
109
		foreach ($registers as $prop => $values) {
0 ignored issues
show
Bug introduced by
The expression $registers of type object<Elgg\SystemMessages\RegisterSet> is not traversable.
Loading history...
110
			if (!empty($values)) {
111
				foreach ($values as $msg) {
112
					$tag = $prop == 'error' ? 'error' : 'info';
113
					$this->write(elgg_format_element($tag, [], $msg));
114
				}
115
			}
116
		}
117
	}
118
}