Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
MethodFactory
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
6 / 6
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 resolveArguments
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 reflectPublicMethod
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 findPublicStaticMethod
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
5
 resolveObject
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of php-fast-forward/container.
7 *
8 * This source file is subject to the license bundled
9 * with this source code in the file LICENSE.
10 *
11 * @copyright Copyright (c) 2025-2026 Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 *
14 * @see       https://github.com/php-fast-forward/container
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\Container\Factory;
20
21use ReflectionMethod;
22use ReflectionException;
23use FastForward\Container\Exception\RuntimeException;
24use Psr\Container\ContainerInterface;
25use Psr\Container\NotFoundExceptionInterface;
26
27/**
28 * A factory that invokes a public method on a class or a container-provided object.
29 *
30 * This factory MUST be used when service creation requires calling a non-constructor method,
31 * and supports both static and instance methods.
32 *
33 * If the method is not public, a RuntimeException SHALL be thrown.
34 *
35 * Arguments MAY be resolved from the container if passed as service identifiers.
36 */
37  class MethodFactory implements FactoryInterface
38{
39    /**
40     * @var array<int|string, mixed> positional or named arguments passed to the method
41     */
42    private array $arguments;
43
44    /**
45     * Constructs the MethodFactory.
46     *
47     * @param string $class the class name or container service ID on which the method is called
48     * @param string $method the name of the method to invoke
49     * @param mixed ...$arguments Optional arguments to pass to the method.
50     */
51    public function __construct(
52        private string $class,
53        private string $method,
54        mixed ...$arguments,
55    ) {
56        $this->arguments = $arguments;
57    }
58
59    /**
60     * Resolves the class and invokes the configured method with arguments.
61     *
62     * Arguments MAY be resolved from the container if passed as string identifiers and found.
63     * Public static class methods are invoked without resolving an instance. Instance targets
64     * are retrieved by class name or service ID and reflected using the resolved object.
65     * Direct construction is permitted only when has() reports a class-name entry as absent.
66     * Failures resolving registered targets propagate, including missing nested dependencies.
67     *
68     * @param ContainerInterface $container The container used to resolve the class and arguments
69     *
70     * @return mixed The result of invoking the method
71     *
72     * @throws ReflectionException If the method does not exist
73     * @throws RuntimeException If the method is not public
74     * @throws NotFoundExceptionInterface if a non-class service ID is absent
75     */
76    public function __invoke(ContainerInterface $container): mixed
77    {
78        $arguments = $this->resolveArguments($container);
79
80        $method = $this->findPublicStaticMethod();
81
82        if ($method instanceof ReflectionMethod) {
83            return $method->invokeArgs(null, $arguments);
84        }
85
86        $object = $this->resolveObject($container);
87
88        return $this->reflectPublicMethod($object)
89            ->invokeArgs($object, $arguments);
90    }
91
92    /**
93     * @param ContainerInterface $container the source of named method arguments
94     *
95     * @return array<int|string, mixed> resolved arguments with their positional or named keys
96     */
97    private function resolveArguments(ContainerInterface $container): array
98    {
99        return array_map(
100            static fn($argument) => \is_string($argument) && $container->has($argument)
101                ? $container->get($argument)
102                : $argument,
103            $this->arguments
104        );
105    }
106
107    /**
108     * @param object|class-string $target the resolved object or a static method's class
109     *
110     * @return ReflectionMethod the public method to invoke
111     *
112     * @throws ReflectionException if the method does not exist
113     * @throws RuntimeException if the method is not public
114     */
115    private function reflectPublicMethod(object|string $target): ReflectionMethod
116    {
117        $reflectionMethod = new ReflectionMethod($target, $this->method);
118
119        if (! $reflectionMethod->isPublic()) {
120            throw RuntimeException::forNonPublicMethod($this->class, $this->method);
121        }
122
123        return $reflectionMethod;
124    }
125
126    /**
127     * @return ReflectionMethod|null a public static method that needs no target instance
128     */
129    private function findPublicStaticMethod(): ?ReflectionMethod
130    {
131        if (! method_exists($this->class, $this->method)) {
132            return null;
133        }
134
135        $method = new ReflectionMethod($this->class, $this->method);
136
137        return $method->isPublic() && $method->isStatic() && ! $method->isAbstract() ? $method : null;
138    }
139
140    /**
141     * @param ContainerInterface $container the source of the target instance
142     *
143     * @return object the configured instance, or a class with no registered entry
144     *
145     * @throws NotFoundExceptionInterface if a non-class service ID is absent
146     */
147    private function resolveObject(ContainerInterface $container): object
148    {
149        if (! class_exists($this->class) || $container->has($this->class)) {
150            return $container->get($this->class);
151        }
152
153        $this->reflectPublicMethod($this->class);
154
155        return new ($this->class)();
156    }
157}