Skip to content

Commit 441427b

Browse files
committed
Refactoring
1 parent bc64683 commit 441427b

File tree

4 files changed

+269
-97
lines changed

4 files changed

+269
-97
lines changed

composer.json

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "fast-forward/http-message",
3+
"description": "Fast Forward PSR-7 HTTP Message utility classes",
4+
"license": "MIT",
5+
"type": "library",
6+
"authors": [
7+
{
8+
"name": "Felipe Sayão Lobato Abreu",
9+
"email": "github@mentordosnerds.com"
10+
}
11+
],
12+
"homepage": "https://github.com/php-fast-forward",
13+
"support": {
14+
"issues": "https://github.com/php-fast-forward/http-message/issues",
15+
"source": "https://github.com/php-fast-forward/http-message"
16+
},
17+
"require": {
18+
"php": "^8.2",
19+
"nyholm/psr7": "^1.8",
20+
"psr/http-message": "^2.0"
21+
},
22+
"require-dev": {
23+
"coisa/php-cs-fixer": "^2.1",
24+
"phpspec/prophecy-phpunit": "^2.3",
25+
"phpunit/phpunit": "^9.6 || ^10.5 || ^11.5"
26+
},
27+
"minimum-stability": "stable",
28+
"autoload": {
29+
"psr-4": {
30+
"FastForward\\Http\\Message\\": "src/"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"FastForward\\Http\\Message\\Tests\\": "tests/"
36+
}
37+
},
38+
"config": {
39+
"sort-packages": true
40+
},
41+
"extra": {
42+
"branch-alias": {
43+
"dev-main": "1.x-dev"
44+
}
45+
},
46+
"scripts": {
47+
"cs-check": "php-cs-fixer fix --dry-run --diff",
48+
"cs-fix": "php-cs-fixer fix",
49+
"mutation-testing": "infection --threads=4",
50+
"pre-commit": [
51+
"@cs-check",
52+
"@static-analysis",
53+
"@tests"
54+
],
55+
"static-analysis": "phpstan analyse --level 5 src",
56+
"tests": "phpunit --testdox"
57+
}
58+
}

src/JsonResponse.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,21 @@ final class JsonResponse extends Response implements JsonResponseInterface
1111
{
1212
public function __construct(
1313
mixed $payload = [],
14-
StatusCode $status = StatusCode::OK,
14+
string $charset = 'utf-8'
1515
) {
1616
parent::__construct(
17-
status: $status->value,
18-
headers: ['Content-Type' => 'application/json; charset=utf-8'],
19-
body: $this->createBody($payload),
20-
reason: $status->reasonPhrase(),
17+
headers: ['Content-Type' => 'application/json; charset=' . $charset],
18+
body: new JsonStream($payload),
2119
);
2220
}
2321

24-
private function createBody(mixed $payload): JsonStreamInterface
25-
{
26-
return new JsonStream($payload);
27-
}
28-
2922
public function getPayload(): mixed
3023
{
3124
return $this->getBody()->getPayload();
3225
}
3326

3427
public function withPayload(mixed $payload): self
3528
{
36-
return $this->withBody($this->createBody($payload));
29+
return $this->withBody($this->getBody()->withPayload($payload));
3730
}
3831
}

src/RequestMethod.php

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,63 @@
1616
*/
1717
enum RequestMethod: string
1818
{
19-
/** @var string The HEAD method requests the headers for a given resource without the response body. */
20-
case HEAD = 'HEAD';
19+
/** The HEAD method requests the headers for a given resource without the response body. */
20+
case Head = 'HEAD';
2121

22-
/** @var string The GET method requests a representation of the specified resource. It MUST NOT have side-effects. */
23-
case GET = 'GET';
22+
/** The GET method requests a representation of the specified resource. It MUST NOT have side-effects. */
23+
case Get = 'GET';
2424

25-
/** @var string The POST method submits data to be processed, often causing a change in state or side-effects. */
26-
case POST = 'POST';
25+
/** The POST method submits data to be processed, often causing a change in state or side-effects. */
26+
case Post = 'POST';
2727

28-
/** @var string The PUT method replaces the target resource with the request payload. */
29-
case PUT = 'PUT';
28+
/** The PUT method replaces the target resource with the request payload. */
29+
case Put = 'PUT';
3030

31-
/** @var string The PATCH method applies partial modifications to the target resource. */
32-
case PATCH = 'PATCH';
31+
/** The PATCH method applies partial modifications to the target resource. */
32+
case Patch = 'PATCH';
3333

34-
/** @var string The DELETE method removes the specified resource. */
35-
case DELETE = 'DELETE';
34+
/** The DELETE method removes the specified resource. */
35+
case Delete = 'DELETE';
3636

37-
/** @var string The PURGE method requests that a cached resource be removed, often used with proxy servers. */
38-
case PURGE = 'PURGE';
37+
/** The PURGE method requests that a cached resource be removed, often used with proxy servers. */
38+
case Purge = 'PURGE';
3939

40-
/** @var string The OPTIONS method describes the communication options for the target resource. */
41-
case OPTIONS = 'OPTIONS';
40+
/** The OPTIONS method describes the communication options for the target resource. */
41+
case Options = 'OPTIONS';
4242

43-
/** @var string The TRACE method performs a message loop-back test along the path to the target resource. */
44-
case TRACE = 'TRACE';
43+
/** The TRACE method performs a message loop-back test along the path to the target resource. */
44+
case Trace = 'TRACE';
4545

46-
/** @var string The CONNECT method establishes a tunnel to the target resource, often used with HTTPS proxies. */
47-
case CONNECT = 'CONNECT';
46+
/** The CONNECT method establishes a tunnel to the target resource, often used with HTTPS proxies. */
47+
case Connect = 'CONNECT';
48+
49+
/**
50+
* Returns true if the method is considered safe (does not modify server state).
51+
*
52+
* @return bool
53+
*/
54+
public function isSafe(): bool
55+
{
56+
return in_array($this, [self::Get, self::Head, self::Options, self::Trace], true);
57+
}
58+
59+
/**
60+
* Returns true if the method is idempotent (multiple identical requests have the same effect as a single one).
61+
*
62+
* @return bool
63+
*/
64+
public function isIdempotent(): bool
65+
{
66+
return in_array($this, [self::Get, self::Head, self::Put, self::Delete, self::Options, self::Trace], true);
67+
}
68+
69+
/**
70+
* Returns true if the method is considered cacheable by default.
71+
*
72+
* @return bool
73+
*/
74+
public function isCacheable(): bool
75+
{
76+
return in_array($this, [self::Get, self::Head], true);
77+
}
4878
}

0 commit comments

Comments
 (0)