add parameter type hints to the Validator component

This commit is contained in:
Christian Flothmann 2019-06-28 09:31:01 +02:00
parent 58d61dc997
commit df3a5cbd11
21 changed files with 71 additions and 71 deletions

View File

@ -28,7 +28,7 @@ class DoctrineInitializer implements ObjectInitializerInterface
$this->registry = $registry;
}
public function initialize($object)
public function initialize(object $object)
{
$manager = $this->registry->getManagerForClass(\get_class($object));
if (null !== $manager) {

View File

@ -72,7 +72,7 @@ class ConstraintViolationList implements \IteratorAggregate, ConstraintViolation
/**
* {@inheritdoc}
*/
public function get($offset)
public function get(int $offset)
{
if (!isset($this->violations[$offset])) {
throw new \OutOfBoundsException(sprintf('The offset "%s" does not exist.', $offset));
@ -84,7 +84,7 @@ class ConstraintViolationList implements \IteratorAggregate, ConstraintViolation
/**
* {@inheritdoc}
*/
public function has($offset)
public function has(int $offset)
{
return isset($this->violations[$offset]);
}
@ -92,7 +92,7 @@ class ConstraintViolationList implements \IteratorAggregate, ConstraintViolation
/**
* {@inheritdoc}
*/
public function set($offset, ConstraintViolationInterface $violation)
public function set(int $offset, ConstraintViolationInterface $violation)
{
$this->violations[$offset] = $violation;
}
@ -100,7 +100,7 @@ class ConstraintViolationList implements \IteratorAggregate, ConstraintViolation
/**
* {@inheritdoc}
*/
public function remove($offset)
public function remove(int $offset)
{
unset($this->violations[$offset]);
}

View File

@ -37,7 +37,7 @@ interface ConstraintViolationListInterface extends \Traversable, \Countable, \Ar
*
* @throws \OutOfBoundsException if the offset does not exist
*/
public function get($offset);
public function get(int $offset);
/**
* Returns whether the given offset exists.
@ -46,7 +46,7 @@ interface ConstraintViolationListInterface extends \Traversable, \Countable, \Ar
*
* @return bool Whether the offset exists
*/
public function has($offset);
public function has(int $offset);
/**
* Sets a violation at a given offset.
@ -54,12 +54,12 @@ interface ConstraintViolationListInterface extends \Traversable, \Countable, \Ar
* @param int $offset The violation offset
* @param ConstraintViolationInterface $violation The violation
*/
public function set($offset, ConstraintViolationInterface $violation);
public function set(int $offset, ConstraintViolationInterface $violation);
/**
* Removes a violation at a given offset.
*
* @param int $offset The offset to remove
*/
public function remove($offset);
public function remove(int $offset);
}

View File

@ -143,7 +143,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath)
public function setNode($value, ?object $object, MetadataInterface $metadata = null, string $propertyPath)
{
$this->value = $value;
$this->object = $object;
@ -154,7 +154,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function setGroup($group)
public function setGroup(?string $group)
{
$this->group = $group;
}
@ -170,7 +170,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function addViolation($message, array $parameters = [])
public function addViolation(string $message, array $parameters = [])
{
$this->violations->add(new ConstraintViolation(
$this->translator->trans($message, $parameters, $this->translationDomain),
@ -188,7 +188,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function buildViolation($message, array $parameters = [])
public function buildViolation(string $message, array $parameters = [])
{
return new ConstraintViolationBuilder(
$this->violations,
@ -283,7 +283,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function getPropertyPath($subPath = '')
public function getPropertyPath(string $subPath = '')
{
return PropertyPath::append($this->propertyPath, $subPath);
}
@ -291,7 +291,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function markGroupAsValidated($cacheKey, $groupHash)
public function markGroupAsValidated(string $cacheKey, string $groupHash)
{
if (!isset($this->validatedObjects[$cacheKey])) {
$this->validatedObjects[$cacheKey] = [];
@ -303,7 +303,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function isGroupValidated($cacheKey, $groupHash)
public function isGroupValidated(string $cacheKey, string $groupHash)
{
return isset($this->validatedObjects[$cacheKey][$groupHash]);
}
@ -311,7 +311,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function markConstraintAsValidated($cacheKey, $constraintHash)
public function markConstraintAsValidated(string $cacheKey, string $constraintHash)
{
$this->validatedConstraints[$cacheKey.':'.$constraintHash] = true;
}
@ -319,7 +319,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function isConstraintValidated($cacheKey, $constraintHash)
public function isConstraintValidated(string $cacheKey, string $constraintHash)
{
return isset($this->validatedConstraints[$cacheKey.':'.$constraintHash]);
}
@ -327,7 +327,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function markObjectAsInitialized($cacheKey)
public function markObjectAsInitialized(string $cacheKey)
{
$this->initializedObjects[$cacheKey] = true;
}
@ -335,7 +335,7 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function isObjectInitialized($cacheKey)
public function isObjectInitialized(string $cacheKey)
{
return isset($this->initializedObjects[$cacheKey]);
}

View File

@ -67,7 +67,7 @@ interface ExecutionContextInterface
* @param string $message The error message
* @param array $params The parameters substituted in the error message
*/
public function addViolation($message, array $params = []);
public function addViolation(string $message, array $params = []);
/**
* Returns a builder for adding a violation with extended information.
@ -86,7 +86,7 @@ interface ExecutionContextInterface
*
* @return ConstraintViolationBuilderInterface The violation builder
*/
public function buildViolation($message, array $parameters = []);
public function buildViolation(string $message, array $parameters = []);
/**
* Returns the validator.
@ -133,7 +133,7 @@ interface ExecutionContextInterface
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath);
public function setNode($value, ?object $object, MetadataInterface $metadata = null, string $propertyPath);
/**
* Sets the currently validated group.
@ -143,7 +143,7 @@ interface ExecutionContextInterface
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function setGroup($group);
public function setGroup(?string $group);
/**
* Sets the currently validated constraint.
@ -165,7 +165,7 @@ interface ExecutionContextInterface
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function markGroupAsValidated($cacheKey, $groupHash);
public function markGroupAsValidated(string $cacheKey, string $groupHash);
/**
* Returns whether an object was validated in a specific validation group.
@ -180,7 +180,7 @@ interface ExecutionContextInterface
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function isGroupValidated($cacheKey, $groupHash);
public function isGroupValidated(string $cacheKey, string $groupHash);
/**
* Marks a constraint as validated for an object.
@ -191,7 +191,7 @@ interface ExecutionContextInterface
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function markConstraintAsValidated($cacheKey, $constraintHash);
public function markConstraintAsValidated(string $cacheKey, string $constraintHash);
/**
* Returns whether a constraint was validated for an object.
@ -204,7 +204,7 @@ interface ExecutionContextInterface
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function isConstraintValidated($cacheKey, $constraintHash);
public function isConstraintValidated(string $cacheKey, string $constraintHash);
/**
* Marks that an object was initialized.
@ -216,7 +216,7 @@ interface ExecutionContextInterface
*
* @see ObjectInitializerInterface
*/
public function markObjectAsInitialized($cacheKey);
public function markObjectAsInitialized(string $cacheKey);
/**
* Returns whether an object was initialized.
@ -230,7 +230,7 @@ interface ExecutionContextInterface
*
* @see ObjectInitializerInterface
*/
public function isObjectInitialized($cacheKey);
public function isObjectInitialized(string $cacheKey);
/**
* Returns the violations generated by the validator so far.
@ -340,5 +340,5 @@ interface ExecutionContextInterface
* string if the validator is currently validating the
* root value of the validation graph.
*/
public function getPropertyPath($subPath = '');
public function getPropertyPath(string $subPath = '');
}

View File

@ -25,7 +25,7 @@ interface CacheInterface
*
* @param string $class
*/
public function has($class);
public function has(string $class);
/**
* Returns the metadata for the given class from the cache.
@ -34,7 +34,7 @@ interface CacheInterface
*
* @return ClassMetadata|false A ClassMetadata instance or false on miss
*/
public function read($class);
public function read(string $class);
/**
* Stores a class metadata in the cache.

View File

@ -36,7 +36,7 @@ final class DoctrineCache implements CacheInterface
/**
* {@inheritdoc}
*/
public function has($class): bool
public function has(string $class): bool
{
return $this->cache->contains($class);
}
@ -44,7 +44,7 @@ final class DoctrineCache implements CacheInterface
/**
* {@inheritdoc}
*/
public function read($class)
public function read(string $class)
{
return $this->cache->fetch($class);
}

View File

@ -31,7 +31,7 @@ class Psr6Cache implements CacheInterface
/**
* {@inheritdoc}
*/
public function has($class)
public function has(string $class)
{
return $this->cacheItemPool->hasItem($this->escapeClassName($class));
}
@ -39,7 +39,7 @@ class Psr6Cache implements CacheInterface
/**
* {@inheritdoc}
*/
public function read($class)
public function read(string $class)
{
$item = $this->cacheItemPool->getItem($this->escapeClassName($class));

View File

@ -364,7 +364,7 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
/**
* {@inheritdoc}
*/
public function hasPropertyMetadata($property)
public function hasPropertyMetadata(string $property)
{
return \array_key_exists($property, $this->members);
}
@ -372,7 +372,7 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
/**
* {@inheritdoc}
*/
public function getPropertyMetadata($property)
public function getPropertyMetadata(string $property)
{
if (!isset($this->members[$property])) {
return [];

View File

@ -81,7 +81,7 @@ interface ClassMetadataInterface extends MetadataInterface
*
* @return bool
*/
public function hasPropertyMetadata($property);
public function hasPropertyMetadata(string $property);
/**
* Returns all metadata instances for the given named property.
@ -94,7 +94,7 @@ interface ClassMetadataInterface extends MetadataInterface
* @return PropertyMetadataInterface[] A list of metadata instances. Empty if
* no metadata exists for the property.
*/
public function getPropertyMetadata($property);
public function getPropertyMetadata(string $property);
/**
* Returns the name of the backing PHP class.

View File

@ -185,7 +185,7 @@ class GenericMetadata implements MetadataInterface
*
* Aware of the global group (* group).
*/
public function findConstraints($group)
public function findConstraints(string $group)
{
return isset($this->constraintsByGroup[$group])
? $this->constraintsByGroup[$group]

View File

@ -62,5 +62,5 @@ interface MetadataInterface
*
* @return Constraint[] A list of constraint instances
*/
public function findConstraints($group);
public function findConstraints(string $group);
}

View File

@ -27,5 +27,5 @@ interface ObjectInitializerInterface
*
* @param object $object The object to validate
*/
public function initialize($object);
public function initialize(object $object);
}

View File

@ -15,7 +15,7 @@ use Symfony\Component\Validator\Mapping\ClassMetadata;
class FakeClassMetadata extends ClassMetadata
{
public function addCustomPropertyMetadata($propertyName, $metadata)
public function addCustomPropertyMetadata(string $propertyName, $metadata)
{
if (!isset($this->members[$propertyName])) {
$this->members[$propertyName] = [];

View File

@ -32,7 +32,7 @@ interface ContextualValidatorInterface
*
* @return $this
*/
public function atPath($path);
public function atPath(string $path);
/**
* Validates a value against a constraint or a list of constraints.
@ -58,7 +58,7 @@ interface ContextualValidatorInterface
*
* @return $this
*/
public function validateProperty($object, $propertyName, $groups = null);
public function validateProperty($object, string $propertyName, $groups = null);
/**
* Validates a value against the constraints specified for an object's
@ -71,7 +71,7 @@ interface ContextualValidatorInterface
*
* @return $this
*/
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null);
public function validatePropertyValue($objectOrClass, string $propertyName, $value, $groups = null);
/**
* Returns the violations that have been generated so far in the context

View File

@ -71,7 +71,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
/**
* {@inheritdoc}
*/
public function atPath($path)
public function atPath(string $path)
{
$this->defaultPropertyPath = $this->context->getPropertyPath($path);
@ -169,7 +169,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
/**
* {@inheritdoc}
*/
public function validateProperty($object, $propertyName, $groups = null)
public function validateProperty($object, string $propertyName, $groups = null)
{
$classMetadata = $this->metadataFactory->getMetadataFor($object);
@ -213,7 +213,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
/**
* {@inheritdoc}
*/
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
public function validatePropertyValue($objectOrClass, string $propertyName, $value, $groups = null)
{
$classMetadata = $this->metadataFactory->getMetadataFor($objectOrClass);
@ -295,7 +295,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
* traversal, the object will be iterated and each nested object will be
* validated instead.
*
* @param object $object The object to cascade
* @param object|int|float|string $object The object to cascade
* @param string $propertyPath The current property path
* @param (string|GroupSequence)[] $groups The validated groups
* @param int $traversalStrategy The strategy for traversing the
@ -437,7 +437,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
*
* @see TraversalStrategy
*/
private function validateClassNode($object, $cacheKey, ClassMetadataInterface $metadata = null, $propertyPath, array $groups, $cascadedGroups, $traversalStrategy, ExecutionContextInterface $context)
private function validateClassNode(object $object, $cacheKey, ClassMetadataInterface $metadata = null, $propertyPath, array $groups, $cascadedGroups, $traversalStrategy, ExecutionContextInterface $context)
{
$context->setNode($object, $object, $metadata, $propertyPath);
@ -616,7 +616,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
*
* @see TraversalStrategy
*/
private function validateGenericNode($value, $object, $cacheKey, MetadataInterface $metadata = null, $propertyPath, array $groups, $cascadedGroups, $traversalStrategy, ExecutionContextInterface $context)
private function validateGenericNode($value, ?object $object, $cacheKey, MetadataInterface $metadata = null, $propertyPath, array $groups, $cascadedGroups, $traversalStrategy, ExecutionContextInterface $context)
{
$context->setNode($value, $object, $metadata, $propertyPath);
@ -724,7 +724,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
* the group sequence
* @param ExecutionContextInterface $context The execution context
*/
private function stepThroughGroupSequence($value, $object, $cacheKey, MetadataInterface $metadata = null, $propertyPath, $traversalStrategy, GroupSequence $groupSequence, $cascadedGroup, ExecutionContextInterface $context)
private function stepThroughGroupSequence($value, ?object $object, $cacheKey, MetadataInterface $metadata = null, $propertyPath, $traversalStrategy, GroupSequence $groupSequence, $cascadedGroup, ExecutionContextInterface $context)
{
$violationCount = \count($context->getViolations());
$cascadedGroups = $cascadedGroup ? [$cascadedGroup] : null;

View File

@ -104,7 +104,7 @@ class RecursiveValidator implements ValidatorInterface
/**
* {@inheritdoc}
*/
public function validateProperty($object, $propertyName, $groups = null)
public function validateProperty($object, string $propertyName, $groups = null)
{
return $this->startContext($object)
->validateProperty($object, $propertyName, $groups)
@ -114,7 +114,7 @@ class RecursiveValidator implements ValidatorInterface
/**
* {@inheritdoc}
*/
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
public function validatePropertyValue($objectOrClass, string $propertyName, $value, $groups = null)
{
// If a class name is passed, take $value as root
return $this->startContext(\is_object($objectOrClass) ? $objectOrClass : $value)

View File

@ -105,7 +105,7 @@ class TraceableValidator implements ValidatorInterface, ResetInterface
/**
* {@inheritdoc}
*/
public function validateProperty($object, $propertyName, $groups = null)
public function validateProperty($object, string $propertyName, $groups = null)
{
return $this->validator->validateProperty($object, $propertyName, $groups);
}
@ -113,7 +113,7 @@ class TraceableValidator implements ValidatorInterface, ResetInterface
/**
* {@inheritdoc}
*/
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
public function validatePropertyValue($objectOrClass, string $propertyName, $value, $groups = null)
{
return $this->validator->validatePropertyValue($objectOrClass, $propertyName, $value, $groups);
}

View File

@ -52,7 +52,7 @@ interface ValidatorInterface extends MetadataFactoryInterface
* If the list is empty, validation
* succeeded
*/
public function validateProperty($object, $propertyName, $groups = null);
public function validateProperty($object, string $propertyName, $groups = null);
/**
* Validates a value against the constraints specified for an object's
@ -67,7 +67,7 @@ interface ValidatorInterface extends MetadataFactoryInterface
* If the list is empty, validation
* succeeded
*/
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null);
public function validatePropertyValue($objectOrClass, string $propertyName, $value, $groups = null);
/**
* Starts a new validation context and returns a validator for that context.

View File

@ -59,7 +59,7 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
/**
* {@inheritdoc}
*/
public function atPath($path)
public function atPath(string $path)
{
$this->propertyPath = PropertyPath::append($this->propertyPath, $path);
@ -69,7 +69,7 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
/**
* {@inheritdoc}
*/
public function setParameter($key, $value)
public function setParameter(string $key, string $value)
{
$this->parameters[$key] = $value;
@ -89,7 +89,7 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
/**
* {@inheritdoc}
*/
public function setTranslationDomain($translationDomain)
public function setTranslationDomain(string $translationDomain)
{
$this->translationDomain = $translationDomain;
@ -109,7 +109,7 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
/**
* {@inheritdoc}
*/
public function setPlural($number)
public function setPlural(int $number)
{
$this->plural = $number;
@ -119,7 +119,7 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
/**
* {@inheritdoc}
*/
public function setCode($code)
public function setCode(?string $code)
{
$this->code = $code;

View File

@ -33,7 +33,7 @@ interface ConstraintViolationBuilderInterface
*
* @return $this
*/
public function atPath($path);
public function atPath(string $path);
/**
* Sets a parameter to be inserted into the violation message.
@ -43,7 +43,7 @@ interface ConstraintViolationBuilderInterface
*
* @return $this
*/
public function setParameter($key, $value);
public function setParameter(string $key, string $value);
/**
* Sets all parameters to be inserted into the violation message.
@ -66,7 +66,7 @@ interface ConstraintViolationBuilderInterface
*
* @see \Symfony\Contracts\Translation\TranslatorInterface
*/
public function setTranslationDomain($translationDomain);
public function setTranslationDomain(string $translationDomain);
/**
* Sets the invalid value that caused this violation.
@ -87,7 +87,7 @@ interface ConstraintViolationBuilderInterface
*
* @see \Symfony\Contracts\Translation\TranslatorInterface::trans()
*/
public function setPlural($number);
public function setPlural(int $number);
/**
* Sets the violation code.
@ -96,7 +96,7 @@ interface ConstraintViolationBuilderInterface
*
* @return $this
*/
public function setCode($code);
public function setCode(?string $code);
/**
* Sets the cause of the violation.