Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Cli

public const PARAMETER_ONLY = 'only';

public const PARAMETER_MEMORY_LIMIT = 'memory-limit';

public static function isOnWindows(): bool
{
return PHP_OS_FAMILY === 'Windows';
Expand Down
9 changes: 9 additions & 0 deletions src/Commands/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ protected function configure(): void
'Only returns with exit code 0, regardless of any errors/warnings'
);

$this->addOption(
Cli::PARAMETER_MEMORY_LIMIT,
'm',
InputOption::VALUE_REQUIRED,
'Sets the PHP memory limit for child processes (e.g. "512M", "-1" for unlimited)'
);

$this->addArgument(
'files',
InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
Expand All @@ -129,6 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$context->isFixing = (bool) $input->getOption(Cli::FLAG_FIX) || $this->config->shouldAutoFix();
$context->runningInCi = (bool) $input->getOption(Cli::FLAG_CI);
$context->files = $files;
$context->memoryLimit = $input->getOption(Cli::PARAMETER_MEMORY_LIMIT);

if ($context->runningInCi) {
$input->setInteractive(false);
Expand Down Expand Up @@ -185,6 +193,7 @@ private function executeContext(InputInterface $input, OutputInterface $output,

$tool->setInput($input);
$tool->setOutput($output);
$tool->setContext($context);

$start = time();
$result = $tool->run($context);
Expand Down
3 changes: 3 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Context
/** @var string[] */
public $toolsExecuted = [];

/** @var string|null */
public $memoryLimit = null;

/** @var Result */
public $result;

Expand Down
5 changes: 5 additions & 0 deletions src/Tools/ComposerNormalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class ComposerNormalize extends Tool
/** @var string */
protected $name = 'composer-normalize';

protected function supportsMemoryLimit(): bool
{
return false;
}

public function shouldRun(Context $context): bool
{
// TODO does not check against file names, only full paths
Expand Down
5 changes: 5 additions & 0 deletions src/Tools/Deptrac.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class Deptrac extends Tool
/** @var string */
protected $name = 'deptrac';

protected function supportsMemoryLimit(): bool
{
return true;
}

public function run(Context $context): bool
{
$outputFile = $this->createTempReportFile();
Expand Down
5 changes: 5 additions & 0 deletions src/Tools/EasyCodingStandard.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class EasyCodingStandard extends Tool
/** @var string */
protected $name = 'ecs';

protected function supportsMemoryLimit(): bool
{
return true;
}

public function run(Context $context): bool
{
$output = [];
Expand Down
5 changes: 5 additions & 0 deletions src/Tools/Phan.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class Phan extends Tool
/** @var string */
protected $name = 'phan';

protected function supportsMemoryLimit(): bool
{
return true;
}

public function run(Context $context): bool
{
$outputFile = $this->createTempReportFile();
Expand Down
5 changes: 5 additions & 0 deletions src/Tools/PhpCodeSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class PhpCodeSniffer extends Tool
/** @var string */
protected $name = 'php_codesniffer';

protected function supportsMemoryLimit(): bool
{
return true;
}

public function run(Context $context): bool
{
if ($context->isFixing) {
Expand Down
5 changes: 5 additions & 0 deletions src/Tools/PhpMessDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class PhpMessDetector extends Tool
/** @var string */
protected $name = 'phpmd';

protected function supportsMemoryLimit(): bool
{
return true;
}

public function run(Context $context): bool
{
$output = [];
Expand Down
5 changes: 5 additions & 0 deletions src/Tools/PhpParallelLint/PhpParallelLint.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class PhpParallelLint extends Tool
/** @var string */
protected $name = self::NAME;

protected function supportsMemoryLimit(): bool
{
return true;
}

public function run(Context $context): bool
{
$config = $context->config->getPart($this->name);
Expand Down
5 changes: 5 additions & 0 deletions src/Tools/Phpstan.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class Phpstan extends Tool
/** @var string */
protected $name = 'phpstan';

protected function supportsMemoryLimit(): bool
{
return true;
}

public function run(Context $context): bool
{
$ignoreSources = (bool) ($context->config->getPart($this->getName())[Config::IGNORE_SOURCES] ?? false);
Expand Down
5 changes: 5 additions & 0 deletions src/Tools/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Psalm extends Tool
/** @var string */
protected $name = 'psalm';

protected function supportsMemoryLimit(): bool
{
return true;
}

public function run(Context $context): bool
{
$binary = self::vendorBinary($this->name);
Expand Down
5 changes: 5 additions & 0 deletions src/Tools/Rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Rector extends Tool
/** @var string */
protected $name = 'rector';

protected function supportsMemoryLimit(): bool
{
return true;
}

public function run(Context $context): bool
{
$binary = self::vendorBinary('rector');
Expand Down
18 changes: 17 additions & 1 deletion src/Tools/Tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ abstract class Tool
/** @var OutputInterface */
protected $output;

/** @var Context|null */
protected $context;

public function setInput(InputInterface $input): void
{
$this->input = $input;
Expand All @@ -45,6 +48,11 @@ public function setOutput(OutputInterface $output): void
$this->output = $output;
}

public function setContext(Context $context): void
{
$this->context = $context;
}

/**
* Runs this tool with the given context.
*/
Expand Down Expand Up @@ -84,7 +92,13 @@ protected function execute(
return $argument !== '';
});

$command = array_merge([$binary], $arguments);
$memoryLimit = $this->context !== null ? $this->context->memoryLimit : null;

if ($memoryLimit !== null && $this->supportsMemoryLimit()) {
$command = array_merge([PHP_BINARY, '-d', "memory_limit={$memoryLimit}", $binary], $arguments);
} else {
$command = array_merge([$binary], $arguments);
}

if ($this->output->isVeryVerbose()) {
$this->output->writeln('Executing: ' . implode(' ', $command), Output::OUTPUT_RAW);
Expand Down Expand Up @@ -129,6 +143,8 @@ protected function execute(
return (int) $process->getExitCode();
}

abstract protected function supportsMemoryLimit(): bool;

protected static function vendorBinary(string $binary): string
{
$binary = PHPCSTD_BINARY_PATH . $binary;
Expand Down