diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index 8c7df6c3a64..7401cd1e621 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -57,10 +57,8 @@ use function implode; use function in_array; use function ini_get; -use function ini_set; use function is_array; use function is_numeric; -use function is_scalar; use function is_string; use function json_encode; use function max; @@ -189,7 +187,7 @@ public static function run(array $argv): void self::validateCliArguments($args); - self::setMemoryLimit($options); + CliUtils::setMemoryLimit($options); self::syncShortOptions($options); @@ -462,29 +460,6 @@ static function (string $arg): void { ); } - /** - * @param array> $options - */ - private static function setMemoryLimit(array $options): void - { - if (!array_key_exists('use-ini-defaults', $options)) { - ini_set('display_errors', 'stderr'); - ini_set('display_startup_errors', '1'); - - $memoryLimit = (8 * 1_024 * 1_024 * 1_024); - - if (array_key_exists('memory-limit', $options)) { - $memoryLimit = $options['memory-limit']; - - if (!is_scalar($memoryLimit)) { - throw new ConfigException('Invalid memory limit specified.'); - } - } - - ini_set('memory_limit', (string) $memoryLimit); - } - } - /** * @param array $args */ diff --git a/src/Psalm/Internal/Cli/Psalter.php b/src/Psalm/Internal/Cli/Psalter.php index 6d5dfa0cf50..9dd8eaf47d0 100644 --- a/src/Psalm/Internal/Cli/Psalter.php +++ b/src/Psalm/Internal/Cli/Psalter.php @@ -43,7 +43,6 @@ use function getopt; use function implode; use function in_array; -use function ini_set; use function is_array; use function is_dir; use function is_numeric; @@ -89,6 +88,7 @@ final class Psalter 'add-newline-between-docblock-annotations:', 'no-cache', 'no-progress', + 'memory-limit:', ]; /** @param array $argv */ @@ -100,8 +100,6 @@ public static function run(array $argv): void ErrorHandler::install($argv); - self::setMemoryLimit(); - $args = array_slice($argv, 1); // get options from command line @@ -109,6 +107,8 @@ public static function run(array $argv): void self::validateCliArguments($args); + CliUtils::setMemoryLimit($options); + self::syncShortOptions($options); if (isset($options['c']) && is_array($options['c'])) { @@ -442,15 +442,6 @@ public static function run(array $argv): void IssueBuffer::finish($project_analyzer, false, $start_time); } - private static function setMemoryLimit(): void - { - $memLimit = CliUtils::getMemoryLimitInBytes(); - // Magic number is 4096M in bytes - if ($memLimit > 0 && $memLimit < 8 * 1_024 * 1_024 * 1_024) { - ini_set('memory_limit', (string) (8 * 1_024 * 1_024 * 1_024)); - } - } - /** @param array $args */ private static function validateCliArguments(array $args): void { diff --git a/src/Psalm/Internal/CliUtils.php b/src/Psalm/Internal/CliUtils.php index fc1f979feb6..1e5a1abdd6e 100644 --- a/src/Psalm/Internal/CliUtils.php +++ b/src/Psalm/Internal/CliUtils.php @@ -14,8 +14,8 @@ use RuntimeException; use function array_filter; +use function array_key_exists; use function array_slice; -use function assert; use function count; use function define; use function dirname; @@ -27,13 +27,13 @@ use function fwrite; use function implode; use function in_array; -use function ini_get; +use function ini_set; use function is_array; use function is_dir; +use function is_scalar; use function is_string; use function json_decode; use function preg_last_error_msg; -use function preg_match; use function preg_replace; use function preg_split; use function realpath; @@ -41,7 +41,6 @@ use function stream_set_blocking; use function strlen; use function strpos; -use function strtoupper; use function substr; use function substr_replace; use function trim; @@ -446,38 +445,27 @@ public static function getPathToConfig(array $options): ?string } /** - * @psalm-pure + * @param array> $options + * @throws ConfigException */ - public static function getMemoryLimitInBytes(): int + public static function setMemoryLimit(array $options): void { - return self::convertMemoryLimitToBytes(ini_get('memory_limit')); - } + if (!array_key_exists('use-ini-defaults', $options)) { + ini_set('display_errors', 'stderr'); + ini_set('display_startup_errors', '1'); - /** @psalm-pure */ - public static function convertMemoryLimitToBytes(string $limit): int - { - // for unlimited = -1 - if ($limit < 0) { - return -1; - } + $memoryLimit = (8 * 1_024 * 1_024 * 1_024); + + if (array_key_exists('memory-limit', $options)) { + $memoryLimit = $options['memory-limit']; - if (preg_match('/^(\d+)(\D?)$/', $limit, $matches)) { - assert(isset($matches[1])); - $limit = (int)$matches[1]; - switch (strtoupper($matches[2] ?? '')) { - case 'G': - $limit *= 1_024 * 1_024 * 1_024; - break; - case 'M': - $limit *= 1_024 * 1_024; - break; - case 'K': - $limit *= 1_024; - break; + if (!is_scalar($memoryLimit)) { + throw new ConfigException('Invalid memory limit specified.'); + } } - } - return (int)$limit; + ini_set('memory_limit', (string) $memoryLimit); + } } public static function initPhpVersion(array $options, Config $config, ProjectAnalyzer $project_analyzer): void diff --git a/tests/CommandFunctions/GetMemoryLimitInBytesTest.php b/tests/CommandFunctions/GetMemoryLimitInBytesTest.php deleted file mode 100644 index fee4bae2d21..00000000000 --- a/tests/CommandFunctions/GetMemoryLimitInBytesTest.php +++ /dev/null @@ -1,54 +0,0 @@ -> - */ - public function memoryLimitSettingProvider(): array - { - return [ - // unlimited - [-1, -1], - // byte values - [1, 1], - [512, 512], - [2_048, 2_048], - // uppercase units - ['1K', 1_024], - ['24K', 24_576], - ['1M', 1_048_576], - ['24M', 25_165_824], - ['1G', 1_073_741_824], - ['24G', 25_769_803_776], - // lowercase units - ['1k', 1_024], - ['24k', 24_576], - ['1m', 1_048_576], - ['24m', 25_165_824], - ['1g', 1_073_741_824], - ['24g', 25_769_803_776], - ]; - } - - /** - * @dataProvider memoryLimitSettingProvider - * @param int|string $setting - * @param int|string $expectedBytes - */ - public function testGetMemoryLimitInBytes( - $setting, - $expectedBytes - ): void { - $this->assertSame( - $expectedBytes, - CliUtils::convertMemoryLimitToBytes((string)$setting), - 'Memory limit in bytes does not fit setting', - ); - } -}