Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Container/Support/CallableInvoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,18 @@ private function resolveCallable(object|string|array|callable $target): Callable
return new CallableWrapper($target, new ReflectionMethod($target, '__invoke'));
}

// 4. String class → resolve and use __invoke
// 4. Plain string function name
if (is_string($target) && function_exists($target)) {
return new CallableWrapper(null, new ReflectionFunction($target));
}

// 5. String class → resolve and use __invoke
if (is_string($target) && class_exists($target)) {
$resolved = $this->serviceResolver->resolve($target);
return new CallableWrapper($resolved, new ReflectionMethod($resolved, '__invoke'));
}

// 5. String "Class::method"
// 6. String "Class::method"
if (is_string($target) && str_contains($target, '::')) {
$parts = explode('::', $target, 2);
if (count($parts) === 2) {
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/Container/CallableInvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Maduser\Argon\Container\Support\CallableInvoker;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReflectionParameter;
use RuntimeException;
use TypeError;

Expand Down Expand Up @@ -104,6 +105,26 @@ public function testThrowsForUnsupportedCallableType(): void
$this->invoker->call('not_a_callable_string', []);
}

/**
* @throws ContainerException
* @throws NotFoundException
*/
public function testResolvesPlainStringFunctionCallable(): void
{
$this->parameterResolver->method('resolve')->willReturnCallback(
static function (ReflectionParameter $param, array $overrides = []): mixed {
$name = $param->getName();

/** @var array<string, mixed> $overrides */
return array_key_exists($name, $overrides) ? $overrides[$name] : null;
}
);

$result = $this->invoker->call('strtoupper', ['string' => 'foo']);

$this->assertSame('FOO', $result);
}

/**
* @throws NotFoundException
* @throws ContainerException
Expand Down