Skip to content

Commit

Permalink
Fix several issue reported by PHPstan
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 20, 2024
1 parent d096542 commit d6cc83f
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 25 deletions.
20 changes: 10 additions & 10 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use BadMethodCallException; // Requires ext-spl
use DateTime; // Requires ext-date
use DateTimeImmutable; // Request ext-date
use DateTimeImmutable; // Requires ext-date
use InvalidArgumentException; // Requires ext-spl
use Throwable;
use Webmozart\Assert\Assert as Webmozart;
Expand Down Expand Up @@ -219,8 +219,8 @@
* @method static void allContains(iterable<string> $value, string $subString, string $message = '', class-string $exception = '')
* @method static void nullOrNotContains(string|null $value, string $subString, string $message = '', class-string $exception = '')
* @method static void allNotContains(iterable<string> $value, string $subString, string $message = '', class-string $exception = '')
* @method static void nullOrWhitespaceOnly(string|null $value, string $message = '', class-string $exception = '')
* @method static void allWhitespaceOnly(iterable<string> $value, string $message = '', class-string $exception = '')
* @method static void nullOrNotWhitespaceOnly(string|null $value, string $message = '', class-string $exception = '')
* @method static void allNotWhitespaceOnly(iterable<string> $value, string $message = '', class-string $exception = '')
* @method static void nullOrStartsWith(string|null $value, string $prefix, string $message = '', class-string $exception = '')
* @method static void allStartsWith(iterable<string> $value, string $prefix, string $message = '', class-string $exception = '')
* @method static void nullOrNotStartsWith(string|null $value, string $prefix, string $message = '', class-string $exception = '')
Expand Down Expand Up @@ -315,7 +315,7 @@ final class Assert

