Skip to content

Commit

Permalink
check matching attribute, use type error, logic exception
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed May 16, 2024
1 parent 53ede83 commit 82f4b7a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
24 changes: 19 additions & 5 deletions src/ReflectionParameterTyped.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\ReflectionParameterTypedInterface;
use InvalidArgumentException;
use LogicException;
use ReflectionIntersectionType;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionUnionType;
use Throwable;
use TypeError;
use function Chevere\Message\message;

final class ReflectionParameterTyped implements ReflectionParameterTypedInterface
Expand All @@ -37,13 +38,26 @@ public function __construct(

try {
$attribute = reflectedParameterAttribute($reflection);
$parameter = $attribute->parameter();
} catch (Throwable) {
// Do nothing
// do nothing
}
if (isset($attribute, $this->type)) {
$typeHint = $this->type->getName();
$attrHint = $attribute->parameter()->type()->typeHinting();
if ($typeHint !== $attrHint) {
throw new TypeError(
(string) message(
'Parameter %name% of type %type% is not compatible with %attr% attribute',
name: $reflection->getName(),
type: $typeHint,
attr: $attribute->parameter()::class
)
);
}
$parameter = $attribute->parameter();
}
if ($this->reflection->isDefaultValueAvailable()
&& method_exists($parameter, 'withDefault')
&& $this->reflection->getDefaultValue() !== null
) {
/** @var ParameterInterface $parameter */
$parameter = $parameter
Expand Down Expand Up @@ -71,7 +85,7 @@ private function getType(): ?ReflectionNamedType
$name = '$' . $this->reflection->getName();
$type = $this->getReflectionType($reflection);

throw new InvalidArgumentException(
throw new LogicException(
(string) message(
'Parameter %name% of type %type% is not supported',
name: $name,
Expand Down
15 changes: 12 additions & 3 deletions tests/ReflectionParameterTypedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
use Chevere\Parameter\Interfaces\StringParameterInterface;
use Chevere\Parameter\ReflectionParameterTyped;
use Chevere\Tests\src\Depends;
use InvalidArgumentException;
use LogicException;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use ReflectionParameter;
use stdClass;
use TypeError;

final class ReflectionParameterTypedTest extends TestCase
{
Expand Down Expand Up @@ -71,19 +72,27 @@ public function testParameterDefault(): void
public function testUnion(): void
{
$parameter = $this->getReflection('useUnion');
$this->expectException(InvalidArgumentException::class);
$this->expectException(LogicException::class);
$this->expectExceptionMessage('$union of type union is not supported');
new ReflectionParameterTyped($parameter);
}

public function testIntersection(): void
{
$parameter = $this->getReflection('useIntersection');
$this->expectException(InvalidArgumentException::class);
$this->expectException(LogicException::class);
$this->expectExceptionMessage('$intersection of type intersection is not supported');
new ReflectionParameterTyped($parameter);
}

public function testInvalidAttribute(): void
{
$parameter = $this->getReflection('useInvalidAttribute');
$this->expectException(TypeError::class);
$this->expectExceptionMessage('Parameter int of type int is not compatible with Chevere\Parameter\StringParameter attribute');
new ReflectionParameterTyped($parameter);
}

private function getReflection(string $method, int $pos = 0): ReflectionParameter
{
$reflection = new ReflectionMethod(Depends::class, $method);
Expand Down
6 changes: 6 additions & 0 deletions tests/src/Depends.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ public function useUnion(string|int $union)
public function useIntersection(stdClass&Depends $intersection)
{
}

public function useInvalidAttribute(
#[StringAttr()]
int $int
) {
}
}

0 comments on commit 82f4b7a

Please sign in to comment.