Skip to content

Commit 32c7bdc

Browse files
committed
chore(app): Style, Rector, and Stan changes
1 parent 12349a0 commit 32c7bdc

File tree

19 files changed

+152
-65
lines changed

19 files changed

+152
-65
lines changed

app/Transformers/Customer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ class Customer extends BaseTransformer
99
/**
1010
* Transform the resource into an array.
1111
*
12-
* @param mixed $resource
13-
*
1412
* @return array<string, mixed>
1513
*/
1614
public function toArray(mixed $resource): array

loader.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# total 5 errors
2+
3+
includes:
4+
- missingType.iterableValue.neon

system/API/ApiException.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
namespace CodeIgniter\API;
1515

16+
use Exception;
17+
1618
/**
1719
* Custom exception for API-related errors.
1820
*/
19-
class ApiException extends \Exception
21+
class ApiException extends Exception
2022
{
2123
/**
2224
* Thrown when the fields requested in a URL are not valid.
23-
*
24-
* @return ApiException
2525
*/
2626
public static function forInvalidFields(string $field): self
2727
{
@@ -30,8 +30,6 @@ public static function forInvalidFields(string $field): self
3030

3131
/**
3232
* Thrown when the includes requested in a URL are not valid.
33-
*
34-
* @return ApiException
3533
*/
3634
public static function forInvalidIncludes(string $include): self
3735
{
@@ -41,8 +39,6 @@ public static function forInvalidIncludes(string $include): self
4139
/**
4240
* Thrown when an include is requested, but the method to handle it
4341
* does not exist on the model.
44-
*
45-
* @return ApiException
4642
*/
4743
public static function forMissingInclude(string $include): self
4844
{
@@ -51,8 +47,6 @@ public static function forMissingInclude(string $include): self
5147

5248
/**
5349
* Thrown when a transformer class cannot be found.
54-
*
55-
* @return ApiException
5650
*/
5751
public static function forTransformerNotFound(string $transformerClass): self
5852
{
@@ -61,8 +55,6 @@ public static function forTransformerNotFound(string $transformerClass): self
6155

6256
/**
6357
* Thrown when a transformer class does not implement TransformerInterface.
64-
*
65-
* @return ApiException
6658
*/
6759
public static function forInvalidTransformer(string $transformerClass): self
6860
{

system/API/BaseTransformer.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,29 @@
5656
*/
5757
abstract class BaseTransformer implements TransformerInterface
5858
{
59+
/**
60+
* @var list<string>|null
61+
*/
5962
private ?array $fields = null;
63+
64+
/**
65+
* @var list<string>|null
66+
*/
6067
private ?array $includes = null;
68+
6169
protected mixed $resource = null;
6270

6371
public function __construct(
6472
private ?IncomingRequest $request = null,
65-
)
66-
{
73+
) {
6774
$this->request = $request ?? request();
6875

69-
$fields = $this->request->getGet('fields');
76+
$fields = $this->request->getGet('fields');
7077
$this->fields = is_string($fields)
7178
? array_map('trim', explode(',', $fields))
7279
: $fields;
7380

74-
$includes = $this->request->getGet('include');
81+
$includes = $this->request->getGet('include');
7582
$this->includes = is_string($includes)
7683
? array_map('trim', explode(',', $includes))
7784
: $includes;
@@ -116,14 +123,15 @@ public function transform(mixed $resource = null): array
116123
*/
117124
public function transformMany(array $resources): array
118125
{
119-
return array_map(fn($resource) => $this->transform($resource), $resources);
126+
return array_map(fn ($resource): array => $this->transform($resource), $resources);
120127
}
121128

122129
/**
123130
* Conditionally include a value.
124131
*
125132
* @param mixed $value
126133
* @param mixed $default
134+
*
127135
* @return mixed
128136
*/
129137
protected function when(bool $condition, $value, $default = null)
@@ -133,16 +141,23 @@ protected function when(bool $condition, $value, $default = null)
133141

134142
/**
135143
* Conditionally exclude a value.
144+
*
145+
* @param mixed $value
146+
* @param mixed|null $default
147+
*
148+
* @return mixed
136149
*/
137150
protected function whenNot(bool $condition, $value, $default = null)
138151
{
139-
return ! $condition ? $value : $default;
152+
return $condition ? $default : $value;
140153
}
141154

142155
/**
143156
* Define which fields can be requested via the 'fields' query parameter.
144157
* Override in child classes to restrict available fields.
145158
* Return null to allow all fields from toArray().
159+
*
160+
* @return list<string>|null
146161
*/
147162
protected function getAllowedFields(): ?array
148163
{
@@ -154,6 +169,8 @@ protected function getAllowedFields(): ?array
154169
* Override in child classes to restrict available includes.
155170
* Return null to allow all includes that have corresponding methods.
156171
* Return an empty array to disable all includes.
172+
*
173+
* @return list<string>|null
157174
*/
158175
protected function getAllowedIncludes(): ?array
159176
{
@@ -163,6 +180,10 @@ protected function getAllowedIncludes(): ?array
163180
/**
164181
* Limits the given data array to only the fields specified
165182
*
183+
* @param array<string, mixed> $data
184+
*
185+
* @return array<string, mixed>
186+
*
166187
* @throws InvalidArgumentException
167188
*/
168189
private function limitFields(array $data): array
@@ -188,6 +209,10 @@ private function limitFields(array $data): array
188209
/**
189210
* Checks the request for 'include' query variable, and if present,
190211
* calls the corresponding include{Resource} methods to add related data.
212+
*
213+
* @param array<string, mixed> $data
214+
*
215+
* @return array<string, mixed>
191216
*/
192217
private function insertIncludes(array $data): array
193218
{
@@ -201,7 +226,7 @@ private function insertIncludes(array $data): array
201226
return $data; // No includes allowed
202227
}
203228

204-
// If whitelist is defined, filter the requested includes
229+
// If whitelist is defined, filter the requested includes
205230
if ($allowedIncludes !== null) {
206231
$invalidIncludes = array_diff($this->includes, $allowedIncludes);
207232

@@ -213,7 +238,7 @@ private function insertIncludes(array $data): array
213238
foreach ($this->includes as $include) {
214239
$method = 'include' . ucfirst($include);
215240
if (method_exists($this, $method)) {
216-
$data[$include] = $this->$method();
241+
$data[$include] = $this->{$method}();
217242
} else {
218243
throw ApiException::forMissingInclude($include);
219244
}

system/API/TransformerInterface.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
declare(strict_types=1);
44

5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
514
namespace CodeIgniter\API;
615

716
/**
@@ -17,16 +26,26 @@ interface TransformerInterface
1726
* This is overridden by child classes to define specific fields.
1827
*
1928
* @param mixed $resource The resource being transformed
29+
*
30+
* @return array<string, mixed>
2031
*/
2132
public function toArray(mixed $resource): array;
2233

2334
/**
2435
* Transforms the given resource into an array.
36+
*
37+
* @param array<string, mixed>|object $resource
38+
*
39+
* @return array<string, mixed>
2540
*/
26-
public function transform(object|array $resource): array;
41+
public function transform(array|object $resource): array;
2742

2843
/**
2944
* Transforms a collection of resources using $this->transform() on each item.
45+
*
46+
* @param array<int|string, mixed> $resources
47+
*
48+
* @return array<int, array<string, mixed>>
3049
*/
3150
public function transformMany(array $resources): array;
3251
}

system/Language/en/Api.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
// API language settings
1515
return [
16-
'invalidFields' => 'Invalid field requested: {0}',
17-
'invalidIncludes' => 'Invalid include requested: {0}',
18-
'missingInclude' => 'Missing include method for: {0}',
19-
'transformerNotFound' => 'Transformer class \'{0}\' not found.',
20-
'invalidTransformer' => 'Transformer class \'{0}\' must implement TransformerInterface.',
16+
'invalidFields' => 'Invalid field requested: {0}',
17+
'invalidIncludes' => 'Invalid include requested: {0}',
18+
'missingInclude' => 'Missing include method for: {0}',
19+
'transformerNotFound' => 'Transformer class \'{0}\' not found.',
20+
'invalidTransformer' => 'Transformer class \'{0}\' must implement TransformerInterface.',
2121
];

tests/_support/API/TestTransformer.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ class TestTransformer extends BaseTransformer
2323
/**
2424
* Transform the resource into an array.
2525
*
26-
* @param mixed $resource
27-
*
2826
* @return array<string, mixed>
2927
*/
3028
public function toArray(mixed $resource): array
3129
{
3230
return [
33-
'id' => $resource['id'] ?? null,
34-
'name' => $resource['name'] ?? null,
35-
'transformed' => true,
36-
'name_upper' => isset($resource['name']) ? strtoupper($resource['name']) : null,
31+
'id' => $resource['id'] ?? null,
32+
'name' => $resource['name'] ?? null,
33+
'transformed' => true,
34+
'name_upper' => isset($resource['name']) ? strtoupper($resource['name']) : null,
3735
];
3836
}
3937
}

tests/system/API/ResponseTraitTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
use Exception;
3737
use PHPUnit\Framework\Attributes\Group;
3838
use stdClass;
39+
use Tests\Support\API\InvalidTransformer;
40+
use Tests\Support\API\TestTransformer;
3941

4042
/**
4143
* @internal
@@ -1065,7 +1067,7 @@ public function testPaginateWithTransformer(): void
10651067

10661068
$controller = $this->makeController('/api/items');
10671069

1068-
$this->invoke($controller, 'paginate', [$model, 20, \Tests\Support\API\TestTransformer::class]);
1070+
$this->invoke($controller, 'paginate', [$model, 20, TestTransformer::class]);
10691071

10701072
$responseBody = json_decode($this->response->getBody(), true);
10711073

@@ -1114,7 +1116,7 @@ public function testPaginateWithTransformerAndQueryBuilder(): void
11141116

11151117
$controller = $this->makeController('/api/items');
11161118

1117-
$this->invoke($controller, 'paginate', [$builder, 20, \Tests\Support\API\TestTransformer::class]);
1119+
$this->invoke($controller, 'paginate', [$builder, 20, TestTransformer::class]);
11181120

11191121
$responseBody = json_decode($this->response->getBody(), true);
11201122

@@ -1152,9 +1154,9 @@ public function testPaginateWithInvalidTransformer(): void
11521154
$controller = $this->makeController('/api/items');
11531155

11541156
$this->expectException(ApiException::class);
1155-
$this->expectExceptionMessage(lang('Api.invalidTransformer', [\Tests\Support\API\InvalidTransformer::class]));
1157+
$this->expectExceptionMessage(lang('Api.invalidTransformer', [InvalidTransformer::class]));
11561158

1157-
$this->invoke($controller, 'paginate', [$model, 20, \Tests\Support\API\InvalidTransformer::class]);
1159+
$this->invoke($controller, 'paginate', [$model, 20, InvalidTransformer::class]);
11581160
}
11591161

11601162
public function testPaginateWithTransformerPreservesMetaAndLinks(): void
@@ -1169,7 +1171,7 @@ public function testPaginateWithTransformerPreservesMetaAndLinks(): void
11691171

11701172
$controller = $this->makeController('/api/items');
11711173

1172-
$this->invoke($controller, 'paginate', [$model, 2, \Tests\Support\API\TestTransformer::class]);
1174+
$this->invoke($controller, 'paginate', [$model, 2, TestTransformer::class]);
11731175

11741176
$responseBody = json_decode($this->response->getBody(), true);
11751177

@@ -1199,7 +1201,7 @@ public function testPaginateWithTransformerEmptyData(): void
11991201

12001202
$controller = $this->makeController('/api/items');
12011203

1202-
$this->invoke($controller, 'paginate', [$model, 20, \Tests\Support\API\TestTransformer::class]);
1204+
$this->invoke($controller, 'paginate', [$model, 20, TestTransformer::class]);
12031205

12041206
$responseBody = json_decode($this->response->getBody(), true);
12051207

0 commit comments

Comments
 (0)