From c21a7be1431097ffd9527dc680dd7896b0daede1 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 29 Jun 2021 12:31:08 +0200 Subject: [PATCH] Add more types --- .../Validator/ConstraintViolation.php | 2 +- .../Test/ConstraintValidatorTestCase.php | 53 +++++++++---------- .../ContextualValidatorInterface.php | 4 +- .../Violation/ConstraintViolationBuilder.php | 13 ++--- .../Component/VarDumper/Caster/ClassStub.php | 4 +- .../Component/VarDumper/Caster/ConstStub.php | 2 +- .../Component/VarDumper/Caster/DOMCaster.php | 2 +- .../VarDumper/Caster/ResourceCaster.php | 7 +-- .../Component/VarDumper/Caster/SplCaster.php | 2 +- .../VarDumper/Cloner/AbstractCloner.php | 1 - .../Component/VarDumper/Cloner/Data.php | 8 +-- .../Workflow/Dumper/GraphvizDumper.php | 2 +- src/Symfony/Component/Yaml/Inline.php | 2 +- 13 files changed, 44 insertions(+), 58 deletions(-) diff --git a/src/Symfony/Component/Validator/ConstraintViolation.php b/src/Symfony/Component/Validator/ConstraintViolation.php index 034c61aa95392..3b472e7d88ed6 100644 --- a/src/Symfony/Component/Validator/ConstraintViolation.php +++ b/src/Symfony/Component/Validator/ConstraintViolation.php @@ -49,7 +49,7 @@ class ConstraintViolation implements ConstraintViolationInterface * caused the violation * @param mixed $cause The cause of the violation */ - public function __construct(string|\Stringable $message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, string $code = null, Constraint $constraint = null, mixed $cause = null) + public function __construct(string|\Stringable $message, ?string $messageTemplate, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, int $plural = null, string $code = null, Constraint $constraint = null, mixed $cause = null) { $this->message = $message; $this->messageTemplate = $messageTemplate; diff --git a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php index d77fd17837ec5..dffd974b51510 100644 --- a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php @@ -19,6 +19,7 @@ use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Constraints\GroupSequence; use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\Constraints\Valid; use Symfony\Component\Validator\ConstraintValidatorInterface; @@ -168,13 +169,13 @@ protected function createContext() return $context; } - protected function setGroup($group) + protected function setGroup(?string $group) { $this->group = $group; $this->context->setGroup($group); } - protected function setObject($object) + protected function setObject(mixed $object) { $this->object = $object; $this->metadata = \is_object($object) @@ -184,7 +185,7 @@ protected function setObject($object) $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath); } - protected function setProperty($object, $property) + protected function setProperty(mixed $object, string $property) { $this->object = $object; $this->metadata = \is_object($object) @@ -194,20 +195,20 @@ protected function setProperty($object, $property) $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath); } - protected function setValue($value) + protected function setValue(mixed $value) { $this->value = $value; $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath); } - protected function setRoot($root) + protected function setRoot(mixed $root) { $this->root = $root; $this->context = $this->createContext(); $this->validator->initialize($this->context); } - protected function setPropertyPath($propertyPath) + protected function setPropertyPath(string $propertyPath) { $this->propertyPath = $propertyPath; $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath); @@ -219,7 +220,7 @@ protected function expectNoValidate() $validator->expectNoValidate(); } - protected function expectValidateAt($i, $propertyPath, $value, $group) + protected function expectValidateAt(int $i, string $propertyPath, mixed $value, string|GroupSequence|array|null $group) { $validator = $this->context->getValidator()->inContext($this->context); $validator->expectValidation($i, $propertyPath, $value, $group, function ($passedConstraints) { @@ -230,7 +231,7 @@ protected function expectValidateAt($i, $propertyPath, $value, $group) }); } - protected function expectValidateValue(int $i, $value, array $constraints = [], $group = null) + protected function expectValidateValue(int $i, mixed $value, Constraint|array $constraints = [], string|GroupSequence|array|null $group = null) { $contextualValidator = $this->context->getValidator()->inContext($this->context); $contextualValidator->expectValidation($i, '', $value, $group, function ($passedConstraints) use ($constraints) { @@ -242,7 +243,7 @@ protected function expectValidateValue(int $i, $value, array $constraints = [], }); } - protected function expectFailingValueValidation(int $i, $value, array $constraints, $group, ConstraintViolationInterface $violation) + protected function expectFailingValueValidation(int $i, mixed $value, Constraint|array $constraints, string|GroupSequence|array|null $group, ConstraintViolationInterface $violation) { $contextualValidator = $this->context->getValidator()->inContext($this->context); $contextualValidator->expectValidation($i, '', $value, $group, function ($passedConstraints) use ($constraints) { @@ -254,7 +255,7 @@ protected function expectFailingValueValidation(int $i, $value, array $constrain }, $violation); } - protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group = null) + protected function expectValidateValueAt(int $i, string $propertyPath, mixed $value, Constraint|array $constraints, string|GroupSequence|array|null $group = null) { $contextualValidator = $this->context->getValidator()->inContext($this->context); $contextualValidator->expectValidation($i, $propertyPath, $value, $group, function ($passedConstraints) use ($constraints) { @@ -262,7 +263,7 @@ protected function expectValidateValueAt($i, $propertyPath, $value, $constraints }); } - protected function expectViolationsAt($i, $value, Constraint $constraint) + protected function expectViolationsAt(int $i, mixed $value, Constraint $constraint) { $context = $this->createContext(); @@ -283,11 +284,9 @@ protected function assertNoViolation() } /** - * @param $message - * * @return ConstraintViolationAssertion */ - protected function buildViolation($message) + protected function buildViolation(string|\Stringable $message) { return new ConstraintViolationAssertion($this->context, $message, $this->constraint); } @@ -334,7 +333,7 @@ public function atPath(string $path) return $this; } - public function setParameter(string $key, $value) + public function setParameter(string $key, string $value) { $this->parameters[$key] = $value; @@ -348,14 +347,14 @@ public function setParameters(array $parameters) return $this; } - public function setTranslationDomain($translationDomain) + public function setTranslationDomain(?string $translationDomain) { // no-op for BC return $this; } - public function setInvalidValue($invalidValue) + public function setInvalidValue(mixed $invalidValue) { $this->invalidValue = $invalidValue; @@ -376,7 +375,7 @@ public function setCode(string $code) return $this; } - public function setCause($cause) + public function setCause(mixed $cause) { $this->cause = $cause; @@ -445,11 +444,11 @@ public function __construct(ExecutionContextInterface $context) $this->context = $context; } - public function atPath($path) + public function atPath(string $path) { } - public function doAtPath($path) + public function doAtPath(string $path) { Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.'); @@ -462,11 +461,11 @@ public function doAtPath($path) return $this; } - public function validate($value, $constraints = null, $groups = null) + public function validate(mixed $value, Constraint|array|null $constraints = null, string|GroupSequence|array|null $groups = null) { } - public function doValidate($value, $constraints = null, $groups = null) + public function doValidate(mixed $value, Constraint|array|null $constraints = null, string|GroupSequence|array|null $groups = null) { Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.'); @@ -487,20 +486,20 @@ public function doValidate($value, $constraints = null, $groups = null) return $this; } - public function validateProperty($object, $propertyName, $groups = null) + public function validateProperty(object $object, string $propertyName, string|GroupSequence|array|null $groups = null) { } - public function doValidateProperty($object, $propertyName, $groups = null) + public function doValidateProperty(object $object, string $propertyName, string|GroupSequence|array|null $groups = null) { return $this; } - public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null) + public function validatePropertyValue(object|string $objectOrClass, string $propertyName, mixed $value, string|GroupSequence|array|null $groups = null) { } - public function doValidatePropertyValue($objectOrClass, $propertyName, $value, $groups = null) + public function doValidatePropertyValue(object|string $objectOrClass, string $propertyName, mixed $value, string|GroupSequence|array|null $groups = null) { return $this; } @@ -519,7 +518,7 @@ public function expectNoValidate() $this->expectNoValidate = true; } - public function expectValidation($call, $propertyPath, $value, $group, $constraints, ConstraintViolationInterface $violation = null) + public function expectValidation(string $call, string $propertyPath, mixed $value, string|GroupSequence|array|null $group, callable $constraints, ConstraintViolationInterface $violation = null) { $this->expectedAtPath[$call] = $propertyPath; $this->expectedValidate[$call] = [$value, $group, $constraints, $violation]; diff --git a/src/Symfony/Component/Validator/Validator/ContextualValidatorInterface.php b/src/Symfony/Component/Validator/Validator/ContextualValidatorInterface.php index 6033def0de2f4..54859e96186b7 100644 --- a/src/Symfony/Component/Validator/Validator/ContextualValidatorInterface.php +++ b/src/Symfony/Component/Validator/Validator/ContextualValidatorInterface.php @@ -39,12 +39,12 @@ public function atPath(string $path); * {@link \Symfony\Component\Validator\Constraints\Valid} is assumed. * * @param mixed $value The value to validate - * @param Constraint|Constraint[] $constraints The constraint(s) to validate against + * @param Constraint|Constraint[]|null $constraints The constraint(s) to validate against * @param string|GroupSequence|array|null $groups The validation groups to validate. If none is given, "Default" is assumed * * @return $this */ - public function validate(mixed $value, Constraint|array $constraints = null, string|GroupSequence|array|null $groups = null); + public function validate(mixed $value, Constraint|array|null $constraints = null, string|GroupSequence|array|null $groups = null); /** * Validates a property of an object against the constraints specified diff --git a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php index 8ed9a7652d054..99731f925f576 100644 --- a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php +++ b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php @@ -37,16 +37,9 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface private $plural; private $constraint; private $code; - - /** - * @var mixed - */ private $cause; - /** - * @param string $message The error message as a string or a stringable object - */ - public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string $translationDomain = null) + public function __construct(ConstraintViolationList $violations, Constraint $constraint, string|\Stringable $message, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string $translationDomain = null) { $this->violations = $violations; $this->message = $message; @@ -102,7 +95,7 @@ public function setTranslationDomain(string $translationDomain) /** * {@inheritdoc} */ - public function setInvalidValue($invalidValue) + public function setInvalidValue(mixed $invalidValue) { $this->invalidValue = $invalidValue; @@ -132,7 +125,7 @@ public function setCode(?string $code) /** * {@inheritdoc} */ - public function setCause($cause) + public function setCause(mixed $cause) { $this->cause = $cause; diff --git a/src/Symfony/Component/VarDumper/Caster/ClassStub.php b/src/Symfony/Component/VarDumper/Caster/ClassStub.php index 612a7ca2d9933..0431fa6baa5c3 100644 --- a/src/Symfony/Component/VarDumper/Caster/ClassStub.php +++ b/src/Symfony/Component/VarDumper/Caster/ClassStub.php @@ -24,7 +24,7 @@ class ClassStub extends ConstStub * @param string $identifier A PHP identifier, e.g. a class, method, interface, etc. name * @param callable $callable The callable targeted by the identifier when it is ambiguous or not a real PHP identifier */ - public function __construct(string $identifier, $callable = null) + public function __construct(string $identifier, callable|array|string $callable = null) { $this->value = $identifier; @@ -87,7 +87,7 @@ public function __construct(string $identifier, $callable = null) } } - public static function wrapCallable($callable) + public static function wrapCallable(mixed $callable) { if (\is_object($callable) || !\is_callable($callable)) { return $callable; diff --git a/src/Symfony/Component/VarDumper/Caster/ConstStub.php b/src/Symfony/Component/VarDumper/Caster/ConstStub.php index 0801bad7680d9..d7d1812bd0ea0 100644 --- a/src/Symfony/Component/VarDumper/Caster/ConstStub.php +++ b/src/Symfony/Component/VarDumper/Caster/ConstStub.php @@ -20,7 +20,7 @@ */ class ConstStub extends Stub { - public function __construct(string $name, $value = null) + public function __construct(string $name, string|int|float $value = null) { $this->class = $name; $this->value = 1 < \func_num_args() ? $value : $name; diff --git a/src/Symfony/Component/VarDumper/Caster/DOMCaster.php b/src/Symfony/Component/VarDumper/Caster/DOMCaster.php index 5644e489f3614..4dd16e0ee7461 100644 --- a/src/Symfony/Component/VarDumper/Caster/DOMCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/DOMCaster.php @@ -82,7 +82,7 @@ public static function castLength($dom, array $a, Stub $stub, bool $isNested) return $a; } - public static function castImplementation($dom, array $a, Stub $stub, bool $isNested) + public static function castImplementation(\DOMImplementation $dom, array $a, Stub $stub, bool $isNested) { $a += [ Caster::PREFIX_VIRTUAL.'Core' => '1.0', diff --git a/src/Symfony/Component/VarDumper/Caster/ResourceCaster.php b/src/Symfony/Component/VarDumper/Caster/ResourceCaster.php index 6ae908524f6c1..3d2a9a4dc9316 100644 --- a/src/Symfony/Component/VarDumper/Caster/ResourceCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/ResourceCaster.php @@ -22,12 +22,7 @@ */ class ResourceCaster { - /** - * @param \CurlHandle|resource $h - * - * @return array - */ - public static function castCurl($h, array $a, Stub $stub, bool $isNested) + public static function castCurl(\CurlHandle $h, array $a, Stub $stub, bool $isNested) { return curl_getinfo($h); } diff --git a/src/Symfony/Component/VarDumper/Caster/SplCaster.php b/src/Symfony/Component/VarDumper/Caster/SplCaster.php index c0dbecfc6c77f..a51cace1d57bd 100644 --- a/src/Symfony/Component/VarDumper/Caster/SplCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/SplCaster.php @@ -211,7 +211,7 @@ public static function castWeakReference(\WeakReference $c, array $a, Stub $stub return $a; } - private static function castSplArray($c, array $a, Stub $stub, bool $isNested): array + private static function castSplArray(\ArrayObject|\ArrayIterator $c, array $a, Stub $stub, bool $isNested): array { $prefix = Caster::PREFIX_VIRTUAL; $flags = $c->getFlags(); diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index 361201c030e30..d84552dae1099 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -150,7 +150,6 @@ abstract class AbstractCloner implements ClonerInterface 'Symfony\Component\VarDumper\Caster\DsPairStub' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPairStub'], 'CurlHandle' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'], - ':curl' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'], ':dba' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'], ':dba persistent' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'], diff --git a/src/Symfony/Component/VarDumper/Cloner/Data.php b/src/Symfony/Component/VarDumper/Cloner/Data.php index e285bc4b87709..578fbf51b6873 100644 --- a/src/Symfony/Component/VarDumper/Cloner/Data.php +++ b/src/Symfony/Component/VarDumper/Cloner/Data.php @@ -150,22 +150,22 @@ public function __isset(string $key) /** * @return bool */ - public function offsetExists($key) + public function offsetExists(mixed $key) { return $this->__isset($key); } - public function offsetGet($key) + public function offsetGet(mixed $key) { return $this->__get($key); } - public function offsetSet($key, $value) + public function offsetSet(mixed $key, mixed $value) { throw new \BadMethodCallException(self::class.' objects are immutable.'); } - public function offsetUnset($key) + public function offsetUnset(mixed $key) { throw new \BadMethodCallException(self::class.' objects are immutable.'); } diff --git a/src/Symfony/Component/Workflow/Dumper/GraphvizDumper.php b/src/Symfony/Component/Workflow/Dumper/GraphvizDumper.php index 56623b55909d1..a8186a8548c07 100644 --- a/src/Symfony/Component/Workflow/Dumper/GraphvizDumper.php +++ b/src/Symfony/Component/Workflow/Dumper/GraphvizDumper.php @@ -245,7 +245,7 @@ protected function dotize(string $id): string /** * @internal */ - protected function escape($value): string + protected function escape(string|bool $value): string { return \is_bool($value) ? ($value ? '1' : '0') : addslashes($value); } diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 125a67159d161..641bbbd65b50a 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -116,7 +116,7 @@ public static function parse(string $value = null, int $flags = 0, array &$refer * * @throws DumpException When trying to dump PHP resource */ - public static function dump($value, int $flags = 0): string + public static function dump(mixed $value, int $flags = 0): string { switch (true) { case \is_resource($value):