Update to laravel 7

This commit is contained in:
KodeStar
2022-03-10 11:54:29 +00:00
parent 61a5a1a8b0
commit f9a19fce91
7170 changed files with 274189 additions and 283773 deletions

View File

@@ -27,6 +27,26 @@ use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
*/
class OptionsResolver implements Options
{
private const VALIDATION_FUNCTIONS = [
'bool' => 'is_bool',
'boolean' => 'is_bool',
'int' => 'is_int',
'integer' => 'is_int',
'long' => 'is_int',
'float' => 'is_float',
'double' => 'is_float',
'real' => 'is_float',
'numeric' => 'is_numeric',
'string' => 'is_string',
'scalar' => 'is_scalar',
'array' => 'is_array',
'iterable' => 'is_iterable',
'countable' => 'is_countable',
'callable' => 'is_callable',
'object' => 'is_object',
'resource' => 'is_resource',
];
/**
* The names of all defined options.
*/
@@ -71,6 +91,11 @@ class OptionsResolver implements Options
*/
private $allowedTypes = [];
/**
* A list of info messages for each option.
*/
private $info = [];
/**
* A list of closures for evaluating lazy options.
*/
@@ -103,11 +128,17 @@ class OptionsResolver implements Options
*/
private $locked = false;
private static $typeAliases = [
'boolean' => 'bool',
'integer' => 'int',
'double' => 'float',
];
private $parentsOptions = [];
/**
* Whether the whole options definition is marked as array prototype.
*/
private $prototype;
/**
* The prototype array's index that is being read.
*/
private $prototypeIndex;
/**
* Sets the default value of a given option.
@@ -163,7 +194,7 @@ class OptionsResolver implements Options
*
* @throws AccessException If called from a lazy option or normalizer
*/
public function setDefault($option, $value)
public function setDefault(string $option, $value)
{
// Setting is not possible once resolving starts, because then lazy
// options could manipulate the state of the object, leading to
@@ -178,7 +209,7 @@ class OptionsResolver implements Options
$reflClosure = new \ReflectionFunction($value);
$params = $reflClosure->getParameters();
if (isset($params[0]) && null !== ($class = $params[0]->getClass()) && Options::class === $class->name) {
if (isset($params[0]) && Options::class === $this->getParameterClassName($params[0])) {
// Initialize the option if no previous value exists
if (!isset($this->defaults[$option])) {
$this->defaults[$option] = null;
@@ -199,7 +230,7 @@ class OptionsResolver implements Options
return $this;
}
if (isset($params[0]) && null !== ($class = $params[0]->getClass()) && self::class === $class->name && (!isset($params[1]) || (null !== ($class = $params[1]->getClass()) && Options::class === $class->name))) {
if (isset($params[0]) && null !== ($type = $params[0]->getType()) && self::class === $type->getName() && (!isset($params[1]) || (($type = $params[1]->getType()) instanceof \ReflectionNamedType && Options::class === $type->getName()))) {
// Store closure for later evaluation
$this->nested[$option][] = $value;
$this->defaults[$option] = [];
@@ -230,10 +261,6 @@ class OptionsResolver implements Options
}
/**
* Sets a list of default values.
*
* @param array $defaults The default values to set
*
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
@@ -253,11 +280,9 @@ class OptionsResolver implements Options
* Returns true if {@link setDefault()} was called for this option.
* An option is also considered set if it was set to null.
*
* @param string $option The option name
*
* @return bool Whether a default value is set
* @return bool
*/
public function hasDefault($option)
public function hasDefault(string $option)
{
return \array_key_exists($option, $this->defaults);
}
@@ -290,11 +315,9 @@ class OptionsResolver implements Options
*
* An option is required if it was passed to {@link setRequired()}.
*
* @param string $option The name of the option
*
* @return bool Whether the option is required
* @return bool
*/
public function isRequired($option)
public function isRequired(string $option)
{
return isset($this->required[$option]);
}
@@ -302,7 +325,7 @@ class OptionsResolver implements Options
/**
* Returns the names of all required options.
*
* @return string[] The names of the required options
* @return string[]
*
* @see isRequired()
*/
@@ -318,11 +341,9 @@ class OptionsResolver implements Options
* to {@link setDefault()}. This option must be passed explicitly to
* {@link resolve()}, otherwise an exception will be thrown.
*
* @param string $option The name of the option
*
* @return bool Whether the option is missing
* @return bool
*/
public function isMissing($option)
public function isMissing(string $option)
{
return isset($this->required[$option]) && !\array_key_exists($option, $this->defaults);
}
@@ -330,9 +351,7 @@ class OptionsResolver implements Options
/**
* Returns the names of all options missing a default value.
*
* @return string[] The names of the missing options
*
* @see isMissing()
* @return string[]
*/
public function getMissingOptions()
{
@@ -371,11 +390,9 @@ class OptionsResolver implements Options
* Returns true for any option passed to {@link setDefault()},
* {@link setRequired()} or {@link setDefined()}.
*
* @param string $option The option name
*
* @return bool Whether the option is defined
* @return bool
*/
public function isDefined($option)
public function isDefined(string $option)
{
return isset($this->defined[$option]);
}
@@ -383,7 +400,7 @@ class OptionsResolver implements Options
/**
* Returns the names of all defined options.
*
* @return string[] The names of the defined options
* @return string[]
*
* @see isDefined()
*/
@@ -414,28 +431,49 @@ class OptionsResolver implements Options
* passed to the closure is the value of the option after validating it
* and before normalizing it.
*
* @param string|\Closure $deprecationMessage
* @param string $package The name of the composer package that is triggering the deprecation
* @param string $version The version of the package that introduced the deprecation
* @param string|\Closure $message The deprecation message to use
*
* @return $this
*/
public function setDeprecated(string $option, $deprecationMessage = 'The option "%name%" is deprecated.'): self
public function setDeprecated(string $option/*, string $package, string $version, $message = 'The option "%name%" is deprecated.' */): self
{
if ($this->locked) {
throw new AccessException('Options cannot be deprecated from a lazy option or normalizer.');
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist, defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist, defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}
if (!\is_string($deprecationMessage) && !$deprecationMessage instanceof \Closure) {
throw new InvalidArgumentException(sprintf('Invalid type for deprecation message argument, expected string or \Closure, but got "%s".', \gettype($deprecationMessage)));
$args = \func_get_args();
if (\func_num_args() < 3) {
trigger_deprecation('symfony/options-resolver', '5.1', 'The signature of method "%s()" requires 2 new arguments: "string $package, string $version", not defining them is deprecated.', __METHOD__);
$message = $args[1] ?? 'The option "%name%" is deprecated.';
$package = $version = '';
} else {
$package = $args[1];
$version = $args[2];
$message = $args[3] ?? 'The option "%name%" is deprecated.';
}
if (!\is_string($message) && !$message instanceof \Closure) {
throw new InvalidArgumentException(sprintf('Invalid type for deprecation message argument, expected string or \Closure, but got "%s".', get_debug_type($message)));
}
// ignore if empty string
if ('' === $deprecationMessage) {
if ('' === $message) {
return $this;
}
$this->deprecated[$option] = $deprecationMessage;
$this->deprecated[$option] = [
'package' => $package,
'version' => $version,
'message' => $message,
];
// Make sure the option is processed
unset($this->resolved[$option]);
@@ -466,22 +504,19 @@ class OptionsResolver implements Options
*
* The resolved option value is set to the return value of the closure.
*
* @param string $option The option name
* @param \Closure $normalizer The normalizer
*
* @return $this
*
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
*/
public function setNormalizer($option, \Closure $normalizer)
public function setNormalizer(string $option, \Closure $normalizer)
{
if ($this->locked) {
throw new AccessException('Normalizers cannot be set from a lazy option or normalizer.');
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}
$this->normalizers[$option] = [$normalizer];
@@ -510,10 +545,6 @@ class OptionsResolver implements Options
*
* The resolved option value is set to the return value of the closure.
*
* @param string $option The option name
* @param \Closure $normalizer The normalizer
* @param bool $forcePrepend If set to true, prepend instead of appending
*
* @return $this
*
* @throws UndefinedOptionsException If the option is undefined
@@ -526,10 +557,11 @@ class OptionsResolver implements Options
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}
if ($forcePrepend) {
$this->normalizers[$option] = $this->normalizers[$option] ?? [];
array_unshift($this->normalizers[$option], $normalizer);
} else {
$this->normalizers[$option][] = $normalizer;
@@ -562,14 +594,14 @@ class OptionsResolver implements Options
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
*/
public function setAllowedValues($option, $allowedValues)
public function setAllowedValues(string $option, $allowedValues)
{
if ($this->locked) {
throw new AccessException('Allowed values cannot be set from a lazy option or normalizer.');
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}
$this->allowedValues[$option] = \is_array($allowedValues) ? $allowedValues : [$allowedValues];
@@ -603,14 +635,14 @@ class OptionsResolver implements Options
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
*/
public function addAllowedValues($option, $allowedValues)
public function addAllowedValues(string $option, $allowedValues)
{
if ($this->locked) {
throw new AccessException('Allowed values cannot be added from a lazy option or normalizer.');
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}
if (!\is_array($allowedValues)) {
@@ -636,7 +668,6 @@ class OptionsResolver implements Options
* acceptable. Additionally, fully-qualified class or interface names may
* be passed.
*
* @param string $option The option name
* @param string|string[] $allowedTypes One or more accepted types
*
* @return $this
@@ -644,14 +675,14 @@ class OptionsResolver implements Options
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
*/
public function setAllowedTypes($option, $allowedTypes)
public function setAllowedTypes(string $option, $allowedTypes)
{
if ($this->locked) {
throw new AccessException('Allowed types cannot be set from a lazy option or normalizer.');
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}
$this->allowedTypes[$option] = (array) $allowedTypes;
@@ -671,7 +702,6 @@ class OptionsResolver implements Options
* acceptable. Additionally, fully-qualified class or interface names may
* be passed.
*
* @param string $option The option name
* @param string|string[] $allowedTypes One or more accepted types
*
* @return $this
@@ -679,14 +709,14 @@ class OptionsResolver implements Options
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
*/
public function addAllowedTypes($option, $allowedTypes)
public function addAllowedTypes(string $option, $allowedTypes)
{
if ($this->locked) {
throw new AccessException('Allowed types cannot be added from a lazy option or normalizer.');
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}
if (!isset($this->allowedTypes[$option])) {
@@ -701,6 +731,80 @@ class OptionsResolver implements Options
return $this;
}
/**
* Defines an option configurator with the given name.
*/
public function define(string $option): OptionConfigurator
{
if (isset($this->defined[$option])) {
throw new OptionDefinitionException(sprintf('The option "%s" is already defined.', $option));
}
return new OptionConfigurator($option, $this);
}
/**
* Sets an info message for an option.
*
* @return $this
*
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
*/
public function setInfo(string $option, string $info): self
{
if ($this->locked) {
throw new AccessException('The Info message cannot be set from a lazy option or normalizer.');
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}
$this->info[$option] = $info;
return $this;
}
/**
* Gets the info message for an option.
*/
public function getInfo(string $option): ?string
{
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}
return $this->info[$option] ?? null;
}
/**
* Marks the whole options definition as array prototype.
*
* @return $this
*
* @throws AccessException If called from a lazy option, a normalizer or a root definition
*/
public function setPrototype(bool $prototype): self
{
if ($this->locked) {
throw new AccessException('The prototype property cannot be set from a lazy option or normalizer.');
}
if (null === $this->prototype && $prototype) {
throw new AccessException('The prototype property cannot be set from a root definition.');
}
$this->prototype = $prototype;
return $this;
}
public function isPrototype(): bool
{
return $this->prototype ?? false;
}
/**
* Removes the option with the given name.
*
@@ -720,7 +824,7 @@ class OptionsResolver implements Options
foreach ((array) $optionNames as $option) {
unset($this->defined[$option], $this->defaults[$option], $this->required[$option], $this->resolved[$option]);
unset($this->lazy[$option], $this->normalizers[$option], $this->allowedTypes[$option], $this->allowedValues[$option]);
unset($this->lazy[$option], $this->normalizers[$option], $this->allowedTypes[$option], $this->allowedValues[$option], $this->info[$option]);
}
return $this;
@@ -749,6 +853,7 @@ class OptionsResolver implements Options
$this->allowedTypes = [];
$this->allowedValues = [];
$this->deprecated = [];
$this->info = [];
return $this;
}
@@ -764,9 +869,7 @@ class OptionsResolver implements Options
* - Options have invalid types;
* - Options have invalid values.
*
* @param array $options A map of option names to values
*
* @return array The merged and validated options
* @return array
*
* @throws UndefinedOptionsException If an option name is undefined
* @throws InvalidOptionsException If an option doesn't fulfill the
@@ -793,7 +896,7 @@ class OptionsResolver implements Options
ksort($clone->defined);
ksort($diff);
throw new UndefinedOptionsException(sprintf((\count($diff) > 1 ? 'The options "%s" do not exist.' : 'The option "%s" does not exist.').' Defined options are: "%s".', implode('", "', array_keys($diff)), implode('", "', array_keys($clone->defined))));
throw new UndefinedOptionsException(sprintf((\count($diff) > 1 ? 'The options "%s" do not exist.' : 'The option "%s" does not exist.').' Defined options are: "%s".', $this->formatOptions(array_keys($diff)), implode('", "', array_keys($clone->defined))));
}
// Override options set by the user
@@ -809,7 +912,7 @@ class OptionsResolver implements Options
if (\count($diff) > 0) {
ksort($diff);
throw new MissingOptionsException(sprintf(\count($diff) > 1 ? 'The required options "%s" are missing.' : 'The required option "%s" is missing.', implode('", "', array_keys($diff))));
throw new MissingOptionsException(sprintf(\count($diff) > 1 ? 'The required options "%s" are missing.' : 'The required option "%s" is missing.', $this->formatOptions(array_keys($diff))));
}
// Lock the container
@@ -827,10 +930,9 @@ class OptionsResolver implements Options
/**
* Returns the resolved value of an option.
*
* @param string $option The option name
* @param bool $triggerDeprecation Whether to trigger the deprecation or not (true by default)
* @param bool $triggerDeprecation Whether to trigger the deprecation or not (true by default)
*
* @return mixed The option value
* @return mixed
*
* @throws AccessException If accessing this method outside of
* {@link resolve()}
@@ -840,18 +942,17 @@ class OptionsResolver implements Options
* @throws OptionDefinitionException If there is a cyclic dependency between
* lazy options and/or normalizers
*/
public function offsetGet($option/*, bool $triggerDeprecation = true*/)
#[\ReturnTypeWillChange]
public function offsetGet($option, bool $triggerDeprecation = true)
{
if (!$this->locked) {
throw new AccessException('Array access is only supported within closures of lazy options and normalizers.');
}
$triggerDeprecation = 1 === \func_num_args() || \func_get_arg(1);
// Shortcut for resolved options
if (isset($this->resolved[$option]) || \array_key_exists($option, $this->resolved)) {
if ($triggerDeprecation && isset($this->deprecated[$option]) && (isset($this->given[$option]) || $this->calling) && \is_string($this->deprecated[$option])) {
@trigger_error(strtr($this->deprecated[$option], ['%name%' => $option]), E_USER_DEPRECATED);
if ($triggerDeprecation && isset($this->deprecated[$option]) && (isset($this->given[$option]) || $this->calling) && \is_string($this->deprecated[$option]['message'])) {
trigger_deprecation($this->deprecated[$option]['package'], $this->deprecated[$option]['version'], strtr($this->deprecated[$option]['message'], ['%name%' => $option]));
}
return $this->resolved[$option];
@@ -860,10 +961,10 @@ class OptionsResolver implements Options
// Check whether the option is set at all
if (!isset($this->defaults[$option]) && !\array_key_exists($option, $this->defaults)) {
if (!isset($this->defined[$option])) {
throw new NoSuchOptionException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
throw new NoSuchOptionException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}
throw new NoSuchOptionException(sprintf('The optional option "%s" has no value set. You should make sure it is set with "isset" before reading it.', $option));
throw new NoSuchOptionException(sprintf('The optional option "%s" has no value set. You should make sure it is set with "isset" before reading it.', $this->formatOptions([$option])));
}
$value = $this->defaults[$option];
@@ -872,22 +973,40 @@ class OptionsResolver implements Options
if (isset($this->nested[$option])) {
// If the closure is already being called, we have a cyclic dependency
if (isset($this->calling[$option])) {
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', array_keys($this->calling))));
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', $this->formatOptions(array_keys($this->calling))));
}
if (!\is_array($value)) {
throw new InvalidOptionsException(sprintf('The nested option "%s" with value %s is expected to be of type array, but is of type "%s".', $option, $this->formatValue($value), $this->formatTypeOf($value)));
throw new InvalidOptionsException(sprintf('The nested option "%s" with value %s is expected to be of type array, but is of type "%s".', $this->formatOptions([$option]), $this->formatValue($value), get_debug_type($value)));
}
// The following section must be protected from cyclic calls.
$this->calling[$option] = true;
try {
$resolver = new self();
$resolver->prototype = false;
$resolver->parentsOptions = $this->parentsOptions;
$resolver->parentsOptions[] = $option;
foreach ($this->nested[$option] as $closure) {
$closure($resolver, $this);
}
$value = $resolver->resolve($value);
if ($resolver->prototype) {
$values = [];
foreach ($value as $index => $prototypeValue) {
if (!\is_array($prototypeValue)) {
throw new InvalidOptionsException(sprintf('The value of the option "%s" is expected to be of type array of array, but is of type array of "%s".', $this->formatOptions([$option]), get_debug_type($prototypeValue)));
}
$resolver->prototypeIndex = $index;
$values[$index] = $resolver->resolve($prototypeValue);
}
$value = $values;
} else {
$value = $resolver->resolve($value);
}
} finally {
$resolver->prototypeIndex = null;
unset($this->calling[$option]);
}
}
@@ -897,7 +1016,7 @@ class OptionsResolver implements Options
// If the closure is already being called, we have a cyclic
// dependency
if (isset($this->calling[$option])) {
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', array_keys($this->calling))));
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', $this->formatOptions(array_keys($this->calling))));
}
// The following section must be protected from cyclic
@@ -917,25 +1036,28 @@ class OptionsResolver implements Options
// Validate the type of the resolved option
if (isset($this->allowedTypes[$option])) {
$valid = false;
$valid = true;
$invalidTypes = [];
foreach ($this->allowedTypes[$option] as $type) {
$type = self::$typeAliases[$type] ?? $type;
if ($valid = $this->verifyTypes($type, $value, $invalidTypes)) {
break;
}
}
if (!$valid) {
$keys = array_keys($invalidTypes);
$fmtActualValue = $this->formatValue($value);
$fmtAllowedTypes = implode('" or "', $this->allowedTypes[$option]);
$fmtProvidedTypes = implode('|', array_keys($invalidTypes));
$allowedContainsArrayType = \count(array_filter($this->allowedTypes[$option], static function ($item) {
return str_ends_with($item, '[]');
})) > 0;
if (1 === \count($keys) && '[]' === substr($keys[0], -2)) {
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but one of the elements is of type "%s".', $option, $this->formatValue($value), implode('" or "', $this->allowedTypes[$option]), $keys[0]));
if (\is_array($value) && $allowedContainsArrayType) {
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but one of the elements is of type "%s".', $this->formatOptions([$option]), $fmtActualValue, $fmtAllowedTypes, $fmtProvidedTypes));
}
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but is of type "%s".', $option, $this->formatValue($value), implode('" or "', $this->allowedTypes[$option]), implode('|', array_keys($invalidTypes))));
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but is of type "%s".', $this->formatOptions([$option]), $fmtActualValue, $fmtAllowedTypes, $fmtProvidedTypes));
}
}
@@ -977,33 +1099,38 @@ class OptionsResolver implements Options
);
}
if (isset($this->info[$option])) {
$message .= sprintf(' Info: %s.', $this->info[$option]);
}
throw new InvalidOptionsException($message);
}
}
// Check whether the option is deprecated
// and it is provided by the user or is being called from a lazy evaluation
if ($triggerDeprecation && isset($this->deprecated[$option]) && (isset($this->given[$option]) || ($this->calling && \is_string($this->deprecated[$option])))) {
$deprecationMessage = $this->deprecated[$option];
if ($triggerDeprecation && isset($this->deprecated[$option]) && (isset($this->given[$option]) || ($this->calling && \is_string($this->deprecated[$option]['message'])))) {
$deprecation = $this->deprecated[$option];
$message = $this->deprecated[$option]['message'];
if ($deprecationMessage instanceof \Closure) {
if ($message instanceof \Closure) {
// If the closure is already being called, we have a cyclic dependency
if (isset($this->calling[$option])) {
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', array_keys($this->calling))));
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', $this->formatOptions(array_keys($this->calling))));
}
$this->calling[$option] = true;
try {
if (!\is_string($deprecationMessage = $deprecationMessage($this, $value))) {
throw new InvalidOptionsException(sprintf('Invalid type for deprecation message, expected string but got "%s", return an empty string to ignore.', \gettype($deprecationMessage)));
if (!\is_string($message = $message($this, $value))) {
throw new InvalidOptionsException(sprintf('Invalid type for deprecation message, expected string but got "%s", return an empty string to ignore.', get_debug_type($message)));
}
} finally {
unset($this->calling[$option]);
}
}
if ('' !== $deprecationMessage) {
@trigger_error(strtr($deprecationMessage, ['%name%' => $option]), E_USER_DEPRECATED);
if ('' !== $message) {
trigger_deprecation($deprecation['package'], $deprecation['version'], strtr($message, ['%name%' => $option]));
}
}
@@ -1012,7 +1139,7 @@ class OptionsResolver implements Options
// If the closure is already being called, we have a cyclic
// dependency
if (isset($this->calling[$option])) {
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', array_keys($this->calling))));
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', $this->formatOptions(array_keys($this->calling))));
}
// The following section must be protected from cyclic
@@ -1040,26 +1167,23 @@ class OptionsResolver implements Options
{
if (\is_array($value) && '[]' === substr($type, -2)) {
$type = substr($type, 0, -2);
$valid = true;
foreach ($value as $val) {
if (!$this->verifyTypes($type, $val, $invalidTypes, $level + 1)) {
return false;
$valid = false;
}
}
return $valid;
}
if (('null' === $type && null === $value) || (isset(self::VALIDATION_FUNCTIONS[$type]) ? self::VALIDATION_FUNCTIONS[$type]($value) : $value instanceof $type)) {
return true;
}
if (('null' === $type && null === $value) || (\function_exists($func = 'is_'.$type) && $func($value)) || $value instanceof $type) {
return true;
}
if (!$invalidTypes) {
$suffix = '';
while (\strlen($suffix) < $level * 2) {
$suffix .= '[]';
}
$invalidTypes[$this->formatTypeOf($value).$suffix] = true;
if (!$invalidTypes || $level > 0) {
$invalidTypes[get_debug_type($value)] = true;
}
return false;
@@ -1070,12 +1194,13 @@ class OptionsResolver implements Options
*
* @param string $option The option name
*
* @return bool Whether the option is set
* @return bool
*
* @throws AccessException If accessing this method outside of {@link resolve()}
*
* @see \ArrayAccess::offsetExists()
*/
#[\ReturnTypeWillChange]
public function offsetExists($option)
{
if (!$this->locked) {
@@ -1088,8 +1213,11 @@ class OptionsResolver implements Options
/**
* Not supported.
*
* @return void
*
* @throws AccessException
*/
#[\ReturnTypeWillChange]
public function offsetSet($option, $value)
{
throw new AccessException('Setting options via array access is not supported. Use setDefault() instead.');
@@ -1098,8 +1226,11 @@ class OptionsResolver implements Options
/**
* Not supported.
*
* @return void
*
* @throws AccessException
*/
#[\ReturnTypeWillChange]
public function offsetUnset($option)
{
throw new AccessException('Removing options via array access is not supported. Use remove() instead.');
@@ -1110,12 +1241,13 @@ class OptionsResolver implements Options
*
* This may be only a subset of the defined options.
*
* @return int Number of options
* @return int
*
* @throws AccessException If accessing this method outside of {@link resolve()}
*
* @see \Countable::count()
*/
#[\ReturnTypeWillChange]
public function count()
{
if (!$this->locked) {
@@ -1125,18 +1257,6 @@ class OptionsResolver implements Options
return \count($this->defaults);
}
/**
* Returns a string representation of the type of the value.
*
* @param mixed $value The value to return the type of
*
* @return string The type of the value
*/
private function formatTypeOf($value): string
{
return \is_object($value) ? \get_class($value) : \gettype($value);
}
/**
* Returns a string representation of the value.
*
@@ -1195,4 +1315,33 @@ class OptionsResolver implements Options
return implode(', ', $values);
}
private function formatOptions(array $options): string
{
if ($this->parentsOptions) {
$prefix = array_shift($this->parentsOptions);
if ($this->parentsOptions) {
$prefix .= sprintf('[%s]', implode('][', $this->parentsOptions));
}
if ($this->prototype && null !== $this->prototypeIndex) {
$prefix .= sprintf('[%s]', $this->prototypeIndex);
}
$options = array_map(static function (string $option) use ($prefix): string {
return sprintf('%s[%s]', $prefix, $option);
}, $options);
}
return implode('", "', $options);
}
private function getParameterClassName(\ReflectionParameter $parameter): ?string
{
if (!($type = $parameter->getType()) instanceof \ReflectionNamedType || $type->isBuiltin()) {
return null;
}
return $type->getName();
}
}