Passed
Pull Request — 1.x (#334)
by Akihito
02:30
created

XHProfResult::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\SemanticLog\Profile;
6
7
use JsonSerializable;
8
use Override;
9
10
use function function_exists;
11
use function xhprof_disable;
12
use function xhprof_enable;
13
14
use const XHPROF_FLAGS_CPU;
15
use const XHPROF_FLAGS_MEMORY;
16
use const XHPROF_FLAGS_NO_BUILTINS;
17
18
final class XHProfResult implements JsonSerializable
19
{
20
    /** @param array<string, mixed>|null $data */
21
    public function __construct(
22
        public readonly ?array $data = null,
23
    ) {
24
    }
25
26
    public static function start(): self
27
    {
28
        if (! function_exists('xhprof_enable')) {
29
            return new self(); // @codeCoverageIgnore
30
        }
31
32
        /** @psalm-suppress UndefinedConstant, MixedArgument */
33
        xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
34
35
        return new self();
36
    }
37
38
    public function stop(string $uri): self
0 ignored issues
show
Unused Code introduced by
The parameter $uri is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
    public function stop(/** @scrutinizer ignore-unused */ string $uri): self

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        if (! function_exists('xhprof_disable')) {
41
            return new self(); // @codeCoverageIgnore
42
        }
43
44
        /** @var array<string, array<string, int>>|false $xhprofData */
45
        $xhprofData = xhprof_disable();
46
47
        if ($xhprofData === false || $xhprofData === []) {
48
            return new self();
49
        }
50
51
        return new self($xhprofData);
52
    }
53
54
    /** @return array<string, mixed> */
55
    #[Override]
56
    public function jsonSerialize(): array
57
    {
58
        if ($this->data === null) {
59
            return [];
60
        }
61
62
        return [
63
            'data' => $this->data,
64
            'spec_url' => 'https://github.com/tideways/php-xhprof-extension?tab=readme-ov-file#data-format',
65
        ];
66
    }
67
}
68