Skip to content

[Config] Add parameter types #41575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(LoaderResolverInterface $resolver, array $defaultOpt
/**
* {@inheritdoc}
*/
public function load($resource, string $type = null)
public function load(mixed $resource, string $type = null)
{
if ($this->loading) {
// This can happen if a fatal error occurs in parent::load().
Expand Down
14 changes: 7 additions & 7 deletions src/Symfony/Component/Config/Definition/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
protected $removeExtraKeys = true;
protected $normalizeKeys = true;

public function setNormalizeKeys($normalizeKeys)
public function setNormalizeKeys(bool $normalizeKeys)
{
$this->normalizeKeys = (bool) $normalizeKeys;
$this->normalizeKeys = $normalizeKeys;
}

/**
Expand All @@ -46,7 +46,7 @@ public function setNormalizeKeys($normalizeKeys)
* If you have a mixed key like foo-bar_moo, it will not be altered.
* The key will also not be altered if the target key already exists.
*/
protected function preNormalize($value)
protected function preNormalize(mixed $value)
{
if (!$this->normalizeKeys || !\is_array($value)) {
return $value;
Expand Down Expand Up @@ -200,7 +200,7 @@ public function addChild(NodeInterface $node)
* @throws UnsetKeyException
* @throws InvalidConfigurationException if the node doesn't have enough children
*/
protected function finalizeValue($value)
protected function finalizeValue(mixed $value)
{
if (false === $value) {
throw new UnsetKeyException(sprintf('Unsetting key for path "%s", value: %s.', $this->getPath(), json_encode($value)));
Expand Down Expand Up @@ -246,7 +246,7 @@ protected function finalizeValue($value)
/**
* {@inheritdoc}
*/
protected function validateType($value)
protected function validateType(mixed $value)
{
if (!\is_array($value) && (!$this->allowFalse || false !== $value)) {
$ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected "array", but got "%s"', $this->getPath(), get_debug_type($value)));
Expand All @@ -264,7 +264,7 @@ protected function validateType($value)
*
* @throws InvalidConfigurationException
*/
protected function normalizeValue($value)
protected function normalizeValue(mixed $value)
{
if (false === $value) {
return $value;
Expand Down Expand Up @@ -345,7 +345,7 @@ protected function remapXml(array $value)
* @throws InvalidConfigurationException
* @throws \RuntimeException
*/
protected function mergeValues($leftSide, $rightSide)
protected function mergeValues(mixed $leftSide, mixed $rightSide)
{
if (false === $rightSide) {
// if this is still false after the last config has been merged the
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Config/Definition/BooleanNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BooleanNode extends ScalarNode
/**
* {@inheritdoc}
*/
protected function validateType($value)
protected function validateType(mixed $value)
{
if (!\is_bool($value)) {
$ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected "bool", but got "%s".', $this->getPath(), get_debug_type($value)));
Expand All @@ -39,7 +39,7 @@ protected function validateType($value)
/**
* {@inheritdoc}
*/
protected function isValueEmpty($value)
protected function isValueEmpty(mixed $value)
{
// a boolean value cannot be empty
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function addDefaultsIfNotSet()
*
* @return $this
*/
public function addDefaultChildrenIfNoneSet($children = null)
public function addDefaultChildrenIfNoneSet(int|string|array|null $children = null)
{
$this->addDefaultChildren = $children;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ abstract class NumericNodeDefinition extends ScalarNodeDefinition
/**
* Ensures that the value is smaller than the given reference.
*
* @param int|float $max
*
* @return $this
*
* @throws \InvalidArgumentException when the constraint is inconsistent
*/
public function max($max)
public function max(int|float $max)
{
if (isset($this->min) && $this->min > $max) {
throw new \InvalidArgumentException(sprintf('You cannot define a max(%s) as you already have a min(%s).', $max, $this->min));
Expand All @@ -45,13 +43,11 @@ public function max($max)
/**
* Ensures that the value is bigger than the given reference.
*
* @param int|float $min
*
* @return $this
*
* @throws \InvalidArgumentException when the constraint is inconsistent
*/
public function min($min)
public function min(int|float $min)
{
if (isset($this->max) && $this->max < $min) {
throw new \InvalidArgumentException(sprintf('You cannot define a min(%s) as you already have a max(%s).', $min, $this->max));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,8 @@ private function writeLine(string $text, int $indent = 0)

/**
* Renders the string conversion of the value.
*
* @param mixed $value
*/
private function writeValue($value): string
private function writeValue(mixed $value): string
{
if ('%%%%not_defined%%%%' === $value) {
return '';
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Definition/EnumNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getValues()
/**
* {@inheritdoc}
*/
protected function finalizeValue($value)
protected function finalizeValue(mixed $value)
{
$value = parent::finalizeValue($value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class InvalidConfigurationException extends Exception
private $path;
private $containsHints = false;

public function setPath($path)
public function setPath(string $path)
{
$this->path = $path;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Definition/FloatNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FloatNode extends NumericNode
/**
* {@inheritdoc}
*/
protected function validateType($value)
protected function validateType(mixed $value)
{
// Integers are also accepted, we just cast them
if (\is_int($value)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Definition/IntegerNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class IntegerNode extends NumericNode
/**
* {@inheritdoc}
*/
protected function validateType($value)
protected function validateType(mixed $value)
{
if (!\is_int($value)) {
$ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected "int", but got "%s".', $this->getPath(), get_debug_type($value)));
Expand Down
13 changes: 3 additions & 10 deletions src/Symfony/Component/Config/Definition/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,29 @@ public function getDefaultValue();
/**
* Normalizes a value.
*
* @param mixed $value The value to normalize
*
* @return mixed The normalized value
*
* @throws InvalidTypeException if the value type is invalid
*/
public function normalize($value);
public function normalize(mixed $value);

/**
* Merges two values together.
*
* @param mixed $leftSide
* @param mixed $rightSide
*
* @return mixed The merged value
*
* @throws ForbiddenOverwriteException if the configuration path cannot be overwritten
* @throws InvalidTypeException if the value type is invalid
*/
public function merge($leftSide, $rightSide);
public function merge(mixed $leftSide, mixed $rightSide);

/**
* Finalizes a value.
*
* @param mixed $value The value to finalize
*
* @return mixed The finalized value
*
* @throws InvalidTypeException if the value type is invalid
* @throws InvalidConfigurationException if the value is invalid configuration
*/
public function finalize($value);
public function finalize(mixed $value);
}
10 changes: 3 additions & 7 deletions src/Symfony/Component/Config/Definition/NumericNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ class NumericNode extends ScalarNode
protected $min;
protected $max;

/**
* @param int|float|null $min
* @param int|float|null $max
*/
public function __construct(?string $name, NodeInterface $parent = null, $min = null, $max = null, string $pathSeparator = BaseNode::DEFAULT_PATH_SEPARATOR)
public function __construct(?string $name, NodeInterface $parent = null, int|float|null $min = null, int|float|null $max = null, string $pathSeparator = BaseNode::DEFAULT_PATH_SEPARATOR)
{
parent::__construct($name, $parent, $pathSeparator);
$this->min = $min;
Expand All @@ -37,7 +33,7 @@ public function __construct(?string $name, NodeInterface $parent = null, $min =
/**
* {@inheritdoc}
*/
protected function finalizeValue($value)
protected function finalizeValue(mixed $value)
{
$value = parent::finalizeValue($value);

Expand All @@ -60,7 +56,7 @@ protected function finalizeValue($value)
/**
* {@inheritdoc}
*/
protected function isValueEmpty($value)
protected function isValueEmpty(mixed $value)
{
// a numeric value cannot be empty
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function hasDefaultValue()
*
* @param int|string|array|null $children The number of children|The child name|The children names to be added
*/
public function setAddChildrenIfNoneSet($children = ['defaults'])
public function setAddChildrenIfNoneSet(int|string|array|null $children = ['defaults'])
{
if (null === $children) {
$this->defaultChildren = ['defaults'];
Expand Down Expand Up @@ -165,7 +165,7 @@ public function addChild(NodeInterface $node)
/**
* {@inheritdoc}
*/
protected function finalizeValue($value)
protected function finalizeValue(mixed $value)
{
if (false === $value) {
throw new UnsetKeyException(sprintf('Unsetting key for path "%s", value: %s.', $this->getPath(), json_encode($value)));
Expand Down Expand Up @@ -195,7 +195,7 @@ protected function finalizeValue($value)
*
* @throws DuplicateKeyException
*/
protected function normalizeValue($value)
protected function normalizeValue(mixed $value)
{
if (false === $value) {
return $value;
Expand Down Expand Up @@ -262,7 +262,7 @@ protected function normalizeValue($value)
/**
* {@inheritdoc}
*/
protected function mergeValues($leftSide, $rightSide)
protected function mergeValues(mixed $leftSide, mixed $rightSide)
{
if (false === $rightSide) {
// if this is still false after the last config has been merged the
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Config/Definition/ScalarNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ScalarNode extends VariableNode
/**
* {@inheritdoc}
*/
protected function validateType($value)
protected function validateType(mixed $value)
{
if (!is_scalar($value) && null !== $value) {
$ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected "scalar", but got "%s".', $this->getPath(), get_debug_type($value)));
Expand All @@ -46,7 +46,7 @@ protected function validateType($value)
/**
* {@inheritdoc}
*/
protected function isValueEmpty($value)
protected function isValueEmpty(mixed $value)
{
// assume environment variables are never empty (which in practice is likely to be true during runtime)
// not doing so breaks many configs that are valid today
Expand Down
14 changes: 6 additions & 8 deletions src/Symfony/Component/Config/Definition/VariableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class VariableNode extends BaseNode implements PrototypeNodeInterface
protected $defaultValue;
protected $allowEmptyValue = true;

public function setDefaultValue($value)
public function setDefaultValue(mixed $value)
{
$this->defaultValueSet = true;
$this->defaultValue = $value;
Expand Down Expand Up @@ -72,14 +72,14 @@ public function setName(string $name)
/**
* {@inheritdoc}
*/
protected function validateType($value)
protected function validateType(mixed $value)
{
}

/**
* {@inheritdoc}
*/
protected function finalizeValue($value)
protected function finalizeValue(mixed $value)
{
// deny environment variables only when using custom validators
// this avoids ever passing an empty value to final validation closures
Expand Down Expand Up @@ -109,15 +109,15 @@ protected function finalizeValue($value)
/**
* {@inheritdoc}
*/
protected function normalizeValue($value)
protected function normalizeValue(mixed $value)
{
return $value;
}

/**
* {@inheritdoc}
*/
protected function mergeValues($leftSide, $rightSide)
protected function mergeValues(mixed $leftSide, mixed $rightSide)
{
return $rightSide;
}
Expand All @@ -129,13 +129,11 @@ protected function mergeValues($leftSide, $rightSide)
* method may be overridden by subtypes to better match their understanding
* of empty data.
*
* @param mixed $value
*
* @return bool
*
* @see finalizeValue()
*/
protected function isValueEmpty($value)
protected function isValueEmpty(mixed $value)
{
return empty($value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct(string $resource, string $sourceResource = null, int
parent::__construct($message, $code, $previous);
}

protected function varToString($var)
protected function varToString(mixed $var)
{
if (\is_object($var)) {
return sprintf('Object(%s)', \get_class($var));
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FileLocator implements FileLocatorInterface
/**
* @param string|string[] $paths A path or an array of paths where to look for resources
*/
public function __construct($paths = [])
public function __construct(string|array $paths = [])
{
$this->paths = (array) $paths;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Config/Loader/DelegatingLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(LoaderResolverInterface $resolver)
/**
* {@inheritdoc}
*/
public function load($resource, string $type = null)
public function load(mixed $resource, string $type = null)
{
if (false === $loader = $this->resolver->resolve($resource, $type)) {
throw new LoaderLoadException($resource, null, 0, null, $type);
Expand All @@ -43,7 +43,7 @@ public function load($resource, string $type = null)
/**
* {@inheritdoc}
*/
public function supports($resource, string $type = null)
public function supports(mixed $resource, string $type = null)
{
return false !== $this->resolver->resolve($resource, $type);
}
Expand Down
Loading