1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Moved code to get memory_limit in bytes to command functions since that is where it is used.

This commit is contained in:
kolja 2020-02-24 22:18:46 +01:00 committed by Matthew Brown
parent 99e5ded20c
commit 215f1c381e
4 changed files with 39 additions and 31 deletions

View File

@ -487,3 +487,29 @@ function get_path_to_config(array $options): ?string
}
return $path_to_config;
}
function getMemoryLimitInBytes(): int
{
$limit = ini_get('memory_limit');
// for unlimited = -1
if ($limit < 0) {
return -1;
}
if (preg_match('/^(\d+)(\D?)$/', $limit, $matches)) {
$limit = (int)$matches[1];
switch (strtoupper($matches[2] ?? '')) {
case 'G':
$limit *= 1024 * 1024 * 1024;
break;
case 'M':
$limit *= 1024 * 1024;
break;
case 'K':
$limit *= 1024;
break;
}
}
return (int)$limit;
}

View File

@ -2,9 +2,6 @@
namespace Psalm;
use Webmozart\PathUtil\Path;
use function preg_match;
use function ini_get;
use function strtoupper;
/**
* @param string $path
@ -16,29 +13,3 @@ function isAbsolutePath($path)
{
return Path::isAbsolute($path);
}
function getMemoryLimitInBytes(): int
{
$limit = ini_get('memory_limit');
// for unlimited = -1
if ($limit < 0) {
return -1;
}
if (preg_match('/^(\d+)(\D?)$/', $limit, $matches)) {
$limit = (int)$matches[1];
switch (strtoupper($matches[2] ?? '')) {
case 'G':
$limit *= 1024 * 1024 * 1024;
break;
case 'M':
$limit *= 1024 * 1024;
break;
case 'K':
$limit *= 1024;
break;
}
}
return (int)$limit;
}

View File

@ -6,12 +6,17 @@ use Psalm\Config;
use Psalm\IssueBuffer;
use Psalm\Progress\DebugProgress;
use Psalm\Progress\DefaultProgress;
use function getMemoryLimitInBytes;
// show all errors
error_reporting(-1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
ini_set('memory_limit', '4096M');
$memLimit = getMemoryLimitInBytes();
// Magic number is 4096M in bytes
if ($memLimit > 0 && $memLimit < 4294967296) {
ini_set('memory_limit', '4096M');
}
gc_collect_cycles();
gc_disable();

View File

@ -1,11 +1,17 @@
<?php
namespace Psalm\Tests\Functions;
use function Psalm\getMemoryLimitInBytes;
use function getMemoryLimitInBytes;
use function ini_set;
class GetMemoryLimitInBytesTest extends \Psalm\Tests\TestCase
{
public function setUp(): void
{
require_once 'src/command_functions.php';
parent::setUp();
}
/**
* @return array<int,array<string|int>>
*/