Skip to content

Instantly share code, notes, and snippets.

@janedbal
Last active October 16, 2024 13:31
Show Gist options
  • Save janedbal/39ffeb55416e00d986dd988ac4b276d4 to your computer and use it in GitHub Desktop.
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.
<?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 [];
}
}
@janedbal
Copy link
Author

Disable GC e.g. by creating bin/phpstan and running that instead of vendor/bin/phpstan:

#!/usr/bin/env php
<?php declare(strict_types = 1);

gc_disable();

Phar::loadPhar(__DIR__ . '/../vendor/phpstan/phpstan/phpstan.phar', 'phpstan.phar');

require 'phar://phpstan.phar/bin/phpstan';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment