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

engine/classes/Elgg/Cli/Command.php (2 issues)

assigning incompatible types to properties.

Bug Documentation 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
namespace Elgg\Cli;
4
5
use Elgg\Http\OutputBufferTransport;
6
use RuntimeException;
7
use Symfony\Component\Console\Command\Command as SymfonyCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Abstract command with some utility methods
13
 */
14
abstract class Command extends SymfonyCommand {
15
16
	use ConsoleInteractions;
17
18
	/**
19
	 * {@inheritdoc}
20
	 */
21
	final public function execute(InputInterface $input, OutputInterface $output) {
22
		$this->input = $input;
0 ignored issues
show
Documentation Bug introduced by
It seems like $input of type object<Symfony\Component...e\Input\InputInterface> is incompatible with the declared type object<Elgg\Cli\InputInterface> of property $input.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
		$this->output = $output;
0 ignored issues
show
Documentation Bug introduced by
It seems like $output of type object<Symfony\Component...Output\OutputInterface> is incompatible with the declared type object<Elgg\Cli\OutputInterface> of property $output.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
24
25
		elgg_set_config('debug', 'NOTICE');
26
27
		_elgg_services()->logger->setPrinter(function ($data, $level) {
28
			$tag = $level == 'ERROR' ? 'error' : 'info';
29
			$this->write(elgg_format_element($tag, [], $data));
30
		});
31
32
		_elgg_services()->responseFactory->setTransport(new ResponseTransport($this));
33
34
		try {
35
			$this->login();
36
37
			$result = $this->command();
38
			if (is_callable($result)) {
39
				$result = call_user_func($result, $this);
40
			}
41
42
			$this->dumpRegisters();
43
44
			$this->logout();
45
		} catch (\Exception $ex) {
46
			elgg_log($ex->getMessage(), 'ERROR');
47
			$result = $ex->getCode() ? : 1;
48
		}
49
50
		return (int) $result;
51
	}
52
53
	/**
54
	 * Command to be executed
55
	 *
56
	 * This method method should return an integer code of the error (or 0 for success).
57
	 * Optionally, the method can return a callable that will receive the instance of this command as an argument
58
	 *
59
	 * @return mixed
60
	 * @see Command::execute()
61
	 */
62
	abstract protected function command();
63
64
	/**
65
	 * Login a user defined by --as option
66
	 *
67
	 * @return void
68
	 * @throws RuntimeException
69
	 */
70
	final protected function login() {
71
		if (!$this->getDefinition()->hasOption('as')) {
72
			return;
73
		}
74
		$username = $this->option('as');
75
		if (!$username) {
76
			return;
77
		}
78
		$user = get_user_by_username($username);
79
		if (!$user) {
80
			throw new RuntimeException("User with username $username not found");
81
		}
82
		if (!login($user)) {
83
			throw new RuntimeException("Unable to login as $username");
84
		}
85
		elgg_log("Logged in as $username [guid: $user->guid]");
86
	}
87
88
	/**
89
	 * Logout a user
90
	 * @return void
91
	 */
92
	final protected function logout() {
93
		if (elgg_is_logged_in()) {
94
			logout();
95
		}
96
	}
97
98
}
99