[Validator] Add more parameter types.

This commit is contained in:
Alexander M. Turek 2019-08-16 01:09:31 +02:00
parent 90e3da4edd
commit a7457099f8
10 changed files with 22 additions and 47 deletions

View File

@ -83,7 +83,7 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
*
* @return string The string representation of the passed value
*/
protected function formatValue($value, $format = 0)
protected function formatValue($value, int $format = 0)
{
$isDateTime = $value instanceof \DateTimeInterface;
@ -156,7 +156,7 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
*
* @see formatValue()
*/
protected function formatValues(array $values, $format = 0)
protected function formatValues(array $values, int $format = 0)
{
foreach ($values as $key => $value) {
$values[$key] = $this->formatValue($value, $format);

View File

@ -199,9 +199,9 @@ class FileValidator extends ConstraintValidator
}
}
private static function moreDecimalsThan($double, $numberOfDecimals)
private static function moreDecimalsThan(string $double, int $numberOfDecimals): bool
{
return \strlen((string) $double) > \strlen(round($double, $numberOfDecimals));
return \strlen($double) > \strlen(round($double, $numberOfDecimals));
}
/**

View File

@ -205,11 +205,9 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
/**
* Adds a constraint to the given property.
*
* @param string $property The name of the property
*
* @return $this
*/
public function addPropertyConstraint($property, Constraint $constraint)
public function addPropertyConstraint(string $property, Constraint $constraint)
{
if (!isset($this->properties[$property])) {
$this->properties[$property] = new PropertyMetadata($this->getClassName(), $property);
@ -225,12 +223,11 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
}
/**
* @param string $property
* @param Constraint[] $constraints
*
* @return $this
*/
public function addPropertyConstraints($property, array $constraints)
public function addPropertyConstraints(string $property, array $constraints)
{
foreach ($constraints as $constraint) {
$this->addPropertyConstraint($property, $constraint);
@ -245,11 +242,9 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
* The name of the getter is assumed to be the name of the property with an
* uppercased first letter and either the prefix "get" or "is".
*
* @param string $property The name of the property
*
* @return $this
*/
public function addGetterConstraint($property, Constraint $constraint)
public function addGetterConstraint(string $property, Constraint $constraint)
{
if (!isset($this->getters[$property])) {
$this->getters[$property] = new GetterMetadata($this->getClassName(), $property);
@ -267,12 +262,9 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
/**
* Adds a constraint to the getter of the given property.
*
* @param string $property The name of the property
* @param string $method The name of the getter method
*
* @return $this
*/
public function addGetterMethodConstraint($property, $method, Constraint $constraint)
public function addGetterMethodConstraint(string $property, string $method, Constraint $constraint)
{
if (!isset($this->getters[$property])) {
$this->getters[$property] = new GetterMetadata($this->getClassName(), $property, $method);
@ -288,12 +280,11 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
}
/**
* @param string $property
* @param Constraint[] $constraints
*
* @return $this
*/
public function addGetterConstraints($property, array $constraints)
public function addGetterConstraints(string $property, array $constraints)
{
foreach ($constraints as $constraint) {
$this->addGetterConstraint($property, $constraint);
@ -303,13 +294,11 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
}
/**
* @param string $property
* @param string $method
* @param Constraint[] $constraints
*
* @return $this
*/
public function addGetterMethodConstraints($property, $method, array $constraints)
public function addGetterMethodConstraints(string $property, string $method, array $constraints)
{
foreach ($constraints as $constraint) {
$this->addGetterMethodConstraint($property, $method, $constraint);
@ -451,11 +440,9 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
/**
* Sets whether a group sequence provider should be used.
*
* @param bool $active
*
* @throws GroupDefinitionException
*/
public function setGroupSequenceProvider($active)
public function setGroupSequenceProvider(bool $active)
{
if ($this->hasGroupSequence()) {
throw new GroupDefinitionException('Defining a group sequence provider is not allowed with a static group sequence');

View File

@ -43,11 +43,8 @@ abstract class AbstractLoader implements LoaderInterface
* $this->addNamespaceAlias('mynamespace', '\\Acme\\Package\\Constraints\\');
*
* $constraint = $this->newConstraint('mynamespace:NotNull');
*
* @param string $alias The alias
* @param string $namespace The PHP namespace
*/
protected function addNamespaceAlias($alias, $namespace)
protected function addNamespaceAlias(string $alias, string $namespace)
{
$this->namespaces[$alias] = $namespace;
}
@ -67,7 +64,7 @@ abstract class AbstractLoader implements LoaderInterface
*
* @throws MappingException If the namespace prefix is undefined
*/
protected function newConstraint($name, $options = null)
protected function newConstraint(string $name, $options = null)
{
if (false !== strpos($name, '\\') && class_exists($name)) {
$className = (string) $name;

View File

@ -35,11 +35,9 @@ abstract class FilesLoader extends LoaderChain
/**
* Returns an array of file loaders for the given file paths.
*
* @param array $paths An array of file paths
*
* @return LoaderInterface[] The metadata loaders
*/
protected function getFileLoaders($paths)
protected function getFileLoaders(array $paths)
{
$loaders = [];
@ -53,9 +51,7 @@ abstract class FilesLoader extends LoaderChain
/**
* Creates a loader for the given file path.
*
* @param string $path The file path
*
* @return LoaderInterface The created loader
*/
abstract protected function getFileLoaderInstance($path);
abstract protected function getFileLoaderInstance(string $path);
}

View File

@ -167,13 +167,11 @@ class XmlFileLoader extends FileLoader
/**
* Loads the XML class descriptions from the given file.
*
* @param string $path The path of the XML file
*
* @return \SimpleXMLElement The class descriptions
*
* @throws MappingException If the file could not be loaded
*/
protected function parseFile($path)
protected function parseFile(string $path)
{
try {
$dom = XmlUtils::loadFile($path, __DIR__.'/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd');

View File

@ -24,7 +24,7 @@ class XmlFilesLoader extends FilesLoader
/**
* {@inheritdoc}
*/
public function getFileLoaderInstance($file)
public function getFileLoaderInstance(string $file)
{
return new XmlFileLoader($file);
}

View File

@ -24,7 +24,7 @@ class YamlFilesLoader extends FilesLoader
/**
* {@inheritdoc}
*/
public function getFileLoaderInstance($file)
public function getFileLoaderInstance(string $file)
{
return new YamlFileLoader($file);
}

View File

@ -25,7 +25,7 @@ abstract class FilesLoader extends BaseFilesLoader
parent::__construct($paths);
}
protected function getFileLoaderInstance($file)
protected function getFileLoaderInstance(string $file)
{
++$this->timesCalled;

View File

@ -29,19 +29,16 @@ class PropertyPath
* returned. Otherwise, the concatenation of the two paths is returned,
* separated by a dot (".").
*
* @param string $basePath The base path
* @param string $subPath The path to append
*
* @return string The concatenation of the two property paths
*/
public static function append($basePath, $subPath)
public static function append(string $basePath, string $subPath)
{
if ('' !== (string) $subPath) {
if ('' !== $subPath) {
if ('[' === $subPath[0]) {
return $basePath.$subPath;
}
return '' !== (string) $basePath ? $basePath.'.'.$subPath : $subPath;
return '' !== $basePath ? $basePath.'.'.$subPath : $subPath;
}
return $basePath;