Skip to content

Commit

Permalink
ReflectionFunctionAbstract::getParameter() speedup
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Sep 24, 2022
1 parent 7261394 commit d0013ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/Reflection/ReflectionFunctionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use RuntimeException;

use function array_filter;
use function array_values;
use function assert;
use function count;
use function is_array;
Expand All @@ -29,7 +30,7 @@ trait ReflectionFunctionAbstract
/** @var non-empty-string */
private string $name;

/** @var list<ReflectionParameter> */
/** @var array<non-empty-string, ReflectionParameter> */
private array $parameters;

private bool $returnsReference;
Expand Down Expand Up @@ -100,21 +101,23 @@ private function fillFromNode(MethodNode|Node\Stmt\Function_|Node\Expr\Closure|N
}
}

/** @return list<ReflectionParameter> */
/** @return array<non-empty-string, ReflectionParameter> */
private function createParameters(Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node): array
{
$parameters = [];

/** @var list<Node\Param> $nodeParams */
$nodeParams = $node->params;
foreach ($nodeParams as $paramIndex => $paramNode) {
$parameters[] = ReflectionParameter::createFromNode(
$parameter = ReflectionParameter::createFromNode(
$this->reflector,
$paramNode,
$this,
$paramIndex,
$this->isParameterOptional($nodeParams, $paramIndex),
);

$parameters[$parameter->getName()] = $parameter;
}

return $parameters;
Expand Down Expand Up @@ -182,7 +185,7 @@ public function getNumberOfRequiredParameters(): int
*/
public function getParameters(): array
{
return $this->parameters;
return array_values($this->parameters);
}

/** @param list<Node\Param> $parameterNodes */
Expand Down Expand Up @@ -211,13 +214,7 @@ private function isParameterOptional(array $parameterNodes, int $parameterIndex)
*/
public function getParameter(string $parameterName): ReflectionParameter|null
{
foreach ($this->getParameters() as $parameter) {
if ($parameter->getName() === $parameterName) {
return $parameter;
}
}

return null;
return $this->parameters[$parameterName] ?? null;
}

public function getDocComment(): string|null
Expand Down
15 changes: 15 additions & 0 deletions test/unit/Reflection/ReflectionFunctionAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,21 @@ public function testGetParameterReturnsNullWhenNotFound(): void
self::assertNull($functionInfo->getParameter('d'));
}

public function testGetParameters(): void
{
$php = '<?php function foo($c, $b, $a) {}';

$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
$functionInfo = $reflector->reflectFunction('foo');
$parameters = $functionInfo->getParameters();

$expectedParameters = ['c', 'b', 'a'];

foreach ($parameters as $parameterNo => $parameter) {
self::assertSame($expectedParameters[$parameterNo], $parameter->getName());
}
}

public function testGetFileName(): void
{
$functionInfo = (new DefaultReflector(
Expand Down

0 comments on commit d0013ec

Please sign in to comment.