1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Merge pull request #6031 from weirdan/separate-memory-units-decoding

Separate memory limit formatting from retrieval
This commit is contained in:
Bruce Weirdan 2021-07-02 03:16:43 +03:00 committed by GitHub
commit ef0009ca46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 27 deletions

View File

@ -589,7 +589,12 @@ HELP;
*/
public static function getMemoryLimitInBytes(): int
{
$limit = ini_get('memory_limit');
return self::convertMemoryLimitToBytes(ini_get('memory_limit'));
}
/** @psalm-pure */
public static function convertMemoryLimitToBytes(string $limit): int
{
// for unlimited = -1
if ($limit < 0) {
return -1;

View File

@ -1,27 +1,11 @@
<?php
namespace Psalm\Tests\CommandFunctions;
use Psalm\Internal\CliUtils;
use function ini_get;
use function ini_set;
/**
* testcase for src/command_functions.php
*/
class GetMemoryLimitInBytesTest extends \Psalm\Tests\TestCase
{
/**
* @var string
*/
private $previousLimit;
public function setUp(): void
{
$this->previousLimit = (string)ini_get('memory_limit');
parent::setUp();
}
/**
* @return array<int,array<string|int>>
*/
@ -56,19 +40,15 @@ class GetMemoryLimitInBytesTest extends \Psalm\Tests\TestCase
*
* @param int|string $setting
* @param int|string $expectedBytes
*
*/
public function testGetMemoryLimitInBytes(
$setting,
$expectedBytes
): void {
ini_set('memory_limit', (string)$setting);
$this->assertSame($expectedBytes, CliUtils::getMemoryLimitInBytes(), 'Memory limit in bytes does not fit setting');
}
public function tearDown(): void
{
ini_set('memory_limit', $this->previousLimit);
parent::tearDown();
$this->assertSame(
$expectedBytes,
CliUtils::convertMemoryLimitToBytes((string)$setting),
'Memory limit in bytes does not fit setting'
);
}
}