Skip to content

Commit

Permalink
Early return Collectors when narrow-param is disabled (#44)
Browse files Browse the repository at this point in the history
collectors store their data in the PHPStan result cache. we shouldn't pollute the result cache with data if we don't need it.
  • Loading branch information
staabm authored Sep 18, 2024
1 parent c36dd8e commit fcb0ea2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Reflection\ClassReflection;
use Rector\TypePerfect\Configuration;
use Rector\TypePerfect\Matcher\Collector\PublicClassMethodMatcher;
use Rector\TypePerfect\PhpDoc\ApiDocStmtAnalyzer;
use Rector\TypePerfect\Printer\CollectorMetadataPrinter;
Expand All @@ -21,7 +22,8 @@
public function __construct(
private ApiDocStmtAnalyzer $apiDocStmtAnalyzer,
private PublicClassMethodMatcher $publicClassMethodMatcher,
private CollectorMetadataPrinter $collectorMetadataPrinter
private CollectorMetadataPrinter $collectorMetadataPrinter,
private Configuration $configuration
) {
}

Expand All @@ -36,6 +38,10 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): ?array
{
if (! $this->configuration->isNarrowParamEnabled()) {
return null;
}

if ($node->params === []) {
return null;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Collector/MethodCall/MethodCallArgTypesCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Reflection\ExtendedMethodReflection;
use Rector\TypePerfect\Configuration;
use Rector\TypePerfect\Matcher\ClassMethodCallReferenceResolver;
use Rector\TypePerfect\Printer\CollectorMetadataPrinter;
use Rector\TypePerfect\ValueObject\MethodCallReference;
Expand All @@ -22,6 +23,7 @@
public function __construct(
private ClassMethodCallReferenceResolver $classMethodCallReferenceResolver,
private CollectorMetadataPrinter $collectorMetadataPrinter,
private Configuration $configuration
) {
}

Expand All @@ -36,6 +38,10 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): ?array
{
if (! $this->configuration->isNarrowParamEnabled()) {
return null;
}

if ($node->isFirstClassCallable() || $node->getArgs() === [] || ! $node->name instanceof Identifier) {
return null;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Collector/MethodCallableNode/MethodCallableCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Node\MethodCallableNode;
use Rector\TypePerfect\Configuration;
use Rector\TypePerfect\Matcher\ClassMethodCallReferenceResolver;
use Rector\TypePerfect\ValueObject\MethodCallReference;

Expand All @@ -22,6 +23,7 @@
{
public function __construct(
private ClassMethodCallReferenceResolver $classMethodCallReferenceResolver,
private Configuration $configuration
) {
}

Expand All @@ -36,6 +38,10 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): ?array
{
if (! $this->configuration->isNarrowParamEnabled()) {
return null;
}

$classMethodCallReference = $this->classMethodCallReferenceResolver->resolve($node, $scope, true);
if (! $classMethodCallReference instanceof MethodCallReference) {
return null;
Expand Down

0 comments on commit fcb0ea2

Please sign in to comment.