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

@@ -47,29 +47,26 @@ class Command
*/
protected static $defaultDescription;
private $application;
private $name;
private $processTitle;
private $aliases = [];
private $application = null;
private ?string $name = null;
private ?string $processTitle = null;
private array $aliases = [];
private $definition;
private $hidden = false;
private $help = '';
private $description = '';
private $fullDefinition;
private $ignoreValidationErrors = false;
private $code;
private $synopsis = [];
private $usages = [];
private $helperSet;
private bool $hidden = false;
private string $help = '';
private string $description = '';
private $fullDefinition = null;
private bool $ignoreValidationErrors = false;
private ?\Closure $code = null;
private array $synopsis = [];
private array $usages = [];
private $helperSet = null;
/**
* @return string|null
*/
public static function getDefaultName()
public static function getDefaultName(): ?string
{
$class = static::class;
if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) {
if ($attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) {
return $attribute[0]->newInstance()->name;
}
@@ -82,7 +79,7 @@ class Command
{
$class = static::class;
if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) {
if ($attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) {
return $attribute[0]->newInstance()->description;
}
@@ -151,20 +148,16 @@ class Command
/**
* Gets the helper set.
*
* @return HelperSet|null
*/
public function getHelperSet()
public function getHelperSet(): ?HelperSet
{
return $this->helperSet;
}
/**
* Gets the application instance for this command.
*
* @return Application|null
*/
public function getApplication()
public function getApplication(): ?Application
{
return $this->application;
}
@@ -247,7 +240,7 @@ class Command
* @see setCode()
* @see execute()
*/
public function run(InputInterface $input, OutputInterface $output)
public function run(InputInterface $input, OutputInterface $output): int
{
// add the application arguments and options
$this->mergeApplicationDefinition();
@@ -326,7 +319,7 @@ class Command
*
* @see execute()
*/
public function setCode(callable $code)
public function setCode(callable $code): static
{
if ($code instanceof \Closure) {
$r = new \ReflectionFunction($code);
@@ -340,6 +333,8 @@ class Command
restore_error_handler();
}
}
} else {
$code = \Closure::fromCallable($code);
}
$this->code = $code;
@@ -377,11 +372,9 @@ class Command
/**
* Sets an array of argument and option instances.
*
* @param array|InputDefinition $definition An array of argument and option instances or a definition instance
*
* @return $this
*/
public function setDefinition($definition)
public function setDefinition(array|InputDefinition $definition): static
{
if ($definition instanceof InputDefinition) {
$this->definition = $definition;
@@ -396,10 +389,8 @@ class Command
/**
* Gets the InputDefinition attached to this Command.
*
* @return InputDefinition
*/
public function getDefinition()
public function getDefinition(): InputDefinition
{
return $this->fullDefinition ?? $this->getNativeDefinition();
}
@@ -411,16 +402,10 @@ class Command
* be changed by merging with the application InputDefinition.
*
* This method is not part of public API and should not be used directly.
*
* @return InputDefinition
*/
public function getNativeDefinition()
public function getNativeDefinition(): InputDefinition
{
if (null === $this->definition) {
throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', static::class));
}
return $this->definition;
return $this->definition ?? throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', static::class));
}
/**
@@ -433,7 +418,7 @@ class Command
*
* @return $this
*/
public function addArgument(string $name, int $mode = null, string $description = '', $default = null)
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null): static
{
$this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
if (null !== $this->fullDefinition) {
@@ -446,15 +431,15 @@ class Command
/**
* Adds an option.
*
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
* @param int|null $mode The option mode: One of the InputOption::VALUE_* constants
* @param mixed $default The default value (must be null for InputOption::VALUE_NONE)
* @param $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
* @param $mode The option mode: One of the InputOption::VALUE_* constants
* @param $default The default value (must be null for InputOption::VALUE_NONE)
*
* @throws InvalidArgumentException If option mode is invalid or incompatible
*
* @return $this
*/
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null)
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null): static
{
$this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
if (null !== $this->fullDefinition) {
@@ -476,7 +461,7 @@ class Command
*
* @throws InvalidArgumentException When the name is invalid
*/
public function setName(string $name)
public function setName(string $name): static
{
$this->validateName($name);
@@ -493,7 +478,7 @@ class Command
*
* @return $this
*/
public function setProcessTitle(string $title)
public function setProcessTitle(string $title): static
{
$this->processTitle = $title;
@@ -502,23 +487,18 @@ class Command
/**
* Returns the command name.
*
* @return string|null
*/
public function getName()
public function getName(): ?string
{
return $this->name;
}
/**
* @param bool $hidden Whether or not the command should be hidden from the list of commands
* The default value will be true in Symfony 6.0
*
* @return $this
*
* @final since Symfony 5.1
*/
public function setHidden(bool $hidden /* = true */)
public function setHidden(bool $hidden = true): static
{
$this->hidden = $hidden;
@@ -528,7 +508,7 @@ class Command
/**
* @return bool whether the command should be publicly shown or not
*/
public function isHidden()
public function isHidden(): bool
{
return $this->hidden;
}
@@ -538,7 +518,7 @@ class Command
*
* @return $this
*/
public function setDescription(string $description)
public function setDescription(string $description): static
{
$this->description = $description;
@@ -547,10 +527,8 @@ class Command
/**
* Returns the description for the command.
*
* @return string
*/
public function getDescription()
public function getDescription(): string
{
return $this->description;
}
@@ -560,7 +538,7 @@ class Command
*
* @return $this
*/
public function setHelp(string $help)
public function setHelp(string $help): static
{
$this->help = $help;
@@ -569,10 +547,8 @@ class Command
/**
* Returns the help for the command.
*
* @return string
*/
public function getHelp()
public function getHelp(): string
{
return $this->help;
}
@@ -580,10 +556,8 @@ class Command
/**
* Returns the processed help for the command replacing the %command.name% and
* %command.full_name% patterns with the real values dynamically.
*
* @return string
*/
public function getProcessedHelp()
public function getProcessedHelp(): string
{
$name = $this->name;
$isSingleCommand = $this->application && $this->application->isSingleCommand();
@@ -609,7 +583,7 @@ class Command
*
* @throws InvalidArgumentException When an alias is invalid
*/
public function setAliases(iterable $aliases)
public function setAliases(iterable $aliases): static
{
$list = [];
@@ -625,10 +599,8 @@ class Command
/**
* Returns the aliases for the command.
*
* @return array
*/
public function getAliases()
public function getAliases(): array
{
return $this->aliases;
}
@@ -637,10 +609,8 @@ class Command
* Returns the synopsis for the command.
*
* @param bool $short Whether to show the short version of the synopsis (with options folded) or not
*
* @return string
*/
public function getSynopsis(bool $short = false)
public function getSynopsis(bool $short = false): string
{
$key = $short ? 'short' : 'long';
@@ -656,7 +626,7 @@ class Command
*
* @return $this
*/
public function addUsage(string $usage)
public function addUsage(string $usage): static
{
if (!str_starts_with($usage, $this->name)) {
$usage = sprintf('%s %s', $this->name, $usage);
@@ -669,10 +639,8 @@ class Command
/**
* Returns alternative usages of the command.
*
* @return array
*/
public function getUsages()
public function getUsages(): array
{
return $this->usages;
}
@@ -680,12 +648,10 @@ class Command
/**
* Gets a helper instance by name.
*
* @return mixed
*
* @throws LogicException if no HelperSet is defined
* @throws InvalidArgumentException if the helper is not defined
*/
public function getHelper(string $name)
public function getHelper(string $name): mixed
{
if (null === $this->helperSet) {
throw new LogicException(sprintf('Cannot retrieve helper "%s" because there is no HelperSet defined. Did you forget to add your command to the application or to set the application on the command using the setApplication() method? You can also set the HelperSet directly using the setHelperSet() method.', $name));