Skip to content
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

Add parameter and return types to ValidatorInterface::isValid() #297

Merged
merged 1 commit into from
Jul 2, 2024
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
10 changes: 5 additions & 5 deletions docs/book/v3/writing-validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class Float extends AbstractValidator
{
const FLOAT = 'float';

protected $messageTemplates = [
protected array $messageTemplates = [
self::FLOAT => "'%value%' is not a floating point value",
];

public function isValid($value)
public function isValid(mixed $value): bool
{
$this->setValue($value);

Expand Down Expand Up @@ -101,7 +101,7 @@ class NumericBetween extends AbstractValidator
public $minimum = 0;
public $maximum = 100;

protected $messageVariables = [
protected array $messageVariables = [
'min' => 'minimum',
'max' => 'maximum',
];
Expand All @@ -112,7 +112,7 @@ class NumericBetween extends AbstractValidator
self::MSG_MAXIMUM => "'%value%' must be no more than '%max%'",
];

public function isValid($value)
public function isValid(mixed $value): bool
{
$this->setValue($value);

Expand Down Expand Up @@ -183,7 +183,7 @@ class PasswordStrength extends AbstractValidator
self::DIGIT => "'%value%' must contain at least one digit character",
];

public function isValid($value)
public function isValid(mixed $value): bool
{
$this->setValue($value);

Expand Down
13 changes: 0 additions & 13 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
<code><![CDATA[$this->cardLength[$type]]]></code>
<code><![CDATA[$this->options['type']]]></code>
<code><![CDATA[$this->options['type']]]></code>
<code><![CDATA[$value]]></code>
</MixedArgument>
<MixedArrayAssignment>
<code><![CDATA[$this->options['type'][]]]></code>
Expand Down Expand Up @@ -375,9 +374,6 @@
<MoreSpecificImplementedParamType>
<code><![CDATA[$value]]></code>
</MoreSpecificImplementedParamType>
<NonInvariantDocblockPropertyType>
<code><![CDATA[$messageTemplates]]></code>
</NonInvariantDocblockPropertyType>
<PossiblyFalseOperand>
<code><![CDATA[strrpos($fileInfo['filename'], '.')]]></code>
</PossiblyFalseOperand>
Expand Down Expand Up @@ -1063,19 +1059,10 @@
</UnresolvableInclude>
</file>
<file src="src/Iban.php">
<DocblockTypeContradiction>
<code><![CDATA[is_string($value)]]></code>
</DocblockTypeContradiction>
<MixedArgument>
<code><![CDATA[$options['allow_non_sepa']]]></code>
<code><![CDATA[$options['country_code']]]></code>
</MixedArgument>
<MixedOperand>
<code><![CDATA[static::$ibanRegex[$countryCode]]]></code>
</MixedOperand>
<MoreSpecificImplementedParamType>
<code><![CDATA[$value]]></code>
</MoreSpecificImplementedParamType>
<PossiblyInvalidArgument>
<code><![CDATA[$options]]></code>
</PossiblyInvalidArgument>
Expand Down
10 changes: 3 additions & 7 deletions src/BusinessIdentifierCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ final class BusinessIdentifierCode extends AbstractValidator
public const NOT_STRING = 'valueNotString';
public const NOT_VALID_COUNTRY = 'valueNotCountry';

/** @var string[] */
protected $messageTemplates = [
/** @var array<string, string> */
protected array $messageTemplates = [
self::NOT_STRING => 'Invalid type given; string expected',
self::INVALID => 'Invalid BIC format',
self::NOT_VALID_COUNTRY => 'Invalid country code',
Expand All @@ -31,8 +31,6 @@ final class BusinessIdentifierCode extends AbstractValidator
* List of all country codes defined by ISO 3166-1 alpha-2
*
* @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Current_codes
*
* @var string[]
*/
private const ISO_COUNTRIES = [
'AD',
Expand Down Expand Up @@ -291,13 +289,11 @@ final class BusinessIdentifierCode extends AbstractValidator
* This code is the only one used by SWIFT that is not defined by ISO 3166-1 alpha-2
*
* @see https://en.wikipedia.org/wiki/ISO_9362
*
* @var string
*/
private const KOSOVO_EXCEPTION = 'XK';

/** {@inheritDoc} */
public function isValid($value): bool
public function isValid(mixed $value): bool
{
if (! is_string($value)) {
$this->error(self::NOT_STRING);
Expand Down
28 changes: 10 additions & 18 deletions src/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class CreditCard extends AbstractValidator
/**
* Validation failure message template definitions
*
* @var array
* @var array<string, string>
*/
protected $messageTemplates = [
protected array $messageTemplates = [
self::CHECKSUM => 'The input seems to contain an invalid checksum',
self::CONTENT => 'The input must contain only digits',
self::INVALID => 'Invalid type given. String expected',
Expand Down Expand Up @@ -361,28 +361,22 @@ public function setService($service)
return $this;
}

// The following rule is buggy for parameters attributes
// phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing.NoSpaceBetweenTypeHintAndParameter

/**
* Returns true if and only if $value follows the Luhn algorithm (mod-10 checksum)
*
* @param mixed $value
* @return bool
*/
public function isValid(
#[SensitiveParameter]
$value
) {
mixed $value
): bool {
$this->setValue($value);

if (! is_string($value)) {
$this->error(self::INVALID, $value);
$this->error(self::INVALID);
return false;
}

if (! ctype_digit($value)) {
$this->error(self::CONTENT, $value);
$this->error(self::CONTENT);
return false;
}

Expand All @@ -403,12 +397,12 @@ public function isValid(
}

if ($foundp === false) {
$this->error(self::PREFIX, $value);
$this->error(self::PREFIX);
return false;
}

if ($foundl === false) {
$this->error(self::LENGTH, $value);
$this->error(self::LENGTH);
return false;
}

Expand Down Expand Up @@ -436,17 +430,15 @@ public function isValid(
'throwExceptions' => true,
]);
if (! $callback->isValid($value)) {
$this->error(self::SERVICE, $value);
$this->error(self::SERVICE);
return false;
}
} catch (Exception) {
$this->error(self::SERVICEFAILURE, $value);
$this->error(self::SERVICEFAILURE);
return false;
}
}

return true;
}

// phpcs:enable SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing.NoSpaceBetweenTypeHintAndParameter
}
11 changes: 5 additions & 6 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ class Date extends AbstractValidator
/**
* Validation failure message template definitions
*
* @var string[]
* @var array<string, string>
*/
protected $messageTemplates = [
protected array $messageTemplates = [
self::INVALID => 'Invalid type given. String, integer, array or DateTime expected',
self::INVALID_DATE => 'The input does not appear to be a valid date',
self::FALSEFORMAT => "The input does not fit the date format '%format%'",
];

/** @var string[] */
protected $messageVariables = [
/** @var array<string, string> */
protected array $messageVariables = [
'format' => 'format',
];

Expand Down Expand Up @@ -115,9 +115,8 @@ public function isStrict(): bool
* Returns true if $value is a DateTimeInterface instance or can be converted into one.
*
* @param string|numeric|array|DateTimeInterface $value
* @return bool
*/
public function isValid($value)
public function isValid(mixed $value): bool
{
$this->setValue($value);

Expand Down
7 changes: 3 additions & 4 deletions src/DateStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ final class DateStep extends Date
/**
* Validation failure message template definitions
*
* @var string[]
* @var array<string, string>
*/
protected $messageTemplates = [
protected array $messageTemplates = [
self::INVALID => 'Invalid type given. String, integer, array or DateTime expected',
self::INVALID_DATE => 'The input does not appear to be a valid date',
self::FALSEFORMAT => "The input does not fit the date format '%format%'",
Expand Down Expand Up @@ -217,10 +217,9 @@ protected function convertString($value, $addErrors = true)
* Returns true if a date is within a valid step
*
* @param string|int|DateTimeInterface $value
* @return bool
* @throws Exception\InvalidArgumentException
*/
public function isValid($value)
public function isValid(mixed $value): bool
{
if (! parent::isValid($value)) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Digits.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class Digits extends AbstractValidator
*
* @var array<string, string>
*/
protected $messageTemplates = [
protected array $messageTemplates = [
self::NOT_DIGITS => 'The input must contain only digits',
self::STRING_EMPTY => 'The input is an empty string',
self::INVALID => 'Invalid type given. String, integer or float expected',
Expand Down
9 changes: 4 additions & 5 deletions src/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class EmailAddress extends AbstractValidator
// phpcs:disable Generic.Files.LineLength.TooLong

/** @var array<string, string> */
protected $messageTemplates = [
protected array $messageTemplates = [
self::INVALID => "Invalid type given. String expected",
self::INVALID_FORMAT => "The input is not a valid email address. Use the basic format local-part@hostname",
self::INVALID_HOSTNAME => "'%hostname%' is not a valid hostname for the email address",
Expand All @@ -59,8 +59,8 @@ final class EmailAddress extends AbstractValidator

// phpcs:enable

/** @var array */
protected $messageVariables = [
/** @var array<string, string> */
protected array $messageVariables = [
'hostname' => 'hostname',
'localPart' => 'localPart',
];
Expand Down Expand Up @@ -486,9 +486,8 @@ protected function splitEmailParts($value)
* @link http://www.columbia.edu/kermit/ascii.html US-ASCII characters
*
* @param string $value
* @return bool
*/
public function isValid($value)
public function isValid(mixed $value): bool
{
if (! is_string($value)) {
$this->error(self::INVALID);
Expand Down
11 changes: 5 additions & 6 deletions src/File/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ final class Count extends AbstractValidator
public const TOO_FEW = 'fileCountTooFew';
/**#@-*/

/** @var array Error message templates */
protected $messageTemplates = [
/** @var array<string, string> */
protected array $messageTemplates = [
self::TOO_MANY => "Too many files, maximum '%max%' are allowed but '%count%' are given",
self::TOO_FEW => "Too few files, minimum '%min%' are expected but '%count%' are given",
];

/** @var array Error message template variables */
protected $messageVariables = [
/** @var array<string, string|array> */
protected array $messageVariables = [
'min' => ['options' => 'min'],
'max' => ['options' => 'max'],
'count' => 'count',
Expand Down Expand Up @@ -211,9 +211,8 @@ public function addFile($file)
*
* @param string|array|UploadedFileInterface $value Filenames to check for count
* @param array $file File data from \Laminas\File\Transfer\Transfer
* @return bool
*/
public function isValid($value, $file = null)
public function isValid(mixed $value, $file = null): bool
{
if ($this->isUploadedFilterInterface($value)) {
$this->addFile($value);
Expand Down
7 changes: 3 additions & 4 deletions src/File/Crc32.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ final class Crc32 extends Hash
public const NOT_DETECTED = 'fileCrc32NotDetected';
public const NOT_FOUND = 'fileCrc32NotFound';

/** @var array Error message templates */
protected $messageTemplates = [
/** @var array<string, string> */
protected array $messageTemplates = [
self::DOES_NOT_MATCH => 'File does not match the given crc32 hashes',
self::NOT_DETECTED => 'A crc32 hash could not be evaluated for the given file',
self::NOT_FOUND => 'File is not readable or does not exist',
Expand Down Expand Up @@ -79,9 +79,8 @@ public function addCrc32($options)
*
* @param string|array $value Filename to check for hash
* @param array $file File data from \Laminas\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value, $file = null)
public function isValid(mixed $value, $file = null): bool
{
$fileInfo = $this->getFileInfo($value, $file);

Expand Down
7 changes: 3 additions & 4 deletions src/File/ExcludeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ final class ExcludeExtension extends Extension
public const FALSE_EXTENSION = 'fileExcludeExtensionFalse';
public const NOT_FOUND = 'fileExcludeExtensionNotFound';

/** @var array Error message templates */
protected $messageTemplates = [
/** @var array<string, string> */
protected array $messageTemplates = [
self::FALSE_EXTENSION => 'File has an incorrect extension',
self::NOT_FOUND => 'File is not readable or does not exist',
];
Expand All @@ -35,9 +35,8 @@ final class ExcludeExtension extends Extension
*
* @param string|array $value Real file to check for extension
* @param array $file File data from \Laminas\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value, $file = null)
public function isValid(mixed $value, $file = null): bool
{
$fileInfo = $this->getFileInfo($value, $file);

Expand Down
7 changes: 3 additions & 4 deletions src/File/ExcludeMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ final class ExcludeMimeType extends MimeType
public const NOT_DETECTED = 'fileExcludeMimeTypeNotDetected';
public const NOT_READABLE = 'fileExcludeMimeTypeNotReadable';

/** @inheritDoc */
protected $messageTemplates = [
/** @var array<string, string> */
protected array $messageTemplates = [
self::FALSE_TYPE => "File has an incorrect mimetype of '%type%'",
self::NOT_DETECTED => 'The mimetype could not be detected from the file',
self::NOT_READABLE => 'File is not readable or does not exist',
Expand All @@ -42,9 +42,8 @@ final class ExcludeMimeType extends MimeType
*
* @param string|array|UploadedFileInterface $value Real file to check for mimetype
* @param array $file File data from \Laminas\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value, $file = null)
public function isValid(mixed $value, $file = null): bool
{
$fileInfo = $this->getFileInfo($value, $file, true);

Expand Down
Loading