Skip to content

Commit

Permalink
[Performance] Fix filter cache on SkippedClassResolver (#5460)
Browse files Browse the repository at this point in the history
* [Performance] Fix filter cache on SkippedPathsResolver

* [ci-review] Rector Rectify

* fix @var

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 13, 2024
1 parent ac072bc commit 2ec5570
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
5 changes: 2 additions & 3 deletions rules/CodeQuality/Rector/Switch_/SingularSwitchToIfRector.php
Expand Up @@ -88,10 +88,9 @@ public function refactor(Node $node): array|If_|null
// only default → basically unwrap
if (! $onlyCase->cond instanceof Expr) {
// remove default clause because it cause syntax error
return array_filter($onlyCase->stmts, function (Stmt $statement) {
return ! $statement instanceof Break_;
});
return array_filter($onlyCase->stmts, static fn(Stmt $stmt): bool => ! $stmt instanceof Break_);
}

$if = new If_(new Identical($node->cond, $onlyCase->cond));
$if->stmts = $this->switchManipulator->removeBreakNodes($onlyCase->stmts);

Expand Down
12 changes: 7 additions & 5 deletions src/Skipper/SkipCriteriaResolver/SkippedClassResolver.php
Expand Up @@ -11,26 +11,28 @@
final class SkippedClassResolver
{
/**
* @var array<string, string[]|null>
* @var null|array<string, string[]|null>
*/
private array $skippedClasses = [];
private null|array $skippedClasses = null;

/**
* @return array<string, string[]|null>
*/
public function resolve(): array
{
// disable cache in tests
if (StaticPHPUnitEnvironment::isPHPUnitRun()) {
// disable cache in tests
$this->skippedClasses = [];
$this->skippedClasses = null;
}

// skip cache in tests
if ($this->skippedClasses !== []) {
// already cached, even only empty array
if ($this->skippedClasses !== null) {
return $this->skippedClasses;
}

$skip = SimpleParameterProvider::provideArrayParameter(Option::SKIP);
$this->skippedClasses = [];

foreach ($skip as $key => $value) {
// e.g. [SomeClass::class] → shift values to [SomeClass::class => null]
Expand Down
2 changes: 1 addition & 1 deletion src/Skipper/SkipCriteriaResolver/SkippedPathsResolver.php
Expand Up @@ -34,7 +34,7 @@ public function resolve(): array
$this->skippedPaths = null;
}

// already filled, even empty array
// already cached, even only empty array
if ($this->skippedPaths !== null) {
return $this->skippedPaths;
}
Expand Down

0 comments on commit 2ec5570

Please sign in to comment.