Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -19,41 +19,38 @@ use Symfony\Component\Console\Exception\LogicException;
*
* Usage:
*
* $definition = new InputDefinition(array(
* new InputArgument('name', InputArgument::REQUIRED),
* new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
* ));
* $definition = new InputDefinition([
* new InputArgument('name', InputArgument::REQUIRED),
* new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
* ]);
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class InputDefinition
{
private $arguments;
private $requiredCount;
private $hasAnArrayArgument = false;
private $hasOptional;
private $options;
private $shortcuts;
private array $arguments = [];
private int $requiredCount = 0;
private $lastArrayArgument = null;
private $lastOptionalArgument = null;
private array $options = [];
private array $negations = [];
private array $shortcuts = [];
/**
* Constructor.
*
* @param array $definition An array of InputArgument and InputOption instance
*/
public function __construct(array $definition = array())
public function __construct(array $definition = [])
{
$this->setDefinition($definition);
}
/**
* Sets the definition of the input.
*
* @param array $definition The definition array
*/
public function setDefinition(array $definition)
{
$arguments = array();
$options = array();
$arguments = [];
$options = [];
foreach ($definition as $item) {
if ($item instanceof InputOption) {
$options[] = $item;
@@ -71,12 +68,12 @@ class InputDefinition
*
* @param InputArgument[] $arguments An array of InputArgument objects
*/
public function setArguments($arguments = array())
public function setArguments(array $arguments = [])
{
$this->arguments = array();
$this->arguments = [];
$this->requiredCount = 0;
$this->hasOptional = false;
$this->hasAnArrayArgument = false;
$this->lastOptionalArgument = null;
$this->lastArrayArgument = null;
$this->addArguments($arguments);
}
@@ -85,7 +82,7 @@ class InputDefinition
*
* @param InputArgument[] $arguments An array of InputArgument objects
*/
public function addArguments($arguments = array())
public function addArguments(?array $arguments = [])
{
if (null !== $arguments) {
foreach ($arguments as $argument) {
@@ -95,10 +92,6 @@ class InputDefinition
}
/**
* Adds an InputArgument object.
*
* @param InputArgument $argument An InputArgument object
*
* @throws LogicException When incorrect argument is given
*/
public function addArgument(InputArgument $argument)
@@ -107,22 +100,22 @@ class InputDefinition
throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
}
if ($this->hasAnArrayArgument) {
throw new LogicException('Cannot add an argument after an array argument.');
if (null !== $this->lastArrayArgument) {
throw new LogicException(sprintf('Cannot add a required argument "%s" after an array argument "%s".', $argument->getName(), $this->lastArrayArgument->getName()));
}
if ($argument->isRequired() && $this->hasOptional) {
throw new LogicException('Cannot add a required argument after an optional one.');
if ($argument->isRequired() && null !== $this->lastOptionalArgument) {
throw new LogicException(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $argument->getName(), $this->lastOptionalArgument->getName()));
}
if ($argument->isArray()) {
$this->hasAnArrayArgument = true;
$this->lastArrayArgument = $argument;
}
if ($argument->isRequired()) {
++$this->requiredCount;
} else {
$this->hasOptional = true;
$this->lastOptionalArgument = $argument;
}
$this->arguments[$argument->getName()] = $argument;
@@ -131,33 +124,25 @@ class InputDefinition
/**
* Returns an InputArgument by name or by position.
*
* @param string|int $name The InputArgument name or position
*
* @return InputArgument An InputArgument object
*
* @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));
}
$arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
$arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
return $arguments[$name];
}
/**
* Returns true if an InputArgument object exists by name or position.
*
* @param string|int $name The InputArgument name or position
*
* @return bool true if the InputArgument object exists, false otherwise
*/
public function hasArgument($name)
public function hasArgument(string|int $name): bool
{
$arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
$arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
return isset($arguments[$name]);
}
@@ -165,41 +150,35 @@ class InputDefinition
/**
* Gets the array of InputArgument objects.
*
* @return InputArgument[] An array of InputArgument objects
* @return InputArgument[]
*/
public function getArguments()
public function getArguments(): array
{
return $this->arguments;
}
/**
* Returns the number of InputArguments.
*
* @return int The number of InputArguments
*/
public function getArgumentCount()
public function getArgumentCount(): int
{
return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments);
return null !== $this->lastArrayArgument ? \PHP_INT_MAX : \count($this->arguments);
}
/**
* Returns the number of required InputArguments.
*
* @return int The number of required InputArguments
*/
public function getArgumentRequiredCount()
public function getArgumentRequiredCount(): int
{
return $this->requiredCount;
}
/**
* Gets the default values.
*
* @return array An array of default values
* @return array<string|bool|int|float|array|null>
*/
public function getArgumentDefaults()
public function getArgumentDefaults(): array
{
$values = array();
$values = [];
foreach ($this->arguments as $argument) {
$values[$argument->getName()] = $argument->getDefault();
}
@@ -212,10 +191,11 @@ class InputDefinition
*
* @param InputOption[] $options An array of InputOption objects
*/
public function setOptions($options = array())
public function setOptions(array $options = [])
{
$this->options = array();
$this->shortcuts = array();
$this->options = [];
$this->shortcuts = [];
$this->negations = [];
$this->addOptions($options);
}
@@ -224,7 +204,7 @@ class InputDefinition
*
* @param InputOption[] $options An array of InputOption objects
*/
public function addOptions($options = array())
public function addOptions(array $options = [])
{
foreach ($options as $option) {
$this->addOption($option);
@@ -232,10 +212,6 @@ class InputDefinition
}
/**
* Adds an InputOption object.
*
* @param InputOption $option An InputOption object
*
* @throws LogicException When option given already exist
*/
public function addOption(InputOption $option)
@@ -243,6 +219,9 @@ class InputDefinition
if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
}
if (isset($this->negations[$option->getName()])) {
throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
}
if ($option->getShortcut()) {
foreach (explode('|', $option->getShortcut()) as $shortcut) {
@@ -258,18 +237,22 @@ class InputDefinition
$this->shortcuts[$shortcut] = $option->getName();
}
}
if ($option->isNegatable()) {
$negatedName = 'no-'.$option->getName();
if (isset($this->options[$negatedName])) {
throw new LogicException(sprintf('An option named "%s" already exists.', $negatedName));
}
$this->negations[$negatedName] = $option->getName();
}
}
/**
* Returns an InputOption by name.
*
* @param string $name The InputOption name
*
* @return InputOption A InputOption object
*
* @throws InvalidArgumentException When option given doesn't exist
*/
public function getOption($name)
public function getOption(string $name): InputOption
{
if (!$this->hasOption($name)) {
throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
@@ -283,12 +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).
*
* @param string $name The InputOption name
*
* @return bool true if the InputOption object exists, false otherwise
*/
public function hasOption($name)
public function hasOption(string $name): bool
{
return isset($this->options[$name]);
}
@@ -296,45 +275,43 @@ class InputDefinition
/**
* Gets the array of InputOption objects.
*
* @return InputOption[] An array of InputOption objects
* @return InputOption[]
*/
public function getOptions()
public function getOptions(): array
{
return $this->options;
}
/**
* Returns true if an InputOption object exists by shortcut.
*
* @param string $name The InputOption shortcut
*
* @return bool true if the InputOption object exists, false otherwise
*/
public function hasShortcut($name)
public function hasShortcut(string $name): bool
{
return isset($this->shortcuts[$name]);
}
/**
* Gets an InputOption by shortcut.
*
* @param string $shortcut the Shortcut name
*
* @return InputOption An InputOption object
* Returns true if an InputOption object exists by negated name.
*/
public function getOptionForShortcut($shortcut)
public function hasNegation(string $name): bool
{
return isset($this->negations[$name]);
}
/**
* Gets an InputOption by shortcut.
*/
public function getOptionForShortcut(string $shortcut): InputOption
{
return $this->getOption($this->shortcutToName($shortcut));
}
/**
* Gets an array of default values.
*
* @return array An array of all default values
* @return array<string|bool|int|float|array|null>
*/
public function getOptionDefaults()
public function getOptionDefaults(): array
{
$values = array();
$values = [];
foreach ($this->options as $option) {
$values[$option->getName()] = $option->getDefault();
}
@@ -345,13 +322,11 @@ class InputDefinition
/**
* Returns the InputOption name given a shortcut.
*
* @param string $shortcut The shortcut
*
* @return string The InputOption name
*
* @throws InvalidArgumentException When option given does not exist
*
* @internal
*/
private function shortcutToName($shortcut)
public function shortcutToName(string $shortcut): string
{
if (!isset($this->shortcuts[$shortcut])) {
throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
@@ -361,15 +336,27 @@ class InputDefinition
}
/**
* Gets the synopsis.
* Returns the InputOption name given a negation.
*
* @param bool $short Whether to return the short version (with options folded) or not
* @throws InvalidArgumentException When option given does not exist
*
* @return string The synopsis
* @internal
*/
public function getSynopsis($short = false)
public function negationToName(string $negation): string
{
$elements = array();
if (!isset($this->negations[$negation])) {
throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $negation));
}
return $this->negations[$negation];
}
/**
* Gets the synopsis.
*/
public function getSynopsis(bool $short = false): string
{
$elements = [];
if ($short && $this->getOptions()) {
$elements[] = '[options]';
@@ -386,29 +373,30 @@ class InputDefinition
}
$shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
$elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
$negation = $option->isNegatable() ? sprintf('|--no-%s', $option->getName()) : '';
$elements[] = sprintf('[%s--%s%s%s]', $shortcut, $option->getName(), $value, $negation);
}
}
if (count($elements) && $this->getArguments()) {
if (\count($elements) && $this->getArguments()) {
$elements[] = '[--]';
}
$tail = '';
foreach ($this->getArguments() as $argument) {
$element = '<'.$argument->getName().'>';
if (!$argument->isRequired()) {
$element = '['.$element.']';
} elseif ($argument->isArray()) {
$element = $element.' ('.$element.')';
}
if ($argument->isArray()) {
$element .= '...';
}
if (!$argument->isRequired()) {
$element = '['.$element;
$tail .= ']';
}
$elements[] = $element;
}
return implode(' ', $elements);
return implode(' ', $elements).$tail;
}
}