Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule registry now gets rules instances in lazy manner
- Loading branch information
1 parent
9bf5694
commit b1b0dc9
Showing
13 changed files
with
195 additions
and
91 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,59 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules; | ||
|
||
use PhpParser\Node; | ||
use function class_implements; | ||
use function class_parents; | ||
|
||
class DirectRegistry implements Registry | ||
{ | ||
|
||
/** @var Rule[][] */ | ||
private array $rules = []; | ||
|
||
/** @var Rule[][] */ | ||
private array $cache = []; | ||
|
||
/** | ||
* @param Rule[] $rules | ||
*/ | ||
public function __construct(array $rules) | ||
{ | ||
foreach ($rules as $rule) { | ||
$this->rules[$rule->getNodeType()][] = $rule; | ||
} | ||
} | ||
|
||
/** | ||
* @template TNodeType of Node | ||
* @phpstan-param class-string<TNodeType> $nodeType | ||
* @param Node $nodeType | ||
* @phpstan-return array<Rule<TNodeType>> | ||
* @return Rule[] | ||
*/ | ||
public function getRules(string $nodeType): array | ||
{ | ||
if (!isset($this->cache[$nodeType])) { | ||
$parentNodeTypes = [$nodeType] + class_parents($nodeType) + class_implements($nodeType); | ||
|
||
$rules = []; | ||
foreach ($parentNodeTypes as $parentNodeType) { | ||
foreach ($this->rules[$parentNodeType] ?? [] as $rule) { | ||
$rules[] = $rule; | ||
} | ||
} | ||
|
||
$this->cache[$nodeType] = $rules; | ||
} | ||
|
||
/** | ||
* @phpstan-var array<Rule<TNodeType>> $selectedRules | ||
* @var Rule[] $selectedRules | ||
*/ | ||
$selectedRules = $this->cache[$nodeType]; | ||
|
||
return $selectedRules; | ||
} | ||
|
||
} |
This file contains 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,74 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\DependencyInjection\Container; | ||
use function class_implements; | ||
use function class_parents; | ||
|
||
class LazyRegistry implements Registry | ||
{ | ||
|
||
public const RULE_TAG = 'phpstan.rules.rule'; | ||
|
||
/** @var Rule[][]|null */ | ||
private ?array $rules = null; | ||
|
||
/** @var Rule[][] */ | ||
private array $cache = []; | ||
|
||
public function __construct(private Container $container) | ||
{ | ||
} | ||
|
||
/** | ||
* @template TNodeType of Node | ||
* @phpstan-param class-string<TNodeType> $nodeType | ||
* @param Node $nodeType | ||
* @phpstan-return array<Rule<TNodeType>> | ||
* @return Rule[] | ||
*/ | ||
public function getRules(string $nodeType): array | ||
{ | ||
if (!isset($this->cache[$nodeType])) { | ||
$parentNodeTypes = [$nodeType] + class_parents($nodeType) + class_implements($nodeType); | ||
|
||
$rules = []; | ||
$rulesFromContainer = $this->getRulesFromContainer(); | ||
foreach ($parentNodeTypes as $parentNodeType) { | ||
foreach ($rulesFromContainer[$parentNodeType] ?? [] as $rule) { | ||
$rules[] = $rule; | ||
} | ||
} | ||
|
||
$this->cache[$nodeType] = $rules; | ||
} | ||
|
||
/** | ||
* @phpstan-var array<Rule<TNodeType>> $selectedRules | ||
* @var Rule[] $selectedRules | ||
*/ | ||
$selectedRules = $this->cache[$nodeType]; | ||
|
||
return $selectedRules; | ||
} | ||
|
||
/** | ||
* @return Rule[][] | ||
*/ | ||
private function getRulesFromContainer(): array | ||
{ | ||
if ($this->rules !== null) { | ||
return $this->rules; | ||
} | ||
|
||
$rules = []; | ||
foreach ($this->container->getServicesByTag(self::RULE_TAG) as $rule) { | ||
$rules[$rule->getNodeType()][] = $rule; | ||
} | ||
|
||
return $this->rules = $rules; | ||
} | ||
|
||
} |
This file contains 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 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
Oops, something went wrong.