Passed
Push — master ( 39df7e...5db39e )
by Shinji
02:01
created

ProcessModuleMemoryMap::isInRange()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 10
c 0
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 implements ProcessModuleMemoryMapInterface
21
{
22
    /** @var ProcessMemoryArea[] */
23
    private array $memory_areas;
24
    private ?int $base_address = null;
25
    /** @var array<int,int>|null  */
26
    private ?array $sorted_offset_to_memory_map = null;
27
28
    /**
29
     * ProcessModuleMemoryMap constructor.
30
     * @param ProcessMemoryArea[] $memory_areas
31
     */
32
    public function __construct(array $memory_areas)
33
    {
34
        $this->memory_areas = $memory_areas;
35
    }
36
37
    public function getBaseAddress(): int
38
    {
39
        if (!isset($this->base_address)) {
40
            $base_address = PHP_INT_MAX;
41
            foreach ($this->memory_areas as $memory_area) {
42
                $base_address = min($base_address, hexdec($memory_area->begin));
43
            }
44
            $this->base_address = $base_address;
45
        }
46
        return $this->base_address;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->base_address could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
47
    }
48
49
    public function getMemoryAddressFromOffset(int $offset): int
50
    {
51
        $ranges = $this->getSortedOffsetToMemoryAreaMap();
52
        $file_offset_decided = 0;
53
        foreach ($ranges as $file_offset => $memory_begin) {
54
            if ($file_offset <= $offset) {
55
                $file_offset_decided = $file_offset;
56
            }
57
        }
58
        return $ranges[$file_offset_decided] + ($offset - $file_offset_decided);
59
    }
60
61
    public function isInRange(int $address): bool
62
    {
63
        foreach ($this->memory_areas as $memory_area) {
64
            if ($memory_area->isInRange($address)) {
65
                return true;
66
            }
67
        }
68
        return false;
69
    }
70
71
    /**
72
     * @return array<int, int>
73
     */
74
    private function getSortedOffsetToMemoryAreaMap(): array
75
    {
76
        if (!isset($this->sorted_offset_to_memory_map)) {
77
            $ranges = [];
78
            foreach ($this->memory_areas as $memory_area) {
79
                $ranges[hexdec($memory_area->file_offset)] = hexdec($memory_area->begin);
80
            }
81
            ksort($ranges);
82
            $this->sorted_offset_to_memory_map = $ranges;
83
        }
84
        return $this->sorted_offset_to_memory_map;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->sorted_offset_to_memory_map could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
85
    }
86
}
87