-
Notifications
You must be signed in to change notification settings - Fork 510
Remove inefficient caching from PhpMethodReflection #3534
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ea07f8c
Remove inefficient caching from PhpMethodReflection
staabm da0aac6
Remove inefficient caching from PhpFunctionReflection
staabm 9a02775
Remove FunctionCallStatementFinder
staabm b8ee0bc
Added *Parser unit tests
staabm 0e03ad5
test nested classes
staabm c3cdb0b
test functions
staabm 687c50d
unique index for same-line anonymous classes
staabm fc7e0ca
Drop inClassLike property
staabm 13b1067
Revert "Drop inClassLike property"
staabm c958367
fix anonymous class detection
staabm f3fb80a
put test out of global namspace
staabm 676d440
fix wrong inClassLike after stack pop
staabm d4d5c1a
My vision
ondrejmirtes d1a8cbd
Optimization
ondrejmirtes cfbadfd
Another optimization
ondrejmirtes 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 was deleted.
Oops, something went wrong.
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,94 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Parser; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Name; | ||
use PhpParser\NodeVisitorAbstract; | ||
use PHPStan\Reflection\ParametersAcceptor; | ||
use function array_filter; | ||
use function array_key_exists; | ||
use function in_array; | ||
|
||
final class VariadicFunctionsVisitor extends NodeVisitorAbstract | ||
{ | ||
|
||
private ?Node $topNode = null; | ||
|
||
private ?string $inNamespace = null; | ||
|
||
private ?string $inFunction = null; | ||
|
||
/** @var array<string, bool> */ | ||
public static array $cache = []; | ||
|
||
/** @var array<string, bool> */ | ||
private array $variadicFunctions = []; | ||
|
||
public const ATTRIBUTE_NAME = 'variadicFunctions'; | ||
|
||
public function beforeTraverse(array $nodes): ?array | ||
{ | ||
$this->topNode = null; | ||
$this->variadicFunctions = []; | ||
$this->inNamespace = null; | ||
$this->inFunction = null; | ||
|
||
return null; | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if ($this->topNode === null) { | ||
$this->topNode = $node; | ||
} | ||
|
||
if ($node instanceof Node\Stmt\Namespace_ && $node->name !== null) { | ||
$this->inNamespace = $node->name->toString(); | ||
} | ||
|
||
if ($node instanceof Node\Stmt\Function_) { | ||
$this->inFunction = $this->inNamespace !== null ? $this->inNamespace . '\\' . $node->name->name : $node->name->name; | ||
} | ||
|
||
if ( | ||
$this->inFunction !== null | ||
&& $node instanceof Node\Expr\FuncCall | ||
&& $node->name instanceof Name | ||
&& in_array((string) $node->name, ParametersAcceptor::VARIADIC_FUNCTIONS, true) | ||
&& !array_key_exists($this->inFunction, $this->variadicFunctions) | ||
) { | ||
$this->variadicFunctions[$this->inFunction] = true; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function leaveNode(Node $node): ?Node | ||
{ | ||
if ($node instanceof Node\Stmt\Namespace_ && $node->name !== null) { | ||
$this->inNamespace = null; | ||
} | ||
|
||
if ($node instanceof Node\Stmt\Function_ && $this->inFunction !== null) { | ||
$this->variadicFunctions[$this->inFunction] ??= false; | ||
$this->inFunction = null; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function afterTraverse(array $nodes): ?array | ||
{ | ||
if ($this->topNode !== null && $this->variadicFunctions !== []) { | ||
foreach ($this->variadicFunctions as $name => $variadic) { | ||
self::$cache[$name] = $variadic; | ||
} | ||
$functions = array_filter($this->variadicFunctions, static fn (bool $variadic) => $variadic); | ||
$this->topNode->setAttribute(self::ATTRIBUTE_NAME, $functions); | ||
} | ||
|
||
return 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,125 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Parser; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\NodeVisitorAbstract; | ||
use PHPStan\Reflection\ParametersAcceptor; | ||
use function array_key_exists; | ||
use function array_pop; | ||
use function count; | ||
use function in_array; | ||
use function sprintf; | ||
|
||
final class VariadicMethodsVisitor extends NodeVisitorAbstract | ||
{ | ||
|
||
public const ATTRIBUTE_NAME = 'variadicMethods'; | ||
|
||
public const ANONYMOUS_CLASS_PREFIX = 'class@anonymous'; | ||
|
||
private ?Node $topNode = null; | ||
|
||
private ?string $inNamespace = null; | ||
|
||
/** @var array<string> */ | ||
private array $classStack = []; | ||
|
||
private ?string $inMethod = null; | ||
|
||
/** @var array<string, array<string, true>> */ | ||
public static array $cache = []; | ||
|
||
/** @var array<string, array<string, true>> */ | ||
private array $variadicMethods = []; | ||
|
||
public function beforeTraverse(array $nodes): ?array | ||
{ | ||
$this->topNode = null; | ||
$this->variadicMethods = []; | ||
$this->inNamespace = null; | ||
$this->classStack = []; | ||
$this->inMethod = null; | ||
|
||
return null; | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if ($this->topNode === null) { | ||
$this->topNode = $node; | ||
} | ||
|
||
if ($node instanceof Node\Stmt\Namespace_ && $node->name !== null) { | ||
$this->inNamespace = $node->name->toString(); | ||
} | ||
|
||
if ($node instanceof Node\Stmt\ClassLike) { | ||
if (!$node->name instanceof Node\Identifier) { | ||
$className = sprintf('%s:%s:%s', self::ANONYMOUS_CLASS_PREFIX, $node->getStartLine(), $node->getEndLine()); | ||
$this->classStack[] = $className; | ||
} else { | ||
$className = $node->name->name; | ||
$this->classStack[] = $this->inNamespace !== null ? $this->inNamespace . '\\' . $className : $className; | ||
} | ||
} | ||
|
||
if ($node instanceof ClassMethod) { | ||
$this->inMethod = $node->name->name; | ||
} | ||
|
||
if ( | ||
$this->inMethod !== null | ||
&& $node instanceof Node\Expr\FuncCall | ||
&& $node->name instanceof Name | ||
&& in_array((string) $node->name, ParametersAcceptor::VARIADIC_FUNCTIONS, true) | ||
) { | ||
$lastClass = $this->classStack[count($this->classStack) - 1] ?? null; | ||
if ($lastClass !== null) { | ||
if ( | ||
!array_key_exists($lastClass, $this->variadicMethods) | ||
|| !array_key_exists($this->inMethod, $this->variadicMethods[$lastClass]) | ||
) { | ||
$this->variadicMethods[$lastClass][$this->inMethod] = true; | ||
} | ||
} | ||
|
||
} | ||
|
||
return null; | ||
} | ||
|
||
public function leaveNode(Node $node): ?Node | ||
{ | ||
if ($node instanceof ClassMethod) { | ||
$this->inMethod = null; | ||
} | ||
|
||
if ($node instanceof Node\Stmt\ClassLike) { | ||
array_pop($this->classStack); | ||
} | ||
|
||
if ($node instanceof Node\Stmt\Namespace_ && $node->name !== null) { | ||
$this->inNamespace = null; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function afterTraverse(array $nodes): ?array | ||
{ | ||
if ($this->topNode !== null && $this->variadicMethods !== []) { | ||
foreach ($this->variadicMethods as $class => $methods) { | ||
foreach ($methods as $name => $variadic) { | ||
self::$cache[$class][$name] = $variadic; | ||
} | ||
} | ||
$this->topNode->setAttribute(self::ATTRIBUTE_NAME, $this->variadicMethods); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.