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

@@ -21,22 +21,22 @@ use Symfony\Component\Console\Exception\LogicException;
*/
class Question
{
private $question;
private $attempts;
private $hidden = false;
private $hiddenFallback = true;
private $autocompleterCallback;
private $validator;
private $default;
private $normalizer;
private $trimmable = true;
private $multiline = false;
private string $question;
private ?int $attempts = null;
private bool $hidden = false;
private bool $hiddenFallback = true;
private ?\Closure $autocompleterCallback = null;
private ?\Closure $validator = null;
private string|int|bool|null|float $default;
private ?\Closure $normalizer = null;
private bool $trimmable = true;
private bool $multiline = false;
/**
* @param string $question The question to ask to the user
* @param string|bool|int|float|null $default The default answer to return if the user enters nothing
*/
public function __construct(string $question, $default = null)
public function __construct(string $question, string|bool|int|float $default = null)
{
$this->question = $question;
$this->default = $default;
@@ -44,20 +44,16 @@ class Question
/**
* Returns the question.
*
* @return string
*/
public function getQuestion()
public function getQuestion(): string
{
return $this->question;
}
/**
* Returns the default answer.
*
* @return string|bool|int|float|null
*/
public function getDefault()
public function getDefault(): string|bool|int|float|null
{
return $this->default;
}
@@ -75,7 +71,7 @@ class Question
*
* @return $this
*/
public function setMultiline(bool $multiline): self
public function setMultiline(bool $multiline): static
{
$this->multiline = $multiline;
@@ -84,10 +80,8 @@ class Question
/**
* Returns whether the user response must be hidden.
*
* @return bool
*/
public function isHidden()
public function isHidden(): bool
{
return $this->hidden;
}
@@ -99,7 +93,7 @@ class Question
*
* @throws LogicException In case the autocompleter is also used
*/
public function setHidden(bool $hidden)
public function setHidden(bool $hidden): static
{
if ($this->autocompleterCallback) {
throw new LogicException('A hidden question cannot use the autocompleter.');
@@ -112,10 +106,8 @@ class Question
/**
* In case the response cannot be hidden, whether to fallback on non-hidden question or not.
*
* @return bool
*/
public function isHiddenFallback()
public function isHiddenFallback(): bool
{
return $this->hiddenFallback;
}
@@ -125,7 +117,7 @@ class Question
*
* @return $this
*/
public function setHiddenFallback(bool $fallback)
public function setHiddenFallback(bool $fallback): static
{
$this->hiddenFallback = $fallback;
@@ -134,10 +126,8 @@ class Question
/**
* Gets values for the autocompleter.
*
* @return iterable|null
*/
public function getAutocompleterValues()
public function getAutocompleterValues(): ?iterable
{
$callback = $this->getAutocompleterCallback();
@@ -151,7 +141,7 @@ class Question
*
* @throws LogicException
*/
public function setAutocompleterValues(?iterable $values)
public function setAutocompleterValues(?iterable $values): static
{
if (\is_array($values)) {
$values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
@@ -186,13 +176,13 @@ class Question
*
* @return $this
*/
public function setAutocompleterCallback(callable $callback = null): self
public function setAutocompleterCallback(callable $callback = null): static
{
if ($this->hidden && null !== $callback) {
throw new LogicException('A hidden question cannot use the autocompleter.');
}
$this->autocompleterCallback = $callback;
$this->autocompleterCallback = null === $callback || $callback instanceof \Closure ? $callback : \Closure::fromCallable($callback);
return $this;
}
@@ -202,19 +192,17 @@ class Question
*
* @return $this
*/
public function setValidator(callable $validator = null)
public function setValidator(callable $validator = null): static
{
$this->validator = $validator;
$this->validator = null === $validator || $validator instanceof \Closure ? $validator : \Closure::fromCallable($validator);
return $this;
}
/**
* Gets the validator for the question.
*
* @return callable|null
*/
public function getValidator()
public function getValidator(): ?callable
{
return $this->validator;
}
@@ -228,7 +216,7 @@ class Question
*
* @throws InvalidArgumentException in case the number of attempts is invalid
*/
public function setMaxAttempts(?int $attempts)
public function setMaxAttempts(?int $attempts): static
{
if (null !== $attempts && $attempts < 1) {
throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
@@ -243,10 +231,8 @@ class Question
* Gets the maximum number of attempts.
*
* Null means an unlimited number of attempts.
*
* @return int|null
*/
public function getMaxAttempts()
public function getMaxAttempts(): ?int
{
return $this->attempts;
}
@@ -258,9 +244,9 @@ class Question
*
* @return $this
*/
public function setNormalizer(callable $normalizer)
public function setNormalizer(callable $normalizer): static
{
$this->normalizer = $normalizer;
$this->normalizer = $normalizer instanceof \Closure ? $normalizer : \Closure::fromCallable($normalizer);
return $this;
}
@@ -269,10 +255,8 @@ class Question
* Gets the normalizer for the response.
*
* The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
*
* @return callable|null
*/
public function getNormalizer()
public function getNormalizer(): ?callable
{
return $this->normalizer;
}
@@ -290,7 +274,7 @@ class Question
/**
* @return $this
*/
public function setTrimmable(bool $trimmable): self
public function setTrimmable(bool $trimmable): static
{
$this->trimmable = $trimmable;