Passed
Push — 0.6.x ( 9a1948...a98caa )
by Shinji
03:23 queued 01:32
created

ProcessMemoryMapParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the reliforp/reli-prof 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 Reli\Lib\Process\MemoryMap;
15
16
use Reli\Lib\String\LineFetcher;
17
18
final class ProcessMemoryMapParser
19
{
20
    public function __construct(
21
        private LineFetcher $line_fetcher
22
    ) {
23
    }
24
25
    public function parse(string $memory_map_string): ProcessMemoryMap
26
    {
27
        $memory_areas = [];
28
        foreach ($this->line_fetcher->createIterable($memory_map_string) as $line) {
29
            $line_parsed = $this->parseLine($line);
30
            if ($line_parsed !== null) {
31
                $memory_areas[] = $line_parsed;
32
            }
33
        }
34
        return new ProcessMemoryMap($memory_areas);
35
    }
36
37
    private function parseLine(string $line): ?ProcessMemoryArea
38
    {
39
        $matches = [];
40
        preg_match(
41
            // phpcs:ignore Generic.Files.LineLength.TooLong
42
            '/([0-9a-f]+)-([0-9a-f]+) ([r\-][w\-][x\-][sp\-]) ([0-9a-f]+) ([0-9][0-9][0-9]?:[0-9][0-9][0-9]?) ([0-9]+) +([^ ].+)/',
43
            $line,
44
            $matches
45
        );
46
        if ($matches === []) {
47
            return null;
48
        }
49
        $begin = $matches[1];
50
        $end = $matches[2];
51
        $attribute_string = $matches[3];
52
        $attribute = new ProcessMemoryAttribute(
53
            $attribute_string[0] === 'r',
54
            $attribute_string[1] === 'w',
55
            $attribute_string[2] === 'x',
56
            $attribute_string[3] === 'p',
57
        );
58
        $file_offset = $matches[4];
59
        $name = $matches[7];
60
        return new ProcessMemoryArea($begin, $end, $file_offset, $attribute, $name);
61
    }
62
}
63