Passed
Push — master ( 41676d...b3d63d )
by Shinji
01:19
created

ProcessModuleMemoryMap::getBegin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Lib\Process\MemoryMap;
15
16
/**
17
 * Class ProcessModuleMemoryMap
18
 * @package PhpProfiler\Lib\Process\MemoryMap
19
 */
20
final class ProcessModuleMemoryMap
21
{
22
    /** @var ProcessMemoryArea[] */
23
    private array $memory_areas;
24
25
    /**
26
     * ProcessModuleMemoryMap constructor.
27
     * @param ProcessMemoryArea[] $memory_areas
28
     */
29
    public function __construct(array $memory_areas)
30
    {
31
        $this->memory_areas = $memory_areas;
32
    }
33
34
    public function getBegin(): int
35
    {
36
        $begin = PHP_INT_MAX;
37
        foreach ($this->memory_areas as $memory_area) {
38
            $begin = min($begin, hexdec($memory_area->begin));
39
        }
40
        return $begin;
41
    }
42
43
    public function getMemoryAddressFromOffset(int $offset): int
44
    {
45
        $ranges = [];
46
        foreach ($this->memory_areas as $memory_area) {
47
            $ranges[hexdec($memory_area->file_offset)] = hexdec($memory_area->begin);
48
        }
49
        ksort($ranges);
50
        $file_offset_decided = 0;
51
        foreach ($ranges as $file_offset => $memory_begin) {
52
            if ($file_offset <= $offset) {
53
                $file_offset_decided = $file_offset;
54
            }
55
        }
56
        return $ranges[$file_offset_decided] + ($offset - $file_offset_decided);
57
    }
58
}
59