fix:更新已知bug,优化代码

This commit is contained in:
Ying
2022-11-28 19:11:12 +08:00
parent f6aee95cfc
commit 9445b206a2
1378 changed files with 53759 additions and 20789 deletions

View File

@@ -40,8 +40,8 @@ use Symfony\Component\Console\Exception\RuntimeException;
*/
class ArgvInput extends Input
{
private $tokens;
private $parsed;
private array $tokens;
private array $parsed;
public function __construct(array $argv = null, InputDefinition $definition = null)
{
@@ -199,7 +199,7 @@ class ArgvInput extends Input
*
* @throws RuntimeException When option given doesn't exist
*/
private function addShortOption(string $shortcut, $value)
private function addShortOption(string $shortcut, mixed $value)
{
if (!$this->definition->hasShortcut($shortcut)) {
throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
@@ -213,7 +213,7 @@ class ArgvInput extends Input
*
* @throws RuntimeException When option given doesn't exist
*/
private function addLongOption(string $name, $value)
private function addLongOption(string $name, mixed $value)
{
if (!$this->definition->hasOption($name)) {
if (!$this->definition->hasNegation($name)) {
@@ -266,7 +266,7 @@ class ArgvInput extends Input
/**
* {@inheritdoc}
*/
public function getFirstArgument()
public function getFirstArgument(): ?string
{
$isOption = false;
foreach ($this->tokens as $i => $token) {
@@ -301,7 +301,7 @@ class ArgvInput extends Input
/**
* {@inheritdoc}
*/
public function hasParameterOption($values, bool $onlyParams = false)
public function hasParameterOption(string|array $values, bool $onlyParams = false): bool
{
$values = (array) $values;
@@ -326,7 +326,7 @@ class ArgvInput extends Input
/**
* {@inheritdoc}
*/
public function getParameterOption($values, $default = false, bool $onlyParams = false)
public function getParameterOption(string|array $values, string|bool|int|float|array|null $default = false, bool $onlyParams = false): mixed
{
$values = (array) $values;
$tokens = $this->tokens;
@@ -356,10 +356,8 @@ class ArgvInput extends Input
/**
* Returns a stringified representation of the args passed to the command.
*
* @return string
*/
public function __toString()
public function __toString(): string
{
$tokens = array_map(function ($token) {
if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {

View File

@@ -25,7 +25,7 @@ use Symfony\Component\Console\Exception\InvalidOptionException;
*/
class ArrayInput extends Input
{
private $parameters;
private array $parameters;
public function __construct(array $parameters, InputDefinition $definition = null)
{
@@ -37,7 +37,7 @@ class ArrayInput extends Input
/**
* {@inheritdoc}
*/
public function getFirstArgument()
public function getFirstArgument(): ?string
{
foreach ($this->parameters as $param => $value) {
if ($param && \is_string($param) && '-' === $param[0]) {
@@ -53,7 +53,7 @@ class ArrayInput extends Input
/**
* {@inheritdoc}
*/
public function hasParameterOption($values, bool $onlyParams = false)
public function hasParameterOption(string|array $values, bool $onlyParams = false): bool
{
$values = (array) $values;
@@ -77,7 +77,7 @@ class ArrayInput extends Input
/**
* {@inheritdoc}
*/
public function getParameterOption($values, $default = false, bool $onlyParams = false)
public function getParameterOption(string|array $values, string|bool|int|float|array|null $default = false, bool $onlyParams = false): mixed
{
$values = (array) $values;
@@ -100,10 +100,8 @@ class ArrayInput extends Input
/**
* Returns a stringified representation of the args passed to the command.
*
* @return string
*/
public function __toString()
public function __toString(): string
{
$params = [];
foreach ($this->parameters as $param => $val) {
@@ -148,7 +146,7 @@ class ArrayInput extends Input
*
* @throws InvalidOptionException When option given doesn't exist
*/
private function addShortOption(string $shortcut, $value)
private function addShortOption(string $shortcut, mixed $value)
{
if (!$this->definition->hasShortcut($shortcut)) {
throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
@@ -163,7 +161,7 @@ class ArrayInput extends Input
* @throws InvalidOptionException When option given doesn't exist
* @throws InvalidOptionException When a required value is missing
*/
private function addLongOption(string $name, $value)
private function addLongOption(string $name, mixed $value)
{
if (!$this->definition->hasOption($name)) {
if (!$this->definition->hasNegation($name)) {
@@ -194,12 +192,9 @@ class ArrayInput extends Input
/**
* Adds an argument value.
*
* @param string|int $name The argument name
* @param mixed $value The value for the argument
*
* @throws InvalidArgumentException When argument given doesn't exist
*/
private function addArgument($name, $value)
private function addArgument(string|int $name, mixed $value)
{
if (!$this->definition->hasArgument($name)) {
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));

View File

@@ -80,7 +80,7 @@ abstract class Input implements InputInterface, StreamableInputInterface
/**
* {@inheritdoc}
*/
public function isInteractive()
public function isInteractive(): bool
{
return $this->interactive;
}
@@ -96,7 +96,7 @@ abstract class Input implements InputInterface, StreamableInputInterface
/**
* {@inheritdoc}
*/
public function getArguments()
public function getArguments(): array
{
return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
}
@@ -104,7 +104,7 @@ abstract class Input implements InputInterface, StreamableInputInterface
/**
* {@inheritdoc}
*/
public function getArgument(string $name)
public function getArgument(string $name): mixed
{
if (!$this->definition->hasArgument($name)) {
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
@@ -116,7 +116,7 @@ abstract class Input implements InputInterface, StreamableInputInterface
/**
* {@inheritdoc}
*/
public function setArgument(string $name, $value)
public function setArgument(string $name, mixed $value)
{
if (!$this->definition->hasArgument($name)) {
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
@@ -128,7 +128,7 @@ abstract class Input implements InputInterface, StreamableInputInterface
/**
* {@inheritdoc}
*/
public function hasArgument(string $name)
public function hasArgument(string $name): bool
{
return $this->definition->hasArgument($name);
}
@@ -136,7 +136,7 @@ abstract class Input implements InputInterface, StreamableInputInterface
/**
* {@inheritdoc}
*/
public function getOptions()
public function getOptions(): array
{
return array_merge($this->definition->getOptionDefaults(), $this->options);
}
@@ -144,7 +144,7 @@ abstract class Input implements InputInterface, StreamableInputInterface
/**
* {@inheritdoc}
*/
public function getOption(string $name)
public function getOption(string $name): mixed
{
if ($this->definition->hasNegation($name)) {
if (null === $value = $this->getOption($this->definition->negationToName($name))) {
@@ -164,7 +164,7 @@ abstract class Input implements InputInterface, StreamableInputInterface
/**
* {@inheritdoc}
*/
public function setOption(string $name, $value)
public function setOption(string $name, mixed $value)
{
if ($this->definition->hasNegation($name)) {
$this->options[$this->definition->negationToName($name)] = !$value;
@@ -180,17 +180,15 @@ abstract class Input implements InputInterface, StreamableInputInterface
/**
* {@inheritdoc}
*/
public function hasOption(string $name)
public function hasOption(string $name): bool
{
return $this->definition->hasOption($name) || $this->definition->hasNegation($name);
}
/**
* Escapes a token through escapeshellarg if it contains unsafe chars.
*
* @return string
*/
public function escapeToken(string $token)
public function escapeToken(string $token): string
{
return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
}

View File

@@ -25,10 +25,10 @@ class InputArgument
public const OPTIONAL = 2;
public const IS_ARRAY = 4;
private $name;
private $mode;
private $default;
private $description;
private string $name;
private int $mode;
private string|int|bool|array|null|float $default;
private string $description;
/**
* @param string $name The argument name
@@ -38,7 +38,7 @@ class InputArgument
*
* @throws InvalidArgumentException When argument mode is not valid
*/
public function __construct(string $name, int $mode = null, string $description = '', $default = null)
public function __construct(string $name, int $mode = null, string $description = '', string|bool|int|float|array $default = null)
{
if (null === $mode) {
$mode = self::OPTIONAL;
@@ -55,10 +55,8 @@ class InputArgument
/**
* Returns the argument name.
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
@@ -68,7 +66,7 @@ class InputArgument
*
* @return bool true if parameter mode is self::REQUIRED, false otherwise
*/
public function isRequired()
public function isRequired(): bool
{
return self::REQUIRED === (self::REQUIRED & $this->mode);
}
@@ -78,7 +76,7 @@ class InputArgument
*
* @return bool true if mode is self::IS_ARRAY, false otherwise
*/
public function isArray()
public function isArray(): bool
{
return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
}
@@ -86,11 +84,9 @@ class InputArgument
/**
* Sets the default value.
*
* @param string|bool|int|float|array|null $default
*
* @throws LogicException When incorrect default value is given
*/
public function setDefault($default = null)
public function setDefault(string|bool|int|float|array $default = null)
{
if ($this->isRequired() && null !== $default) {
throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
@@ -109,20 +105,16 @@ class InputArgument
/**
* Returns the default value.
*
* @return string|bool|int|float|array|null
*/
public function getDefault()
public function getDefault(): string|bool|int|float|array|null
{
return $this->default;
}
/**
* Returns the description text.
*
* @return string
*/
public function getDescription()
public function getDescription(): string
{
return $this->description;
}

View File

@@ -28,13 +28,13 @@ use Symfony\Component\Console\Exception\LogicException;
*/
class InputDefinition
{
private $arguments;
private $requiredCount;
private $lastArrayArgument;
private $lastOptionalArgument;
private $options;
private $negations;
private $shortcuts;
private array $arguments = [];
private int $requiredCount = 0;
private $lastArrayArgument = null;
private $lastOptionalArgument = null;
private array $options = [];
private array $negations = [];
private array $shortcuts = [];
/**
* @param array $definition An array of InputArgument and InputOption instance
@@ -124,13 +124,9 @@ class InputDefinition
/**
* Returns an InputArgument by name or by position.
*
* @param string|int $name The InputArgument name or position
*
* @return InputArgument
*
* @throws InvalidArgumentException When argument given doesn't exist
*/
public function getArgument($name)
public function getArgument(string|int $name): InputArgument
{
if (!$this->hasArgument($name)) {
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
@@ -143,12 +139,8 @@ class InputDefinition
/**
* Returns true if an InputArgument object exists by name or position.
*
* @param string|int $name The InputArgument name or position
*
* @return bool
*/
public function hasArgument($name)
public function hasArgument(string|int $name): bool
{
$arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
@@ -160,27 +152,23 @@ class InputDefinition
*
* @return InputArgument[]
*/
public function getArguments()
public function getArguments(): array
{
return $this->arguments;
}
/**
* Returns the number of InputArguments.
*
* @return int
*/
public function getArgumentCount()
public function getArgumentCount(): int
{
return null !== $this->lastArrayArgument ? \PHP_INT_MAX : \count($this->arguments);
}
/**
* Returns the number of required InputArguments.
*
* @return int
*/
public function getArgumentRequiredCount()
public function getArgumentRequiredCount(): int
{
return $this->requiredCount;
}
@@ -188,7 +176,7 @@ class InputDefinition
/**
* @return array<string|bool|int|float|array|null>
*/
public function getArgumentDefaults()
public function getArgumentDefaults(): array
{
$values = [];
foreach ($this->arguments as $argument) {
@@ -262,11 +250,9 @@ class InputDefinition
/**
* Returns an InputOption by name.
*
* @return InputOption
*
* @throws InvalidArgumentException When option given doesn't exist
*/
public function getOption(string $name)
public function getOption(string $name): InputOption
{
if (!$this->hasOption($name)) {
throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
@@ -280,10 +266,8 @@ class InputDefinition
*
* This method can't be used to check if the user included the option when
* executing the command (use getOption() instead).
*
* @return bool
*/
public function hasOption(string $name)
public function hasOption(string $name): bool
{
return isset($this->options[$name]);
}
@@ -293,17 +277,15 @@ class InputDefinition
*
* @return InputOption[]
*/
public function getOptions()
public function getOptions(): array
{
return $this->options;
}
/**
* Returns true if an InputOption object exists by shortcut.
*
* @return bool
*/
public function hasShortcut(string $name)
public function hasShortcut(string $name): bool
{
return isset($this->shortcuts[$name]);
}
@@ -318,10 +300,8 @@ class InputDefinition
/**
* Gets an InputOption by shortcut.
*
* @return InputOption
*/
public function getOptionForShortcut(string $shortcut)
public function getOptionForShortcut(string $shortcut): InputOption
{
return $this->getOption($this->shortcutToName($shortcut));
}
@@ -329,7 +309,7 @@ class InputDefinition
/**
* @return array<string|bool|int|float|array|null>
*/
public function getOptionDefaults()
public function getOptionDefaults(): array
{
$values = [];
foreach ($this->options as $option) {
@@ -373,10 +353,8 @@ class InputDefinition
/**
* Gets the synopsis.
*
* @return string
*/
public function getSynopsis(bool $short = false)
public function getSynopsis(bool $short = false): string
{
$elements = [];

View File

@@ -23,10 +23,8 @@ interface InputInterface
{
/**
* Returns the first argument from the raw parameters (not parsed).
*
* @return string|null
*/
public function getFirstArgument();
public function getFirstArgument(): ?string;
/**
* Returns true if the raw parameters (not parsed) contain a value.
@@ -38,10 +36,8 @@ interface InputInterface
*
* @param string|array $values The values to look for in the raw parameters (can be an array)
* @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal
*
* @return bool
*/
public function hasParameterOption($values, bool $onlyParams = false);
public function hasParameterOption(string|array $values, bool $onlyParams = false): bool;
/**
* Returns the value of a raw option (not parsed).
@@ -57,7 +53,7 @@ interface InputInterface
*
* @return mixed
*/
public function getParameterOption($values, $default = false, bool $onlyParams = false);
public function getParameterOption(string|array $values, string|bool|int|float|array|null $default = false, bool $onlyParams = false);
/**
* Binds the current Input instance with the given arguments and options.
@@ -78,7 +74,7 @@ interface InputInterface
*
* @return array<string|bool|int|float|array|null>
*/
public function getArguments();
public function getArguments(): array;
/**
* Returns the argument value for a given argument name.
@@ -92,25 +88,21 @@ interface InputInterface
/**
* Sets an argument value by name.
*
* @param mixed $value The argument value
*
* @throws InvalidArgumentException When argument given doesn't exist
*/
public function setArgument(string $name, $value);
public function setArgument(string $name, mixed $value);
/**
* Returns true if an InputArgument object exists by name or position.
*
* @return bool
*/
public function hasArgument(string $name);
public function hasArgument(string $name): bool;
/**
* Returns all the given options merged with the default values.
*
* @return array<string|bool|int|float|array|null>
*/
public function getOptions();
public function getOptions(): array;
/**
* Returns the option value for a given option name.
@@ -124,25 +116,19 @@ interface InputInterface
/**
* Sets an option value by name.
*
* @param mixed $value The option value
*
* @throws InvalidArgumentException When option given doesn't exist
*/
public function setOption(string $name, $value);
public function setOption(string $name, mixed $value);
/**
* Returns true if an InputOption object exists by name.
*
* @return bool
*/
public function hasOption(string $name);
public function hasOption(string $name): bool;
/**
* Is this input means interactive?
*
* @return bool
*/
public function isInteractive();
public function isInteractive(): bool;
/**
* Sets the input interactivity.

View File

@@ -46,11 +46,11 @@ class InputOption
*/
public const VALUE_NEGATABLE = 16;
private $name;
private $shortcut;
private $mode;
private $default;
private $description;
private string $name;
private string|array|null $shortcut;
private int $mode;
private string|int|bool|array|null|float $default;
private string $description;
/**
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
@@ -59,7 +59,7 @@ class InputOption
*
* @throws InvalidArgumentException If option mode is invalid or incompatible
*/
public function __construct(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null)
public function __construct(string $name, string|array $shortcut = null, int $mode = null, string $description = '', string|bool|int|float|array $default = null)
{
if (str_starts_with($name, '--')) {
$name = substr($name, 2);
@@ -109,20 +109,16 @@ class InputOption
/**
* Returns the option shortcut.
*
* @return string|null
*/
public function getShortcut()
public function getShortcut(): ?string
{
return $this->shortcut;
}
/**
* Returns the option name.
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
@@ -132,7 +128,7 @@ class InputOption
*
* @return bool true if value mode is not self::VALUE_NONE, false otherwise
*/
public function acceptValue()
public function acceptValue(): bool
{
return $this->isValueRequired() || $this->isValueOptional();
}
@@ -142,7 +138,7 @@ class InputOption
*
* @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
*/
public function isValueRequired()
public function isValueRequired(): bool
{
return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
}
@@ -152,7 +148,7 @@ class InputOption
*
* @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
*/
public function isValueOptional()
public function isValueOptional(): bool
{
return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
}
@@ -162,7 +158,7 @@ class InputOption
*
* @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
*/
public function isArray()
public function isArray(): bool
{
return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
}
@@ -172,10 +168,7 @@ class InputOption
return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode);
}
/**
* @param string|bool|int|float|array|null $default
*/
public function setDefault($default = null)
public function setDefault(string|bool|int|float|array $default = null)
{
if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
@@ -194,30 +187,24 @@ class InputOption
/**
* Returns the default value.
*
* @return string|bool|int|float|array|null
*/
public function getDefault()
public function getDefault(): string|bool|int|float|array|null
{
return $this->default;
}
/**
* Returns the description text.
*
* @return string
*/
public function getDescription()
public function getDescription(): string
{
return $this->description;
}
/**
* Checks whether the given option equals this one.
*
* @return bool
*/
public function equals(self $option)
public function equals(self $option): bool
{
return $option->getName() === $this->getName()
&& $option->getShortcut() === $this->getShortcut()