Skip to content

Commit f65f3a1

Browse files
committed
refactor(core): updated syntax for php8 and phpunit
Signed-off-by: Fred Myerscough <oniice@gmail.com>
1 parent 30791d7 commit f65f3a1

File tree

6 files changed

+44
-95
lines changed

6 files changed

+44
-95
lines changed

src/Body.php

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ class Body implements Arrayable, Jsonable
3232

3333
/**
3434
* Status code of the response
35-
*
36-
* @var int $status
3735
*/
38-
private $status;
36+
private int $status;
3937

4038
public function __construct($data = [], $meta = [], $messages = [], int $status = 200)
4139
{
@@ -45,39 +43,25 @@ public function __construct($data = [], $meta = [], $messages = [], int $status
4543
$this->setStatus($status);
4644
}
4745

48-
/**
49-
* @return mixed
50-
*/
5146
public function getData(): array
5247
{
5348
return $this->data;
5449
}
5550

56-
/**
57-
* @param mixed $data
58-
*
59-
* @return Body
60-
*/
61-
public function setData($data): Body
51+
52+
public function setData(mixed $data): Body
6253
{
6354
$this->data = $data;
6455

6556
return $this;
6657
}
6758

68-
/**
69-
* @return mixed
70-
*/
7159
public function getMessages(): array
7260
{
7361
return $this->messages;
7462
}
7563

76-
/**
77-
* @param mixed $messages
78-
*
79-
* @return Body
80-
*/
64+
8165
public function setMessages(array $messages): Body
8266
{
8367
$this->messages = $messages;
@@ -87,8 +71,6 @@ public function setMessages(array $messages): Body
8771

8872
/**
8973
* @param string $message
90-
*
91-
* @return Body
9274
*/
9375
public function addMessage($message): Body
9476
{
@@ -97,39 +79,25 @@ public function addMessage($message): Body
9779
return $this;
9880
}
9981

100-
/**
101-
* @return mixed
102-
*/
10382
public function getMeta(): array
10483
{
10584
return $this->meta;
10685
}
10786

108-
/**
109-
* @param mixed $meta
110-
*
111-
* @return Body
112-
*/
113-
public function setMeta($meta): Body
87+
88+
public function setMeta(mixed $meta): Body
11489
{
11590
$this->meta = $meta;
11691

11792
return $this;
11893
}
11994

120-
/**
121-
* @return int
122-
*/
12395
public function getStatus(): int
12496
{
12597
return $this->status;
12698
}
12799

128-
/**
129-
* @param int $status
130-
*
131-
* @return Body
132-
*/
100+
133101
public function setStatus(int $status): Body
134102
{
135103
$this->status = $status;
@@ -141,7 +109,6 @@ public function setStatus(int $status): Body
141109
* Convert the object to its JSON representation.
142110
*
143111
* @param int $options
144-
* @return string
145112
*/
146113
public function toJson($options = 0): string
147114
{
@@ -156,8 +123,6 @@ public function toJson($options = 0): string
156123

157124
/**
158125
* Get the instance as an array.
159-
*
160-
* @return array
161126
*/
162127
public function toArray(): array
163128
{

src/Builder.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public function __construct()
2323

2424
/**
2525
* Reset response body, headers and options
26-
*
27-
* @return Builder
2826
*/
2927
public function fresh(): Builder
3028
{
@@ -46,9 +44,6 @@ public function body()
4644

4745
/**
4846
* Set the api data response
49-
*
50-
* @param array $data
51-
* @return Builder
5247
*/
5348
public function data(array $data): Builder
5449
{
@@ -59,9 +54,6 @@ public function data(array $data): Builder
5954

6055
/**
6156
* Set meta data of current api response
62-
*
63-
* @param array $meta
64-
* @return Builder
6557
*/
6658
public function meta(array $meta): Builder
6759
{
@@ -72,9 +64,6 @@ public function meta(array $meta): Builder
7264

7365
/**
7466
* Add a response message
75-
*
76-
* @param string $messages
77-
* @return Builder
7867
*/
7968
public function message(string $messages): Builder
8069
{
@@ -85,9 +74,6 @@ public function message(string $messages): Builder
8574

8675
/**
8776
* Set collection of response messages
88-
*
89-
* @param array $messages
90-
* @return Builder
9177
*/
9278
public function messages(array $messages): Builder
9379
{
@@ -98,9 +84,6 @@ public function messages(array $messages): Builder
9884

9985
/**
10086
* Set HTTP status of response
101-
*
102-
* @param int $status
103-
* @return Builder
10487
*/
10588
public function status(int $status): Builder
10689
{
@@ -112,11 +95,9 @@ public function status(int $status): Builder
11295
/**
11396
* Set a HTTP header value to be returned with the response
11497
*
115-
* @param string $key
116-
* @param string $value
11798
* @return $this
11899
*/
119-
public function header(string $key, string $value)
100+
public function header(string $key, string $value): static
120101
{
121102
$this->headers[$key] = $value;
122103

@@ -126,10 +107,9 @@ public function header(string $key, string $value)
126107
/**
127108
* Set headers that should be returned with the response
128109
*
129-
* @param array $headers
130110
* @return $this
131111
*/
132-
public function headers(array $headers)
112+
public function headers(array $headers): static
133113
{
134114
$this->headers = array_filter($headers);
135115

src/helpers.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<?php
22

3+
use Myerscode\Laravel\ApiResponse\Builder;
34
if (!function_exists('api')) {
45

56
/**
67
* Start creating a idempotent api response
7-
*
8-
* @return \Myerscode\Laravel\ApiResponse\Builder
98
*/
10-
function api()
9+
function api(): Builder
1110
{
12-
return new \Myerscode\Laravel\ApiResponse\Builder();
11+
return new Builder();
1312
}
1413
}

tests/BodyTest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@
22

33
namespace Tests;
44

5+
use Mockery;
56
use Illuminate\Database\Eloquent\JsonEncodingException;
67
use Myerscode\Laravel\ApiResponse\Body;
8+
use PHPUnit\Framework\Attributes\DataProvider;
79

810
class BodyTest extends TestCase
911
{
1012

11-
public function provider()
13+
public static function provider(): array
1214
{
1315
return [
1416
[200, [], [], []],
1517
];
1618
}
1719

1820
/**
19-
* @dataProvider provider
21+
* @param $status
22+
* @param $data
23+
* @param $messages
24+
* @param $meta
25+
*
26+
* @return void
2027
*/
21-
public function testResponsePropertiesAreSet($status, $data, $messages, $meta)
28+
#[DataProvider('provider')]
29+
public function testResponsePropertiesAreSet($status, $data, $messages, $meta): void
2230
{
2331
$body = new Body();
2432
$body->setStatus(($status))->setData($data)->setMessages($messages)->setMeta($meta);
@@ -29,34 +37,32 @@ public function testResponsePropertiesAreSet($status, $data, $messages, $meta)
2937
$this->assertEquals($meta, $body->getMeta());
3038
}
3139

32-
public function testAddMessageToBody()
40+
public function testAddMessageToBody(): void
3341
{
3442
$body = new Body();
3543
$body->addMessage(('Hello World'));
3644

3745
$this->assertEquals(['Hello World'], $body->getMessages());
3846
}
3947

40-
/**
41-
* @dataProvider provider
42-
*/
43-
public function testBodyConvertedToJson($status, $data, $messages, $meta)
48+
#[DataProvider('provider')]
49+
public function testBodyConvertedToJson($status, $data, $messages, $meta): void
4450
{
4551
$body = new Body();
4652
$body->setStatus(($status))->setData($data)->setMessages($messages)->setMeta($meta);
4753

4854
$this->assertJson($body->toJson());
4955
}
5056

51-
public function testInvalidJsonThrowsError()
57+
public function testInvalidJsonThrowsError(): void
5258
{
5359
$this->expectException(JsonEncodingException::class);
5460

55-
$body = \Mockery::mock(Body::class)->makePartial();
61+
$body = Mockery::mock(Body::class)->makePartial();
5662

5763
$body->shouldReceive('toArray')
5864
->andReturn([NAN]);
5965

6066
$body->toJson();
6167
}
62-
}
68+
}

tests/BuilderTest.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33
namespace Tests;
44

55
use Illuminate\Testing\TestResponse;
6+
use PHPUnit\Framework\Attributes\DataProvider;
67

78
class BuilderTest extends TestCase
89
{
910

10-
public function provider()
11+
public static function provider(): array
1112
{
1213
return [
1314
[200, [], [], []],
1415
];
1516
}
1617

1718

18-
/**
19-
* @dataProvider provider
20-
*/
21-
public function testResponsePropertiesAreSet($status, $data, $messages, $meta)
19+
#[DataProvider('provider')]
20+
public function testResponsePropertiesAreSet($status, $data, $messages, $meta): void
2221
{
2322
$builder = api()->status($status)->data($data)->messages($messages)->meta($meta);
2423

@@ -28,26 +27,26 @@ public function testResponsePropertiesAreSet($status, $data, $messages, $meta)
2827
$this->assertEquals($meta, $builder->body()->getMeta());
2928
}
3029

31-
public function testAddMessageToResponse()
30+
public function testAddMessageToResponse(): void
3231
{
3332
$builder = api()->message('Hello World');
3433

3534
$this->assertEquals(['Hello World'], $builder->body()->getMessages());
3635
}
3736

38-
public function testAddingHeaderToResponse()
37+
public function testAddingHeaderToResponse(): void
3938
{
40-
$response = new TestResponse(api()->header('foo', 'bar')->respond());
39+
$testResponse = new TestResponse(api()->header('foo', 'bar')->respond());
4140

42-
$response->assertHeader('foo');
41+
$testResponse->assertHeader('foo');
4342
}
4443

45-
public function testAddingMultipleHeaderToResponse()
44+
public function testAddingMultipleHeaderToResponse(): void
4645
{
47-
$response = new TestResponse(api()->headers(['foo' => 'bar', 'hello' => 'world', 'invalid header'])->respond());
46+
$testResponse = new TestResponse(api()->headers(['foo' => 'bar', 'hello' => 'world', 'invalid header'])->respond());
4847

49-
$response->assertHeader('foo');
50-
$response->assertHeader('hello');
51-
$response->assertHeaderMissing('invalid header');
48+
$testResponse->assertHeader('foo');
49+
$testResponse->assertHeader('hello');
50+
$testResponse->assertHeaderMissing('invalid header');
5251
}
5352
}

tests/RespondTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
class RespondTest extends TestCase
1010
{
1111

12-
public function testHelperMakesBuilder()
12+
public function testHelperMakesBuilder(): void
1313
{
1414
$this->assertInstanceOf(Builder::class, api());
1515
}
1616

17-
public function testRespondCreatesJsonResponse()
17+
public function testRespondCreatesJsonResponse(): void
1818
{
1919
$this->assertInstanceOf(JsonResponse::class, api()->respond());
2020
$this->assertInstanceOf(JsonResponse::class, api()->toResponse( new Request()));

0 commit comments

Comments
 (0)