Skip to content

Commit

Permalink
fix attribute error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed May 18, 2024
1 parent 3d7cd84 commit 5e36fb2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
23 changes: 23 additions & 0 deletions src/Exceptions/AttributeNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Parameter\Exceptions;

use Exception;

/**
* Exception thrown when there's no attribute defined.
*/
final class AttributeNotFoundException extends Exception
{
}
15 changes: 9 additions & 6 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ArrayAccess;
use Chevere\Parameter\Attributes\ReturnAttr;
use Chevere\Parameter\Exceptions\AttributeNotFoundException;
use Chevere\Parameter\Exceptions\ParameterException;
use Chevere\Parameter\Exceptions\ReturnException;
use Chevere\Parameter\Interfaces\ArgumentsInterface;
Expand Down Expand Up @@ -255,11 +256,10 @@ function reflectionToParameters(ReflectionFunction|ReflectionMethod $reflection)
foreach ($reflection->getParameters() as $reflectionParameter) {
try {
$push = reflectedParameterAttribute($reflectionParameter);
$push = $push->parameter();
} catch (LogicException) {
$reflectType = new ReflectionParameterTyped($reflectionParameter);
$push = $reflectType->parameter();
} catch (AttributeNotFoundException) {
$push = new ReflectionParameterTyped($reflectionParameter);
}
$push = $push->parameter();
if ($reflectionParameter->isDefaultValueAvailable()
&& $reflectionParameter->getDefaultValue() !== null
&& $push->default() === null
Expand Down Expand Up @@ -319,9 +319,12 @@ function reflectionToReturn(ReflectionFunction|ReflectionMethod $reflection): Pa
function reflectedParameterAttribute(
ReflectionParameter $reflection,
): ParameterAttributeInterface {
$attributes = $reflection->getAttributes(ParameterAttributeInterface::class, ReflectionAttribute::IS_INSTANCEOF);
$attributes = $reflection->getAttributes(
ParameterAttributeInterface::class,
ReflectionAttribute::IS_INSTANCEOF
);
if ($attributes === []) {
throw new LogicException(
throw new AttributeNotFoundException(
(string) message(
'No `%type%` attribute for parameter `%name%`',
type: ParameterAttributeInterface::class,
Expand Down

0 comments on commit 5e36fb2

Please sign in to comment.