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

CompleteContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 23
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\SemanticLog\Profile\Verbose;
6
7
use BEAR\Resource\ResourceObject;
8
use BEAR\Resource\SemanticLog\Profile\PhpProfile;
9
use BEAR\Resource\SemanticLog\Profile\Profile;
10
use BEAR\Resource\Types;
11
use JsonSerializable;
12
use Koriym\SemanticLogger\AbstractContext;
13
use Override;
14
15
/**
16
 * @psalm-import-type Headers from Types
17
 * @psalm-import-type HttpBody from Types
18
 */
19
final class CompleteContext extends AbstractContext implements JsonSerializable
20
{
21
    /** @psalm-suppress InvalidClassConstantType */
22
    public const TYPE = 'bear_resource_complete';
23
24
    /** @psalm-suppress InvalidClassConstantType */
25
    public const SCHEMA_URL = 'https://bearsunday.github.io/BEAR.Resource/schemas/complete-context.json';
26
27
    public readonly string $uri;
28
    public readonly int $code;
29
30
    /** @var Headers */
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\SemanticLog\Profile\Verbose\Headers 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...
31
    public readonly array $headers;
32
33
    /** @var HttpBody  */
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\SemanticLog\Profile\Verbose\HttpBody 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...
34
    public readonly mixed $body;
35
    public readonly string $view;
36
    public readonly Profile $profile;
37
38
    public function __construct(ResourceObject $resource, OpenContext $openContext)
39
    {
40
        // Trigger rendering to get view
41
        $resourceString = (string) $resource;
42
        unset($resourceString);
43
        $this->uri = (string) $resource->uri;
0 ignored issues
show
Bug introduced by
The property uri is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
44
        $this->code = $resource->code;
0 ignored issues
show
Bug introduced by
The property code is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
45
        $this->headers = $resource->headers;
0 ignored issues
show
Bug introduced by
The property headers is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
46
        /** @psalm-suppress MixedAssignment */
47
        /** @phpstan-ignore-next-line */
48
        $this->body = $resource->body;
0 ignored issues
show
Bug introduced by
The property body is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
49
        /** @psalm-suppress PossiblyNullPropertyAssignmentValue */
50
        $this->view = $resource->view ?? '';
0 ignored issues
show
Bug introduced by
The property view is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
51
52
        // Stop profiling and capture final profile data
53
        $xhprofResult = $openContext->profile->xhprof?->stop($this->uri);
54
        $xdebugTrace = $openContext->profile->xdebug?->stop();
55
        $phpProfile = PhpProfile::capture();
56
57
        $this->profile = new Profile(
0 ignored issues
show
Bug introduced by
The property profile is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
58
            xhprof: $xhprofResult,
59
            xdebug: $xdebugTrace,
60
            php: $phpProfile,
61
        );
62
    }
63
64
    public static function create(ResourceObject $resource, OpenContext $openContext): self
65
    {
66
        return new self($resource, $openContext);
67
    }
68
69
    /** @return array<string, mixed> */
70
    #[Override]
71
    public function jsonSerialize(): array
72
    {
73
        return [
74
            'uri' => $this->uri,
75
            'code' => $this->code,
76
            'headers' => $this->headers,
77
            'body' => $this->body,
78
            'view' => $this->view,
79
            'profile' => $this->profile,
80
        ];
81
    }
82
}
83