ZendString   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __get() 0 8 1
A getCTypeName() 0 3 1
A fromCastedCData() 0 14 1
A getValuePointer() 0 6 1
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\Zend;
15
16
use FFI\PhpInternals\zend_string;
17
use PhpProfiler\Lib\PhpInternals\CastedCData;
18
use PhpProfiler\Lib\PhpInternals\Types\C\RawString;
19
use PhpProfiler\Lib\Process\Pointer\Dereferencable;
20
use PhpProfiler\Lib\Process\Pointer\Pointer;
21
22
final class ZendString implements Dereferencable
23
{
24
    /** @psalm-suppress PropertyNotSetInConstructor */
25
    public int $h;
26
    /** @psalm-suppress PropertyNotSetInConstructor */
27
    public int $len;
28
    /**
29
     * @psalm-suppress PropertyNotSetInConstructor
30
     * @var Pointer<RawString>
31
     */
32
    public Pointer $val;
33
34
    /** @param CastedCData<zend_string> $casted_cdata */
35
    public function __construct(
36
        private CastedCData $casted_cdata,
37
        private int $offset_to_val,
38
    ) {
39
        unset($this->h);
40
        unset($this->len);
41
        unset($this->val);
42
    }
43
44
    public function __get(string $field_name)
45
    {
46
        return match ($field_name) {
47
            'h' => $this->h = $this->casted_cdata->casted->h,
48
            'len' => $this->len = $this->casted_cdata->casted->len,
49
            'val' => $this->val = Pointer::fromCData(
50
                RawString::class,
51
                $this->casted_cdata->casted->val,
52
            )
53
        };
54
    }
55
56
    public static function getCTypeName(): string
57
    {
58
        return 'zend_string';
59
    }
60
61
    public static function fromCastedCData(
62
        CastedCData $casted_cdata,
63
        Pointer $pointer
64
    ): static {
65
        /** @var CastedCData<zend_string> $casted_cdata */
66
        /*
67
        $head_addr = \FFI::cast('long', \FFI::addr($cdata))->cdata;
68
        $val_addr = \FFI::cast('long', \FFI::addr($cdata->val))->cdata;
69
        $offset_to_val = $val_addr - $head_addr;
70
71
        return new self($cdata, $offset_to_val);
72
        */
73
        // an almost safe assumption I think
74
        return new self($casted_cdata, 24);
75
    }
76
77
    /**
78
     * @param Pointer<ZendString> $pointer
79
     * @return Pointer<RawString>
80
     */
81
    public function getValuePointer(Pointer $pointer): Pointer
82
    {
83
        return new Pointer(
84
            RawString::class,
85
            $pointer->address + $this->offset_to_val,
86
            $this->len
87
        );
88
    }
89
}
90