Skip to content

Commit

Permalink
Merge pull request #30 from Icinga/phpstan
Browse files Browse the repository at this point in the history
Github Actions: Add Phpstan
  • Loading branch information
nilmerg authored Aug 24, 2023
2 parents 48ff606 + fa93b58 commit b53fba1
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 58 deletions.
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

0 comments on commit b53fba1

Please sign in to comment.