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

Github Actions: Add Phpstan #30

Merged
merged 21 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ef101e3
Github Actions: Add Phpstan
sukhwinder33445 Jun 19, 2023
5b16e2c
BetweenValidator (phpDoc): Add possible var/param hints instead of `m…
sukhwinder33445 Aug 18, 2023
e0ceec8
CidrValidator::isValid(): Add phpDoc
sukhwinder33445 Aug 18, 2023
a4561b3
InArrayValidator: property $haystack can be null
sukhwinder33445 Aug 18, 2023
3ddaff3
EmailAddressValidator: Don't call `getmxrr()` if `idn_to_ascii()` fails
sukhwinder33445 Aug 18, 2023
ebd888b
FileValidator: Handle if `$file->getClientMediaType()` returns null
sukhwinder33445 Aug 18, 2023
d1a56a8
FileValidator::isValid() Add phpDoc
sukhwinder33445 Aug 18, 2023
a7c42f6
GreaterThanValidator (phpDoc): Add possible var|param|return type hin…
sukhwinder33445 Aug 18, 2023
02ce144
HostnameValidator: Remove unnecessary emtpy check of $value
sukhwinder33445 Aug 18, 2023
00c00de
LessThanValidator (phpDoc): Add possible var|param|return type hints …
sukhwinder33445 Aug 18, 2023
7ecb0fb
PrivateKeyValidator: Add phpDoc and method return type
sukhwinder33445 Aug 18, 2023
d5e66c9
StringLengthValidator (phpDoc): Define possible var/param/return type…
sukhwinder33445 Aug 18, 2023
dfa0f57
ValidatorChain: Add variable type hints
sukhwinder33445 Aug 18, 2023
f443e8d
ValidatorChain::addValidatorLoader(): Set param `$postfix = ''` as de…
sukhwinder33445 Aug 18, 2023
0d09427
FileValidator: Don't pass null if string is expected
sukhwinder33445 Aug 18, 2023
1c8f239
BetweenValidator::setInclusive(): Set param type to bool
sukhwinder33445 Aug 18, 2023
1585f94
DateTimeValidator::__construct(): Set param type to bool
sukhwinder33445 Aug 18, 2023
4be9fd5
Validators::isValid(): Add return type where missing
sukhwinder33445 Aug 18, 2023
17086c3
FileValidator: Fix name case of method `getClientFilename()`
sukhwinder33445 Aug 22, 2023
74e32db
Add phpstan `baseline` config
yhabteab Aug 23, 2023
fa93b58
Add common ignore errors pattern to phpstan config
yhabteab Aug 23, 2023
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
4 changes: 4 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
if: success() || matrix.allow_failure
run: phpcs -wps --colors

- name: PHPStan
uses: php-actions/phpstan@v3
if: success() || matrix.allow_failure

test:
name: Unit tests with php ${{ matrix.php }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand Down
96 changes: 96 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
parameters:
ignoreErrors:
-
message: "#^Method ipl\\\\Validator\\\\BetweenValidator\\:\\:__construct\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: src/BetweenValidator.php

-
message: "#^Method ipl\\\\Validator\\\\DeferredInArrayValidator\\:\\:__construct\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: src/DeferredInArrayValidator.php

-
message: "#^Method ipl\\\\Validator\\\\DeferredInArrayValidator\\:\\:getHaystack\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/DeferredInArrayValidator.php

-
message: "#^Method ipl\\\\Validator\\\\EmailAddressValidator\\:\\:__construct\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: src/EmailAddressValidator.php

-
message: "#^Method ipl\\\\Validator\\\\FileValidator\\:\\:__construct\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: src/FileValidator.php

-
message: "#^Method ipl\\\\Validator\\\\GreaterThanValidator\\:\\:__construct\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: src/GreaterThanValidator.php

-
message: "#^Method ipl\\\\Validator\\\\InArrayValidator\\:\\:__construct\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: src/InArrayValidator.php

-
message: "#^Method ipl\\\\Validator\\\\InArrayValidator\\:\\:findInvalid\\(\\) has parameter \\$values with no value type specified in iterable type array\\.$#"
count: 1
path: src/InArrayValidator.php

-
message: "#^Method ipl\\\\Validator\\\\InArrayValidator\\:\\:findInvalid\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/InArrayValidator.php

-
message: "#^Method ipl\\\\Validator\\\\InArrayValidator\\:\\:getHaystack\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/InArrayValidator.php

-
message: "#^Method ipl\\\\Validator\\\\InArrayValidator\\:\\:setHaystack\\(\\) has parameter \\$haystack with no value type specified in iterable type array\\.$#"
count: 1
path: src/InArrayValidator.php

-
message: "#^Property ipl\\\\Validator\\\\InArrayValidator\\:\\:\\$haystack type has no value type specified in iterable type array\\.$#"
count: 1
path: src/InArrayValidator.php

-
message: "#^Method ipl\\\\Validator\\\\LessThanValidator\\:\\:__construct\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: src/LessThanValidator.php

-
message: "#^Method ipl\\\\Validator\\\\StringLengthValidator\\:\\:__construct\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: src/StringLengthValidator.php

-
message: "#^Class ipl\\\\Validator\\\\ValidatorChain implements generic interface IteratorAggregate but does not specify its types\\: TKey, TValue$#"
count: 1
path: src/ValidatorChain.php

-
message: "#^Method ipl\\\\Validator\\\\ValidatorChain\\:\\:addValidators\\(\\) has parameter \\$validators with no value type specified in iterable type iterable\\.$#"
count: 1
path: src/ValidatorChain.php

-
message: "#^Method ipl\\\\Validator\\\\ValidatorChain\\:\\:getValidatorsThatBreakTheChain\\(\\) return type with generic class SplObjectStorage does not specify its types\\: TObject, TData$#"
count: 1
path: src/ValidatorChain.php

-
message: "#^Method ipl\\\\Validator\\\\ValidatorChain\\:\\:toArray\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/ValidatorChain.php

-
message: "#^Property ipl\\\\Validator\\\\ValidatorChain\\:\\:\\$validatorsThatBreakTheChain with generic class SplObjectStorage does not specify its types\\: TObject, TData$#"
count: 1
path: src/ValidatorChain.php
22 changes: 22 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
includes:
- phpstan-baseline.neon

parameters:
level: max

checkFunctionNameCase: true
checkInternalClassCaseSensitivity: true
treatPhpDocTypesAsCertain: false

paths:
- src

scanDirectories:
- vendor

ignoreErrors:
-
messages:
- '#Unsafe usage of new static\(\)#'
- '#. but return statement is missing#'
reportUnmatched: false
27 changes: 16 additions & 11 deletions src/BetweenValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class BetweenValidator extends BaseValidator
{
use Translation;

/** @var mixed Min value */
/** @var int|float Min value */
protected $min;

/** @var mixed Max value */
/** @var int|float Max value */
protected $max;

/**
Expand All @@ -33,8 +33,8 @@ class BetweenValidator extends BaseValidator
*
* Required options:
*
* - min: (scalar) Minimum border
* - max: (scalar) Maximum border
* - min: (int|float) Minimum border
* - max: (int|float) Maximum border
*
* Optional options:
*
Expand All @@ -58,7 +58,7 @@ public function __construct(array $options)
/**
* Return the min option
*
* @return mixed
* @return int|float
*/
public function getMin()
{
Expand All @@ -68,7 +68,7 @@ public function getMin()
/**
* Set the min option
*
* @param mixed $min
* @param int|float $min
*
* @return $this
*/
Expand All @@ -82,7 +82,7 @@ public function setMin($min): self
/**
* Return the max option
*
* @return mixed
* @return int|float
*/
public function getMax()
{
Expand All @@ -92,7 +92,7 @@ public function getMax()
/**
* Set the max option
*
* @param mixed $max
* @param int|float $max
*
* @return $this
*/
Expand Down Expand Up @@ -120,14 +120,19 @@ public function getInclusive(): bool
*
* @return $this
*/
public function setInclusive($inclusive = true): self
public function setInclusive(bool $inclusive = true): self
{
$this->inclusive = (bool) $inclusive;
$this->inclusive = $inclusive;

return $this;
}

public function isValid($value)
/**
* @param int|float $value
*
* @return bool
*/
public function isValid($value): bool
{
// Multiple isValid() calls must not stack validation messages
$this->clearMessages();
Expand Down
2 changes: 1 addition & 1 deletion src/CallbackValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(callable $callback)
$this->callback = $callback;
}

public function isValid($value)
public function isValid($value): bool
{
// Multiple isValid() calls must not stack validation messages
$this->clearMessages();
Expand Down
4 changes: 4 additions & 0 deletions src/CidrValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class CidrValidator extends BaseValidator
{
use Translation;

/**
* @param string $value
* @return bool
*/
public function isValid($value): bool
{
$this->clearMessages();
Expand Down
6 changes: 3 additions & 3 deletions src/DateTimeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class DateTimeValidator extends BaseValidator
*
* @param bool $local
*/
public function __construct($local = true)
public function __construct(bool $local = true)
{
$this->local = (bool) $local;
$this->local = $local;
}

/**
Expand All @@ -35,7 +35,7 @@ public function __construct($local = true)
*
* @return bool
*/
public function isValid($value)
public function isValid($value): bool
{
// Multiple isValid() calls must not stack validation messages
$this->clearMessages();
Expand Down
3 changes: 2 additions & 1 deletion src/EmailAddressValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ private function validateMXRecords(string $hostname, string $email): bool
//decode IDN domain name
$decodedHostname = idn_to_ascii($hostname, 0, INTL_IDNA_VARIANT_UTS46);

$result = getmxrr($decodedHostname, $mxHosts);
$result = $decodedHostname && getmxrr($decodedHostname, $mxHosts);

if (! $result) {
$this->addMessage(sprintf(
$this->translate("'%s' does not appear to have a valid MX record for the email address '%s'"),
Expand Down
33 changes: 20 additions & 13 deletions src/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ public function setMaxFileNameLength(?int $maxFileNameLength): self
return $this;
}

public function isValid($value)
/**
* @param UploadedFileInterface|UploadedFileInterface[] $value
* @return bool
*/
public function isValid($value): bool
{
// Multiple isValid() calls must not stack validation messages
$this->clearMessages();
Expand All @@ -185,7 +189,7 @@ private function validateFile(UploadedFileInterface $file): bool
if ($this->getMaxSize() && $file->getSize() > $this->getMaxSize()) {
$this->addMessage(sprintf(
$this->translate('File %s is bigger than the allowed maximum size of %d'),
$file->getClientFileName(),
$file->getClientFilename(),
$this->getMaxSize()
));

Expand All @@ -195,7 +199,7 @@ private function validateFile(UploadedFileInterface $file): bool
if ($this->getMinSize() && $file->getSize() < $this->getMinSize()) {
$this->addMessage(sprintf(
$this->translate('File %s is smaller than the minimum required size of %d'),
$file->getClientFileName(),
$file->getClientFilename(),
$this->getMinSize()
));

Expand All @@ -205,7 +209,7 @@ private function validateFile(UploadedFileInterface $file): bool
if ($this->getMaxFileNameLength()) {
$strValidator = new StringLengthValidator(['max' => $this->getMaxFileNameLength()]);

if (! $strValidator->isValid($file->getClientFilename())) {
if (! $strValidator->isValid($file->getClientFilename() ?? '')) {
$this->addMessage(sprintf(
$this->translate('File name is longer than the allowed length of %d characters.'),
$this->maxFileNameLength
Expand All @@ -217,24 +221,27 @@ private function validateFile(UploadedFileInterface $file): bool

if (! empty($this->getAllowedMimeTypes())) {
$hasAllowedMimeType = false;
foreach ($this->getAllowedMimeTypes() as $type) {
$fileMimetype = $file->getClientMediaType();
if (($pos = strpos($type, '/*')) !== false) { // image/*
$typePrefix = substr($type, 0, $pos);
if (Str::startsWith($fileMimetype, $typePrefix)) {
$fileMimetype = $file->getClientMediaType();

if ($fileMimetype) {
foreach ($this->getAllowedMimeTypes() as $type) {
if (($pos = strpos($type, '/*')) !== false) { // image/*
$typePrefix = substr($type, 0, $pos);
if (Str::startsWith($fileMimetype, $typePrefix)) {
$hasAllowedMimeType = true;
break;
}
} elseif ($fileMimetype === $type) { // image/png
$hasAllowedMimeType = true;
break;
}
} elseif ($fileMimetype === $type) { // image/png
$hasAllowedMimeType = true;
break;
}
}

if (! $hasAllowedMimeType) {
$this->addMessage(sprintf(
$this->translate('File %s is of type %s. Only %s allowed.'),
$file->getClientFileName(),
$file->getClientFilename(),
$file->getClientMediaType(),
implode(', ', $this->allowedMimeTypes)
));
Expand Down
15 changes: 10 additions & 5 deletions src/GreaterThanValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class GreaterThanValidator extends BaseValidator
{
use Translation;

/** @var mixed Comparison value for greater than */
/** @var int|float Comparison value for greater than */
protected $min;

/**
* Create a new GreaterThanValidator
*
* Optional options:
* - min: (scalar) Comparison value for greater than, default 0
* - min: (int|float) Comparison value for greater than, default 0
*/
public function __construct(array $options = [])
{
Expand All @@ -28,7 +28,7 @@ public function __construct(array $options = [])
/**
* Get the min option
*
* @return mixed
* @return int|float
*/
public function getMin()
{
Expand All @@ -38,7 +38,7 @@ public function getMin()
/**
* Set the min option
*
* @param mixed $min
* @param int|float $min
*
* @return $this
*/
Expand All @@ -49,7 +49,12 @@ public function setMin($min): self
return $this;
}

public function isValid($value)
/**
* @param int|float $value
*
* @return bool
*/
public function isValid($value): bool
{
// Multiple isValid() calls must not stack validation messages
$this->clearMessages();
Expand Down
Loading