-
Notifications
You must be signed in to change notification settings - Fork 1
Introduce Argument Augmenter #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
henriquemoody
wants to merge
1
commit into
Respect:main
Choose a base branch
from
henriquemoody:augment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: ISC | ||
| * SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| * SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Respect\Parameter; | ||
|
|
||
| use ReflectionFunctionAbstract; | ||
|
|
||
| interface Augmenter | ||
| { | ||
| /** | ||
| * Augment the given arguments with values for the parameters they do not already fill. | ||
| * | ||
| * The given arguments are authoritative: they are never rebound, reordered, | ||
| * or padded with defaults or null. Only parameters left unfilled may gain a | ||
| * value, added as named arguments. Variadic and builtin-typed parameters | ||
| * are never augmented. | ||
| * | ||
| * @param array<int|string, mixed> $arguments Positional and/or named arguments | ||
| * | ||
| * @return array<int|string, mixed> | ||
| */ | ||
| public function augment(ReflectionFunctionAbstract $reflection, array $arguments): array; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: ISC | ||
| * SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| * SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Respect\Parameter; | ||
|
|
||
| use Psr\Container\ContainerInterface; | ||
| use ReflectionFunctionAbstract; | ||
| use ReflectionMethod; | ||
| use ReflectionNamedType; | ||
|
|
||
| use function array_filter; | ||
| use function array_is_list; | ||
| use function array_key_exists; | ||
| use function array_keys; | ||
| use function class_exists; | ||
| use function count; | ||
| use function in_array; | ||
| use function interface_exists; | ||
| use function is_int; | ||
|
|
||
| /** | ||
| * Augments arguments with services from a PSR-11 container. | ||
| * | ||
| * Types listed as unresolvable are never looked up in the container, which | ||
| * keeps value-like classes (clocks, dates) from being served as services. | ||
| */ | ||
| final class ContainerAugmenter implements Augmenter | ||
| { | ||
| /** @var array<string, list<array{int, string, class-string}>> */ | ||
| private array $augmentableParametersCache = []; | ||
|
|
||
| /** @param array<class-string> $unresolvableTypes */ | ||
| public function __construct( | ||
| private readonly ContainerInterface $container, | ||
| private readonly array $unresolvableTypes = [], | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int|string, mixed> $arguments Positional and/or named arguments | ||
| * | ||
| * @return array<int|string, mixed> | ||
| */ | ||
| public function augment(ReflectionFunctionAbstract $reflection, array $arguments): array | ||
| { | ||
| if (count($arguments) >= $reflection->getNumberOfParameters()) { | ||
| return $arguments; | ||
| } | ||
|
|
||
| $augmentableParameters = $this->augmentableParameters($reflection); | ||
| if ($augmentableParameters === []) { | ||
| return $arguments; | ||
| } | ||
|
|
||
| $positionalArgumentsCount = count( | ||
| array_is_list($arguments) ? $arguments : array_filter(array_keys($arguments), is_int(...)), | ||
| ); | ||
|
|
||
| foreach ($augmentableParameters as [$position, $name, $type]) { | ||
| if ($position < $positionalArgumentsCount || array_key_exists($name, $arguments)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!$this->container->has($type)) { | ||
| continue; | ||
| } | ||
|
|
||
| $arguments[$name] = $this->container->get($type); | ||
| } | ||
|
|
||
| return $arguments; | ||
| } | ||
|
|
||
| /** @return list<array{int, string, class-string}> */ | ||
| private function augmentableParameters(ReflectionFunctionAbstract $reflection): array | ||
| { | ||
| $cacheKey = self::createCacheKey($reflection); | ||
| if (isset($this->augmentableParametersCache[$cacheKey])) { | ||
| return $this->augmentableParametersCache[$cacheKey]; | ||
| } | ||
|
|
||
| $parameters = []; | ||
| foreach ($reflection->getParameters() as $parameter) { | ||
| $type = $parameter->getType(); | ||
| if ($parameter->isVariadic() || !$type instanceof ReflectionNamedType || $type->isBuiltin()) { | ||
| continue; | ||
| } | ||
|
|
||
| $typeName = $type->getName(); | ||
| if (!class_exists($typeName) && !interface_exists($typeName)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (in_array($typeName, $this->unresolvableTypes, true)) { | ||
| continue; | ||
| } | ||
|
|
||
| $parameters[] = [$parameter->getPosition(), $parameter->getName(), $typeName]; | ||
| } | ||
|
|
||
| return $this->augmentableParametersCache[$cacheKey] = $parameters; | ||
| } | ||
|
|
||
| private static function createCacheKey(ReflectionFunctionAbstract $reflection): string | ||
| { | ||
| if ($reflection instanceof ReflectionMethod) { | ||
| return $reflection->class . '::' . $reflection->name; | ||
| } | ||
|
|
||
| if (!$reflection->isClosure()) { | ||
| return $reflection->name; | ||
| } | ||
|
|
||
| $file = $reflection->getFileName() ?: 'internal'; | ||
| $line = $reflection->getStartLine() ?: 0; | ||
|
|
||
| return $reflection->getName() . '@' . $file . ':' . $line; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: ISC | ||
| * SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| * SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Respect\Parameter\Test\Fixtures; | ||
|
|
||
| final class OptionalServiceConsumer | ||
| { | ||
| public function __construct( | ||
| public readonly string $name = 'default', | ||
| public readonly SampleService|null $service = null, | ||
| ) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: ISC | ||
| * SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| * SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| // phpcs:disable Squiz.Functions.GlobalFunction.Found | ||
|
|
||
| namespace Respect\Parameter\Test\Fixtures; | ||
|
|
||
| function namedFunctionWithService(string $name, SampleService $service): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // phpcs:ignore SlevomatCodingStandard.PHP.RequireExplicitAssertion.RequiredExplicitAssertion | ||
| function functionWithNonExistentType(NonExistentClass123 $x): bool // @phpstan-ignore class.notFound | ||
| { | ||
| return true; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you run benchmarks? Claude often recommends caching reflection, but in PHP 8.5, he's wrong. Reflection is fast now, and caching it often decreases performance in some cases.
There are legitimate cases though (such as the attribute/template thing we did on Validation which involves multiple reflection calls that coalesce in a single cache).