Passed
Pull Request — develop (#83)
by
unknown
12:02
created

Payload::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace InShore\Bookwhen\ValueObjects\Transporter;
6
7
use Http\Discovery\Psr17Factory;
8
use Http\Message\MultipartStream\MultipartStreamBuilder;
0 ignored issues
show
Bug introduced by
The type Http\Message\MultipartSt...\MultipartStreamBuilder 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...
9
use InShore\Bookwhen\Contracts\Request;
0 ignored issues
show
Bug introduced by
The type InShore\Bookwhen\Contracts\Request 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...
10
use InShore\Bookwhen\Enums\Transporter\ContentType;
0 ignored issues
show
Bug introduced by
The type InShore\Bookwhen\Enums\Transporter\ContentType 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 InShore\Bookwhen\Enums\Transporter\Method;
12
use InShore\Bookwhen\ValueObjects\ResourceUri;
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\StreamInterface;
15
16
/**
17
 * @internal
18
 */
19
final class Payload
20
{
21
    /**
22
     * Creates a new Request value object.
23
     *
24
     * @param  array<string, mixed>  $parameters
25
     */
26
    private function __construct(
27
        //private readonly ContentType $contentType,
28
        private readonly Method $method,
29
        private readonly ResourceUri $uri,
30
        private readonly array $parameters = [],
31
    ) {
32
        // ..
33
    }
34
35
    /**
36
     * Creates a new Payload value object from the given parameters.
37
     */
38
    public static function list(string $resource, array $parameters = []): self
39
    {
40
        //var_export($parameters);
41
        //$contentType = ContentType::JSON;
42
        $method = Method::GET;
43
        $uri = ResourceUri::list($resource);
44
45
        return new self($method, $uri, $parameters);
46
    }
47
48
    /**
49
     * Creates a new Payload value object from the given parameters.
50
     */
51
    public static function retrieve(string $resource, string $id, string $suffix = ''): self
52
    {
53
        // $contentType = ContentType::JSON;
54
        $method = Method::GET;
55
        $uri = ResourceUri::retrieve($resource, $id, $suffix);
56
57
        return new self($method, $uri);
58
    }
59
60
    /**
61
     * Creates a new Payload value object from the given parameters.
62
     */
63
    public static function retrieveContent(string $resource, string $id): self
64
    {
65
        $contentType = ContentType::JSON;
66
        $method = Method::GET;
67
        $uri = ResourceUri::retrieveContent($resource, $id);
68
69
        return new self($contentType, $method, $uri);
0 ignored issues
show
Bug introduced by
$method of type InShore\Bookwhen\Enums\Transporter\Method is incompatible with the type InShore\Bookwhen\ValueObjects\ResourceUri expected by parameter $uri of InShore\Bookwhen\ValueOb...\Payload::__construct(). ( Ignorable by Annotation )

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

69
        return new self($contentType, /** @scrutinizer ignore-type */ $method, $uri);
Loading history...
Bug introduced by
$uri of type InShore\Bookwhen\ValueObjects\ResourceUri is incompatible with the type array expected by parameter $parameters of InShore\Bookwhen\ValueOb...\Payload::__construct(). ( Ignorable by Annotation )

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

69
        return new self($contentType, $method, /** @scrutinizer ignore-type */ $uri);
Loading history...
70
    }
71
72
    /**
73
     * Creates a new Payload value object from the given parameters.
74
     *
75
     * @param  array<string, mixed>  $parameters
76
     */
77
    public static function create(string $resource, array $parameters): self
78
    {
79
        $contentType = ContentType::JSON;
80
        $method = Method::POST;
81
        $uri = ResourceUri::create($resource);
82
83
        return new self($contentType, $method, $uri, $parameters);
0 ignored issues
show
Bug introduced by
$method of type InShore\Bookwhen\Enums\Transporter\Method is incompatible with the type InShore\Bookwhen\ValueObjects\ResourceUri expected by parameter $uri of InShore\Bookwhen\ValueOb...\Payload::__construct(). ( Ignorable by Annotation )

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

83
        return new self($contentType, /** @scrutinizer ignore-type */ $method, $uri, $parameters);
Loading history...
Unused Code introduced by
The call to InShore\Bookwhen\ValueOb...\Payload::__construct() has too many arguments starting with $parameters. ( Ignorable by Annotation )

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

83
        return /** @scrutinizer ignore-call */ new self($contentType, $method, $uri, $parameters);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
$uri of type InShore\Bookwhen\ValueObjects\ResourceUri is incompatible with the type array expected by parameter $parameters of InShore\Bookwhen\ValueOb...\Payload::__construct(). ( Ignorable by Annotation )

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

83
        return new self($contentType, $method, /** @scrutinizer ignore-type */ $uri, $parameters);
Loading history...
84
    }
85
86
    /**
87
     * Creates a new Payload value object from the given parameters.
88
     *
89
     * @param  array<string, mixed>  $parameters
90
     */
91
    public static function upload(string $resource, array $parameters): self
92
    {
93
        $contentType = ContentType::MULTIPART;
94
        $method = Method::POST;
95
        $uri = ResourceUri::upload($resource);
96
97
        return new self($contentType, $method, $uri, $parameters);
0 ignored issues
show
Unused Code introduced by
The call to InShore\Bookwhen\ValueOb...\Payload::__construct() has too many arguments starting with $parameters. ( Ignorable by Annotation )

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

97
        return /** @scrutinizer ignore-call */ new self($contentType, $method, $uri, $parameters);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
$uri of type InShore\Bookwhen\ValueObjects\ResourceUri is incompatible with the type array expected by parameter $parameters of InShore\Bookwhen\ValueOb...\Payload::__construct(). ( Ignorable by Annotation )

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

97
        return new self($contentType, $method, /** @scrutinizer ignore-type */ $uri, $parameters);
Loading history...
Bug introduced by
$method of type InShore\Bookwhen\Enums\Transporter\Method is incompatible with the type InShore\Bookwhen\ValueObjects\ResourceUri expected by parameter $uri of InShore\Bookwhen\ValueOb...\Payload::__construct(). ( Ignorable by Annotation )

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

97
        return new self($contentType, /** @scrutinizer ignore-type */ $method, $uri, $parameters);
Loading history...
98
    }
99
100
    /**
101
     * Creates a new Payload value object from the given parameters.
102
     */
103
    public static function cancel(string $resource, string $id): self
104
    {
105
        $contentType = ContentType::JSON;
106
        $method = Method::POST;
107
        $uri = ResourceUri::cancel($resource, $id);
108
109
        return new self($contentType, $method, $uri);
0 ignored issues
show
Bug introduced by
$method of type InShore\Bookwhen\Enums\Transporter\Method is incompatible with the type InShore\Bookwhen\ValueObjects\ResourceUri expected by parameter $uri of InShore\Bookwhen\ValueOb...\Payload::__construct(). ( Ignorable by Annotation )

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

109
        return new self($contentType, /** @scrutinizer ignore-type */ $method, $uri);
Loading history...
Bug introduced by
$uri of type InShore\Bookwhen\ValueObjects\ResourceUri is incompatible with the type array expected by parameter $parameters of InShore\Bookwhen\ValueOb...\Payload::__construct(). ( Ignorable by Annotation )

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

109
        return new self($contentType, $method, /** @scrutinizer ignore-type */ $uri);
Loading history...
110
    }
111
112
    /**
113
     * Creates a new Payload value object from the given parameters.
114
     */
115
    public static function delete(string $resource, string $id): self
116
    {
117
        $contentType = ContentType::JSON;
118
        $method = Method::DELETE;
119
        $uri = ResourceUri::delete($resource, $id);
120
121
        return new self($contentType, $method, $uri);
0 ignored issues
show
Bug introduced by
$method of type InShore\Bookwhen\Enums\Transporter\Method is incompatible with the type InShore\Bookwhen\ValueObjects\ResourceUri expected by parameter $uri of InShore\Bookwhen\ValueOb...\Payload::__construct(). ( Ignorable by Annotation )

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

121
        return new self($contentType, /** @scrutinizer ignore-type */ $method, $uri);
Loading history...
Bug introduced by
$uri of type InShore\Bookwhen\ValueObjects\ResourceUri is incompatible with the type array expected by parameter $parameters of InShore\Bookwhen\ValueOb...\Payload::__construct(). ( Ignorable by Annotation )

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

121
        return new self($contentType, $method, /** @scrutinizer ignore-type */ $uri);
Loading history...
122
    }
123
124
    /**
125
     * Creates a new Psr 7 Request instance.
126
     */
127
    public function toRequest(BaseUri $baseUri, Headers $headers, QueryParams $queryParams): RequestInterface
0 ignored issues
show
Unused Code introduced by
The parameter $queryParams 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

127
    public function toRequest(BaseUri $baseUri, Headers $headers, /** @scrutinizer ignore-unused */ QueryParams $queryParams): RequestInterface

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...
128
    {
129
        $psr17Factory = new Psr17Factory();
130
131
        $body = null;
132
133
        $uri = $baseUri->toString().$this->uri->toString();
134
135
        if (! empty($this->parameters)) {
136
            $uri .= '?'.http_build_query($this->parameters);
137
        }
138
        //die($uri);
139
        $request = $psr17Factory->createRequest($this->method->value, $uri);
140
141
        if ($body !== null) {
0 ignored issues
show
introduced by
The condition $body !== null is always false.
Loading history...
142
            $request = $request->withBody($body);
143
        }
144
145
        foreach ($headers->toArray() as $name => $value) {
146
            $request = $request->withHeader($name, $value);
147
        }
148
        return $request;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request could return the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\RequestInterface. Consider adding an additional type-check to rule them out.
Loading history...
149
    }
150
}
151