Passed
Push — renovate/php-8.x ( bfb893...222c0a )
by
unknown
02:20
created

RawString::fromCastedCData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
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\PhpInternals\Types\C;
15
16
use FFI\CData;
17
use PhpProfiler\Lib\PhpInternals\CastedCData;
18
use PhpProfiler\Lib\Process\Pointer\Dereferencable;
19
use PhpProfiler\Lib\Process\Pointer\Pointer;
20
21
final class RawString implements Dereferencable
22
{
23
    /** @psalm-suppress PropertyNotSetInConstructor */
24
    public string $value;
25
26
    /** @param CastedCData<CData> $cdata */
27
    public function __construct(
28
        private CastedCData $cdata,
29
        private int $len,
30
    ) {
31
        unset($this->value);
32
    }
33
34
    public function __toString(): string
35
    {
36
        return $this->value;
37
    }
38
39
    public function __get(string $field_name): string
40
    {
41
        return match ($field_name) {
42
            'value' => $this->value = substr(
43
                \FFI::string($this->cdata->casted),
44
                0,
45
                $this->len
46
            ),
47
        };
48
    }
49
50
    public static function getCTypeName(): string
51
    {
52
        return 'char[0]';
53
    }
54
55
    /** @param CastedCData<CData> $casted_cdata */
56
    public static function fromCastedCData(
57
        CastedCData $casted_cdata,
58
        Pointer $pointer
59
    ): static {
60
        return new self($casted_cdata, $pointer->size);
61
    }
62
}
63