Last active
October 16, 2024 13:31
-
-
Save janedbal/39ffeb55416e00d986dd988ac4b276d4 to your computer and use it in GitHub Desktop.
Following rule MAY improve PHPStan run time. It makes sense only when you disable GC for PHPStan execution.
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
<?php declare(strict_types = 1); | |
namespace ShipMonk\Rules\PHPStan\Rule; | |
use PhpParser\Node; | |
use PHPStan\Analyser\Scope; | |
use PHPStan\Node\InClassNode; | |
use PHPStan\Rules\IdentifierRuleError; | |
use PHPStan\Rules\Rule; | |
use function gc_collect_cycles; | |
/** | |
* @implements Rule<InClassNode> | |
*/ | |
class GarbageCollectorRule implements Rule | |
{ | |
private const int FREE_MEMORY_AFTER_CLASSES_ANALYSED = 100; | |
private int $classCounter = 0; | |
public function getNodeType(): string | |
{ | |
return InClassNode::class; | |
} | |
/** | |
* @param InClassNode $node | |
* @return list<IdentifierRuleError> | |
*/ | |
public function processNode( | |
Node $node, | |
Scope $scope, | |
): array | |
{ | |
if ($this->classCounter > self::FREE_MEMORY_AFTER_CLASSES_ANALYSED) { | |
gc_collect_cycles(); | |
$this->classCounter = 0; | |
} | |
$this->classCounter++; | |
return []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Disable GC e.g. by creating
bin/phpstan
and running that instead ofvendor/bin/phpstan
: