Skip to content

Commit

Permalink
Give up on some phpstan warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Jun 2, 2024
1 parent beb1387 commit 07c3cf0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
9 changes: 9 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ parameters:
paths:
- src
- tests
ignoreErrors:
-
message: "#^Function uuf6429\\\\Castable\\\\cast\\(\\) should return T but returns object\\.$#"
count: 1
path: src/functions.php
-
message: "#^Unable to resolve the template type T in call to method uuf6429\\\\Castable\\\\Castable\\:\\:castTo\\(\\)$#"
count: 1
path: src/functions.php
1 change: 1 addition & 0 deletions src/Castable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface Castable
* @template T
* @param class-string<T> $type
* @return T
* @throws NotCastableException
*/
public function castTo(string $type);
}
12 changes: 4 additions & 8 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* @param mixed $value
* @param class-string<T> $type
* @return T
* @throws NotCastableException
*/
function cast($value, string $type)
function cast(mixed $value, string $type)
{
static $basicTypes = ['bool', 'string', 'int', 'float', 'array', 'object', 'null'];
static $typeAliases = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float'];
Expand All @@ -35,16 +36,11 @@ function cast($value, string $type)
}

throw new NotCastableException(
sprintf('Cannot cast %s to %s', is_object($value) ? get_class($value) : gettype($value), $type)
sprintf('Cannot cast %s to %s', get_debug_type($value), $type)
);
} catch (Throwable $ex) {
throw new NotCastableException(
sprintf(
'Cannot cast %s to %s: %s',
is_object($value) ? get_class($value) : gettype($value),
$type,
$ex->getMessage()
),
sprintf('Cannot cast %s to %s: %s', get_debug_type($value), $type, $ex->getMessage()),
0,
$ex
);
Expand Down
13 changes: 8 additions & 5 deletions tests/CastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CastTest extends TestCase
* @param mixed $originalValue
* @param class-string $targetType
*/
public function test_that_invalid_casting_triggers_exception($originalValue, string $targetType): void
public function test_that_invalid_casting_triggers_exception(mixed $originalValue, string $targetType): void
{
$this->expectException(NotCastableException::class);

Expand All @@ -30,12 +30,12 @@ public static function invalidCastingDataProvider(): iterable
];

yield 'invalid conversion; object to string' => [
'originalValue' => (object)[],
'originalValue' => (object) [],
'targetType' => 'string',
];

yield 'invalid conversion; object to specific class' => [
'originalValue' => (object)[],
'originalValue' => (object) [],
'targetType' => ArrayObject::class,
];

Expand All @@ -51,8 +51,11 @@ public static function invalidCastingDataProvider(): iterable
* @param class-string $targetType
* @param mixed $expectedValue
*/
public function test_that_valid_casting_returns_expected_value($originalValue, string $targetType, $expectedValue): void
{
public function test_that_valid_casting_returns_expected_value(
mixed $originalValue,
string $targetType,
mixed $expectedValue
): void {
$this->assertSame($expectedValue, cast($originalValue, $targetType));
}

Expand Down
11 changes: 3 additions & 8 deletions tests/ExampleCastableClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

namespace uuf6429\Castable;

use InvalidArgumentException;

class ExampleCastableClass implements Castable
{
/**
* @param string $type
* @return int|string
*/
public function castTo(string $type)
public function castTo(string $type): int|string
{
/** @noinspection PhpSwitchCanBeReplacedWithMatchExpressionInspection */
switch ($type) {
case 'int':
return 123;
Expand All @@ -20,7 +15,7 @@ public function castTo(string $type)
return 'example';

default:
throw new InvalidArgumentException("Unsupported cast type: $type");
throw new NotCastableException("Unsupported cast type: $type");
}
}
}

0 comments on commit 07c3cf0

Please sign in to comment.