Skip to content

Commit 09d18c0

Browse files
committed
Introducing BytesHelper
1 parent 1639213 commit 09d18c0

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

src/Command/InceptionResult.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Command;
44

55
use PHPStan\DependencyInjection\Container;
6+
use PHPStan\Internal\BytesHelper;
67
use function memory_get_peak_usage;
78

89
class InceptionResult
@@ -99,29 +100,11 @@ public function getGenerateBaselineFile(): ?string
99100
public function handleReturn(int $exitCode): int
100101
{
101102
if ($this->getErrorOutput()->isVerbose()) {
102-
$this->getErrorOutput()->writeLineFormatted(sprintf('Used memory: %s', $this->bytes(memory_get_peak_usage(true))));
103+
$this->getErrorOutput()->writeLineFormatted(sprintf('Used memory: %s', BytesHelper::bytes(memory_get_peak_usage(true))));
103104
}
104105

105106
@unlink($this->memoryLimitFile);
106107
return $exitCode;
107108
}
108109

109-
private function bytes(int $bytes): string
110-
{
111-
$bytes = round($bytes);
112-
$units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];
113-
foreach ($units as $unit) {
114-
if (abs($bytes) < 1024 || $unit === end($units)) {
115-
break;
116-
}
117-
$bytes /= 1024;
118-
}
119-
120-
if (!isset($unit)) {
121-
throw new \PHPStan\ShouldNotHappenException();
122-
}
123-
124-
return round($bytes, 2) . ' ' . $unit;
125-
}
126-
127110
}

src/Internal/BytesHelper.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Internal;
4+
5+
class BytesHelper
6+
{
7+
8+
public static function bytes(int $bytes): string
9+
{
10+
$bytes = round($bytes);
11+
$units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];
12+
foreach ($units as $unit) {
13+
if (abs($bytes) < 1024 || $unit === end($units)) {
14+
break;
15+
}
16+
$bytes /= 1024;
17+
}
18+
19+
if (!isset($unit)) {
20+
throw new \PHPStan\ShouldNotHappenException();
21+
}
22+
23+
return round($bytes, 2) . ' ' . $unit;
24+
}
25+
26+
}

0 commit comments

Comments
 (0)