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

engine/classes/Elgg/Cli/ConsoleInteractions.php (1 issue)

Check that a foreach expression is traversable

Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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');
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
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
}