From 3f94708eff339ef824e8fb7bbfc989891bf447f8 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Mon, 28 Oct 2019 21:48:29 -0700 Subject: [PATCH] Shell: Add more types Change-Id: I315f0bb2746ccf7249b8d622a153162dd634ff2e --- includes/shell/Command.php | 18 +++++++++--------- includes/shell/CommandFactory.php | 6 +++--- includes/shell/FirejailCommand.php | 4 ++-- includes/shell/Shell.php | 8 +++++--- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/includes/shell/Command.php b/includes/shell/Command.php index b36ae84c428..236bc82ec60 100644 --- a/includes/shell/Command.php +++ b/includes/shell/Command.php @@ -185,7 +185,7 @@ class Command { * @param string $method * @return $this */ - public function profileMethod( $method ): Command { + public function profileMethod( string $method ): Command { $this->method = $method; return $this; @@ -197,7 +197,7 @@ class Command { * @param string|null $inputString * @return $this */ - public function input( $inputString ): Command { + public function input( ?string $inputString ): Command { $this->inputString = is_null( $inputString ) ? null : (string)$inputString; return $this; @@ -210,7 +210,7 @@ class Command { * @param bool $yesno * @return $this */ - public function includeStderr( $yesno = true ): Command { + public function includeStderr( bool $yesno = true ): Command { $this->doIncludeStderr = $yesno; return $this; @@ -222,7 +222,7 @@ class Command { * @param bool $yesno * @return $this */ - public function logStderr( $yesno = true ): Command { + public function logStderr( bool $yesno = true ): Command { $this->doLogStderr = $yesno; return $this; @@ -247,7 +247,7 @@ class Command { * @param int $restrictions * @return $this */ - public function restrict( $restrictions ): Command { + public function restrict( int $restrictions ): Command { $this->restrictions |= $restrictions; return $this; @@ -260,7 +260,7 @@ class Command { * * @return bool */ - protected function hasRestriction( $restriction ) { + protected function hasRestriction( int $restriction ): bool { return ( $this->restrictions & $restriction ) === $restriction; } @@ -286,7 +286,7 @@ class Command { * @param string $command Already-escaped command to run * @return array [ command, whether to use log pipe ] */ - protected function buildFinalCommand( $command ) { + protected function buildFinalCommand( string $command ): array { $envcmd = ''; foreach ( $this->env as $k => $v ) { if ( wfIsWindows() ) { @@ -345,7 +345,7 @@ class Command { * @throws ProcOpenError * @throws ShellDisabledError */ - public function execute() { + public function execute(): Result { $this->everExecuted = true; $profileMethod = $this->method ?: wfGetCaller(); @@ -553,7 +553,7 @@ class Command { * * @return string */ - public function __toString() { + public function __toString(): string { return "#Command: {$this->command}"; } } diff --git a/includes/shell/CommandFactory.php b/includes/shell/CommandFactory.php index d3e00b18960..e549e498163 100644 --- a/includes/shell/CommandFactory.php +++ b/includes/shell/CommandFactory.php @@ -63,7 +63,7 @@ class CommandFactory { $this->cgroup = $cgroup; if ( $restrictionMethod === 'autodetect' ) { // On Linux systems check for firejail - if ( PHP_OS === 'Linux' && $this->findFirejail() !== false ) { + if ( PHP_OS === 'Linux' && $this->findFirejail() !== null ) { $this->restrictionMethod = 'firejail'; } else { $this->restrictionMethod = false; @@ -74,7 +74,7 @@ class CommandFactory { $this->setLogger( new NullLogger() ); } - private function findFirejail() { + private function findFirejail(): ?string { if ( $this->firejail === null ) { $this->firejail = ExecutableFinder::findInDefaultPaths( 'firejail' ); } @@ -88,7 +88,7 @@ class CommandFactory { * @param bool $yesno * @see Command::logStderr */ - public function logStderr( $yesno = true ) { + public function logStderr( bool $yesno = true ): void { $this->doLogStderr = $yesno; } diff --git a/includes/shell/FirejailCommand.php b/includes/shell/FirejailCommand.php index 6bf94cdbe0d..5db89373b69 100644 --- a/includes/shell/FirejailCommand.php +++ b/includes/shell/FirejailCommand.php @@ -43,7 +43,7 @@ class FirejailCommand extends Command { /** * @param string $firejail Path to firejail */ - public function __construct( $firejail ) { + public function __construct( string $firejail ) { parent::__construct(); $this->firejail = $firejail; } @@ -59,7 +59,7 @@ class FirejailCommand extends Command { /** * @inheritDoc */ - protected function buildFinalCommand( $command ) { + protected function buildFinalCommand( string $command ): array { // If there are no restrictions, don't use firejail if ( $this->restrictions === 0 ) { $splitCommand = explode( ' ', $command, 2 ); diff --git a/includes/shell/Shell.php b/includes/shell/Shell.php index 009e7d65d2b..21e4197c190 100644 --- a/includes/shell/Shell.php +++ b/includes/shell/Shell.php @@ -134,7 +134,7 @@ class Shell { * * @return bool */ - public static function isDisabled() { + public static function isDisabled(): bool { static $disabled = null; if ( is_null( $disabled ) ) { @@ -160,7 +160,7 @@ class Shell { * array of strings parameter. Null values are ignored. * @return string */ - public static function escape( ...$args ) { + public static function escape( ...$args ): string { if ( count( $args ) === 1 && is_array( reset( $args ) ) ) { // If only one argument has been passed, and that argument is an array, // treat it as a list of arguments @@ -233,7 +233,9 @@ class Shell { * @phan-param array{php?:string,wrapper?:string} $options * @return Command */ - public static function makeScriptCommand( $script, $parameters, $options = [] ): Command { + public static function makeScriptCommand( + string $script, array $parameters, $options = [] + ): Command { global $wgPhpCli; // Give site config file a chance to run the script in a wrapper. // The caller may likely want to call wfBasename() on $script.