/**
* @param string $name
* @param array $arguments
* @param array<mixed> $arguments
*/
public static function __callStatic(string $name, array $arguments): void
{
Expand Down Expand Up @@ -367,7 +367,7 @@ public static function __callStatic(string $name, array $arguments): void
* Handle nullOr* for either Webmozart or for our custom assertions
*
* @param callable $method
* @param array $arguments
* @param array<mixed> $arguments
* @return void
*/
private static function nullOr(callable $method, array $arguments): void
Expand All @@ -381,7 +381,7 @@ private static function nullOr(callable $method, array $arguments): void
* all* for our custom assertions
*
* @param callable $method
* @param array $arguments
* @param array<mixed> $arguments
* @return void
*/
private static function all(callable $method, array $arguments): void
Expand All @@ -402,6 +402,10 @@ private static function all(callable $method, array $arguments): void
*/
protected static function valueToString(mixed $value): string
{
if (is_resource($value)) {
return 'resource';
}

if (null === $value) {
return 'null';
}
Expand Down Expand Up @@ -430,10 +434,6 @@ protected static function valueToString(mixed $value): string
return $value::class;
}

if (is_resource($value)) {
return 'resource';
}

if (is_string($value)) {
return '"' . $value . '"';
}
Expand Down
33 changes: 32 additions & 1 deletion src/CustomAssertionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,37 @@

/**
* @package simplesamlphp/assert
*
* @method static void validDuration(mixed $value, string $message = '', class-string $exception = '')
* @method static void stringPlausibleBase64(mixed $value, string $message = '', class-string $exception = '')
* @method static void validDateTime(mixed $value, string $message = '', class-string $exception = '')
* @method static void validDateTimeZulu(mixed $value, string $message = '', class-string $exception = '')
* @method static void notInArray(mixed $value, array $values, string $message = '', class-string $exception = '')
* @method static void validURN(mixed $value, string $message = '', class-string $exception = '')
* @method static void validURI(mixed $value, string $message = '', class-string $exception = '')
* @method static void validURL(mixed $value, string $message = '', class-string $exception = '')
* @method static void validNCName(mixed $value, string $message = '', class-string $exception = '')
* @method static void validQName(mixed $value, string $message = '', class-string $exception = '')
* @method static void nullOrValidDuration(mixed $value, string $message = '', class-string $exception = '')
* @method static void nullOrStringPlausibleBase64(mixed $value, string $message = '', class-string $exception = '')
* @method static void nullOrValidDateTime(mixed $value, string $message = '', class-string $exception = '')
* @method static void nullOrValidDateTimeZulu(mixed $value, string $message = '', class-string $exception = '')
* @method static void nullOrNotInArray(mixed $value, array $values, string $message = '', class-string $exception = '')
* @method static void nullOrValidURN(mixed $value, string $message = '', class-string $exception = '')
* @method static void nullOrValidURI(mixed $value, string $message = '', class-string $exception = '')
* @method static void nullOrValidURL(mixed $value, string $message = '', class-string $exception = '')
* @method static void nullOrValidNCName(mixed $value, string $message = '', class-string $exception = '')
* @method static void nullOrValidQName(mixed $value, string $message = '', class-string $exception = '')
* @method static void allValidDuration(mixed $value, string $message = '', class-string $exception = '')
* @method static void allStringPlausibleBase64(mixed $value, string $message = '', class-string $exception = '')
* @method static void allValidDateTime(mixed $value, string $message = '', class-string $exception = '')
* @method static void allValidDateTimeZulu(mixed $value, string $message = '', class-string $exception = '')
* @method static void allNotInArray(mixed $value, array $values, string $message = '', class-string $exception = '')
* @method static void allValidURN(mixed $value, string $message = '', class-string $exception = '')
* @method static void allValidURI(mixed $value, string $message = '', class-string $exception = '')
* @method static void allValidURL(mixed $value, string $message = '', class-string $exception = '')
* @method static void allValidNCName(mixed $value, string $message = '', class-string $exception = '')
* @method static void allValidQName(mixed $value, string $message = '', class-string $exception = '')
*/
trait CustomAssertionTrait
{
Expand Down Expand Up @@ -144,7 +175,7 @@ private static function validDateTimeZulu(string $value, string $message = ''):

/**
* @param mixed $value
* @param array $values
* @param array<mixed> $values
* @param string $message
*/
private static function notInArray($value, array $values, string $message = ''): void
Expand Down
9 changes: 6 additions & 3 deletions tests/Assert/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use SimpleSAML\Test\Utils\TestClass;
use stdClass;

use function getcwd;
use function opendir;
use function sys_get_temp_dir;

/**
* Class \SimpleSAML\Assert\Assert
Expand Down Expand Up @@ -57,6 +57,7 @@ public function testAssertionFailingWithCustomExceptionThrowsCustomException():
public function testUnknownAssertionRaisesBadMethodCallException(): void
{
$this->expectException(BadMethodCallException::class);
// @phpstan-ignore-next-line
Assert::thisAssertionDoesNotExist('a', 'b', LogicException::class);
}

Expand All @@ -66,6 +67,7 @@ public function testUnknownAssertionRaisesBadMethodCallException(): void
public function testUnknownNullOrAssertionRaisesBadMethodCallException(): void
{
$this->expectException(BadMethodCallException::class);
// @phpstan-ignore-next-line
Assert::nullOrThisAssertionDoesNotExist('a', 'b', LogicException::class);
}

Expand All @@ -75,6 +77,7 @@ public function testUnknownNullOrAssertionRaisesBadMethodCallException(): void
public function testUnknownAllAssertionRaisesBadMethodCallException(): void
{
$this->expectException(BadMethodCallException::class);
// @phpstan-ignore-next-line
Assert::allThisAssertionDoesNotExist('a', 'b', LogicException::class);
}

Expand Down Expand Up @@ -127,7 +130,7 @@ public function testValueToString(mixed $value, string $expected): void


/**
* @return array
* @return array<string, array{0: mixed, 1: string}>
*/
public static function provideValue(): array
{
Expand All @@ -137,7 +140,7 @@ public static function provideValue(): array

$otherObject = new stdClass();

$resource = opendir(getcwd());
$resource = opendir(sys_get_temp_dir());

return [
'null' => [null, 'null'],
Expand Down
2 changes: 1 addition & 1 deletion tests/Assert/Base64Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testStringPlausibleBase64(bool $shouldPass, string $name): void


/**
* @return array
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideBase64(): array
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Assert/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testValidDateTimeZulu(bool $shouldPass, string $timestamp): void


/**
* @return array
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideDateTime(): array
{
Expand All @@ -68,7 +68,7 @@ public static function provideDateTime(): array


/**
* @return array
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideDateTimeZulu(): array
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Assert/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testValidDuration(bool $shouldPass, string $duration): void


/**
* @return array
* @return array<int, array{0: bool, 1: string}>
*/
public static function provideDuration(): array
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Assert/NCNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testValidNCName(bool $shouldPass, string $name): void


/**
* @return array
* @return array<int, array{0: bool, 1: string}>
*/
public static function provideNCName(): array
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Assert/NotInArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class NotInArrayTest extends TestCase
* @dataProvider provideNotInArray
* @param boolean $shouldPass
* @param mixed $item
* @param array $arr
* @param array<mixed> $arr
*/
public function testnotInArray(bool $shouldPass, $item, array $arr): void
{
Expand All @@ -36,7 +36,7 @@ public function testnotInArray(bool $shouldPass, $item, array $arr): void


/**
* @return array
* @return array<int, array{0: bool, 1: int, 2: array{0: int}}>
*/
public static function provideNotInArray(): array
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Assert/QNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testValidQName(bool $shouldPass, string $name): void


/**
* @return array
* @return array<int, array{0: bool, 1: string}>
*/
public static function provideQName(): array
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Assert/URITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testValidURN(bool $shouldPass, string $urn): void


/**
* @return array
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideURI(): array
{
Expand All @@ -84,7 +84,7 @@ public static function provideURI(): array


/**
* @return array
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideURL(): array
{
Expand All @@ -99,7 +99,7 @@ public static function provideURL(): array


/**
* @return array
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideURN(): array
{
Expand Down

0 comments on commit d6cc83f

Please sign in to comment.