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

OpenContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 1
eloc 9
c 3
b 1
f 1
nc 1
nop 1
dl 0
loc 16
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\SemanticLog\Profile\Verbose;
6
7
use BEAR\Resource\AbstractRequest;
8
use JsonSerializable;
9
use Koriym\SemanticLogger\AbstractContext;
10
use Koriym\SemanticLogger\Profiler\XdebugTrace;
0 ignored issues
show
Bug introduced by
The type Koriym\SemanticLogger\Profiler\XdebugTrace was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Koriym\SemanticLogger\Profiler\XHProfResult;
0 ignored issues
show
Bug introduced by
The type Koriym\SemanticLogger\Profiler\XHProfResult was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Override;
13
14
use function spl_object_hash;
15
use function strtolower;
16
use function strtoupper;
17
use function ucfirst;
18
use function uniqid;
19
20
final class OpenContext extends AbstractContext implements JsonSerializable
21
{
22
    /** @psalm-suppress InvalidClassConstantType */
23
    public const TYPE = 'bear_resource_request';
24
25
    /** @psalm-suppress InvalidClassConstantType */
26
    public const SCHEMA_URL = 'https://bearsunday.github.io/BEAR.Resource/schemas/open-context.json';
27
28
    public readonly string $method;
29
    public readonly string $uri;
30
31
    /** @var array{string, string} */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{string, string} at position 2 could not be parsed: Expected ':' at position 2, but found 'string'.
Loading history...
32
    public readonly array $call;
33
34
    /** @var array<string, string|null> */
35
    private static array $xdebugIdMap = [];
36
37
    public function __construct(AbstractRequest $request)
38
    {
39
        $this->method = strtoupper($request->method);
0 ignored issues
show
Bug introduced by
The property method is declared read-only in BEAR\Resource\SemanticLo...ile\Verbose\OpenContext.
Loading history...
40
        $this->uri = $request->toUri();
0 ignored issues
show
Bug introduced by
The property uri is declared read-only in BEAR\Resource\SemanticLo...ile\Verbose\OpenContext.
Loading history...
41
        $this->call = [
0 ignored issues
show
Bug introduced by
The property call is declared read-only in BEAR\Resource\SemanticLo...ile\Verbose\OpenContext.
Loading history...
42
            $request->resourceObject::class,
43
            'on' . ucfirst(strtolower($request->method)),
44
        ];
45
46
        // Start profiling (but don't capture data yet - that's for close)
47
        XHProfResult::start();
48
        XdebugTrace::start();
49
50
        // Generate an ID for profiling context
51
        $xdebugId = uniqid('profile_', true);
52
        self::$xdebugIdMap[spl_object_hash($this)] = $xdebugId;
53
    }
54
55
    public static function create(AbstractRequest $request): self
56
    {
57
        return new self($request);
58
    }
59
60
    public function getXdebugId(): ?string
61
    {
62
        return self::$xdebugIdMap[spl_object_hash($this)] ?? null;
63
    }
64
65
    /** @return array<string, mixed> */
66
    #[Override]
67
    public function jsonSerialize(): array
68
    {
69
        return [
70
            'method' => $this->method,
71
            'uri' => $this->uri,
72
            'call' => $this->call,
73
        ];
74
    }
75
}
76