Skip to content

Commit

Permalink
typos
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Dec 16, 2023
1 parent 529ad37 commit 5aeafb9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
7 changes: 0 additions & 7 deletions src/Attributes/CallableAttr.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Attribute;
use Chevere\Parameter\Interfaces\ParameterAttributeInterface;
use Chevere\Parameter\Interfaces\ParameterInterface;
use LogicException;
use function Chevere\Parameter\object;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION)]
Expand All @@ -28,12 +27,6 @@ public function __construct(callable $callable)
{
$return = $callable();
object(ParameterInterface::class)($return);

Check warning on line 29 in src/Attributes/CallableAttr.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ public function __construct(callable $callable) { $return = $callable(); - object(ParameterInterface::class)($return); + $this->parameter = $return; } public function __invoke(mixed $mixed) : mixed

Check warning on line 29 in src/Attributes/CallableAttr.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ public function __construct(callable $callable) { $return = $callable(); - object(ParameterInterface::class)($return); + $this->parameter = $return; } public function __invoke(mixed $mixed) : mixed

Check warning on line 29 in src/Attributes/CallableAttr.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ public function __construct(callable $callable) { $return = $callable(); - object(ParameterInterface::class)($return); + $this->parameter = $return; } public function __invoke(mixed $mixed) : mixed
// if (!is_object($return)) {
// throw new LogicException('DynamicAttr must return an object');
// }
// if (! $return instanceof ParameterInterface) {
// throw new LogicException('DynamicAttr must return an object implementing ParameterInterface');
// }
$this->parameter = $return;
}

Expand Down
17 changes: 13 additions & 4 deletions src/Attributes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace Chevere\Parameter\Attributes;

use Chevere\Parameter\Interfaces\ArgumentsInterface;
use Chevere\Parameter\Interfaces\ParameterInterface;
use LogicException;
use ReflectionAttribute;
use ReflectionFunction;
use ReflectionMethod;
use Throwable;
Expand Down Expand Up @@ -154,9 +154,18 @@ function validReturn(mixed $var): mixed
$reflection = $class
? new ReflectionMethod($class, $method)
: new ReflectionFunction($method);
/** @var ReflectionAttribute<ReturnAttr> $attribute */
$attribute = $reflection->getAttributes(ReturnAttr::class)[0]
?? throw new LogicException('No return attribute found');
$attribute = $reflection->getAttributes(ReturnAttr::class)[0] ?? null;
if ($attribute === null) {
$function = "{$class}::return";
if (is_callable($function)) {
$parameter = $function($var);
if ($parameter instanceof ParameterInterface) {
return $parameter->__invoke($var);
}
}

throw new LogicException('No rules defined for return');
}

try {
return $attribute->newInstance()->__invoke($var);
Expand Down
4 changes: 2 additions & 2 deletions tests/src/UsesParameterAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
final class UsesParameterAttributes
{
#[ReturnAttr(
new CallableAttr(__CLASS__ . '::acceptResponse')
new CallableAttr(__CLASS__ . '::return')
)]
public function __construct(
#[StringAttr('/^[A-Za-z]+$/')]
Expand Down Expand Up @@ -61,7 +61,7 @@ public function __construct(
validReturn($id);
}

public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return int();
}
Expand Down

0 comments on commit 5aeafb9

Please sign in to comment.