Skip to content

Commit 8c79d89

Browse files
authored
chore(laravel): code cleanup (#6526)
* chore(laravel): code cleanup * fix ci * fix phpdoc * fix review * fix review
1 parent a6f3553 commit 8c79d89

31 files changed

+128
-95
lines changed

features/http_cache/tag_collector_service.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Feature: Cache invalidation through HTTP Cache tags (custom TagCollector service
1717
Then the response status code should be 201
1818
And the header "Cache-Tags" should not exist
1919

20-
Scenario: TagCollector can identify $object (IRI is overriden with custom logic)
20+
Scenario: TagCollector can identify $object (IRI is overridden with custom logic)
2121
When I send a "GET" request to "/relation_embedders/1"
2222
Then the response status code should be 200
2323
And the header "Cache-Tags" should be equal to "/RE/1#anotherRelated,/RE/1#related,/RE/1"
@@ -126,7 +126,7 @@ Feature: Cache invalidation through HTTP Cache tags (custom TagCollector service
126126
Then the response status code should be 201
127127
And the header "Cache-Tags" should not exist
128128

129-
Scenario: TagCollector can read propertyMetadata (tag is overriden with data from extraProperties)
129+
Scenario: TagCollector can read propertyMetadata (tag is overridden with data from extraProperties)
130130
When I send a "GET" request to "/extra_properties_on_properties/1"
131131
Then the response status code should be 200
132132
And the header "Cache-Tags" should be equal to "/extra_properties_on_properties/1#overrideRelationTag,/extra_properties_on_properties/1"

src/Laravel/ApiResource/Error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __construct(
6868
private readonly string $detail,
6969
#[ApiProperty(identifier: true)] private int $status,
7070
private readonly array $originalTrace,
71-
private ?string $instance = null,
71+
private readonly ?string $instance = null,
7272
private string $type = 'about:blank',
7373
private array $headers = []
7474
) {

src/Laravel/Controller/ApiPlatformController.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ public function __construct(
4242
public function __invoke(Request $request): Response
4343
{
4444
$operation = $request->attributes->get('_api_operation');
45-
4645
if (!$operation) {
4746
throw new \RuntimeException('Operation not found.');
4847
}
4948

49+
if (!$operation instanceof HttpOperation) {
50+
throw new \LogicException('Operation is not an HttpOperation.');
51+
}
52+
5053
$uriVariables = $this->getUriVariables($request, $operation);
5154
// at some point we could introduce that back
5255
// if ($this->uriVariablesConverter) {
@@ -64,12 +67,12 @@ public function __invoke(Request $request): Response
6467
$operation = $operation->withValidate(!$request->isMethodSafe() && !$request->isMethod('DELETE'));
6568
}
6669

67-
if (null === $operation->canRead() && $operation instanceof HttpOperation) {
68-
$operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
70+
if (null === $operation->canRead()) {
71+
$operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe()); // @phpstan-ignore-line
6972
}
7073

71-
if (null === $operation->canDeserialize() && $operation instanceof HttpOperation) {
72-
$operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true));
74+
if (null === $operation->canDeserialize()) {
75+
$operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true)); // @phpstan-ignore-line
7376
}
7477

7578
$body = $this->provider->provide($operation, $uriVariables, $context);

src/Laravel/Eloquent/Metadata/Factory/Property/EloquentAttributePropertyMetadataFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121
final class EloquentAttributePropertyMetadataFactory implements PropertyMetadataFactoryInterface
2222
{
23-
public function __construct(private readonly ?PropertyMetadataFactoryInterface $decorated = null)
24-
{
23+
public function __construct(
24+
private readonly ?PropertyMetadataFactoryInterface $decorated = null,
25+
) {
2526
}
2627

2728
public function create(string $resourceClass, string $property, array $options = []): ApiProperty

src/Laravel/Eloquent/Metadata/Factory/Property/EloquentAttributePropertyNameCollectionFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919

2020
final class EloquentAttributePropertyNameCollectionFactory implements PropertyNameCollectionFactoryInterface
2121
{
22-
public function __construct(private readonly ?PropertyNameCollectionFactoryInterface $decorated = null)
23-
{
22+
public function __construct(
23+
private readonly ?PropertyNameCollectionFactoryInterface $decorated = null,
24+
) {
2425
}
2526

2627
public function create(string $resourceClass, array $options = []): PropertyNameCollection

src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
use Symfony\Component\PropertyInfo\Type;
2323

2424
/**
25-
* Use Doctrine metadata to populate the identifier property.
25+
* Uses Eloquent metadata to populate the identifier property.
2626
*
2727
* @author Kévin Dunglas <[email protected]>
2828
*/
2929
final class EloquentPropertyMetadataFactory implements PropertyMetadataFactoryInterface
3030
{
31-
public function __construct(private ModelMetadata $modelMetadata, private readonly ?PropertyMetadataFactoryInterface $decorated = null)
32-
{
31+
public function __construct(
32+
private readonly ModelMetadata $modelMetadata,
33+
private readonly ?PropertyMetadataFactoryInterface $decorated = null
34+
) {
3335
}
3436

3537
/**

src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyNameCollectionMetadataFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121

2222
final class EloquentPropertyNameCollectionMetadataFactory implements PropertyNameCollectionFactoryInterface
2323
{
24-
public function __construct(private ModelMetadata $modelMetadata, private PropertyNameCollectionFactoryInterface $decorated, private ResourceClassResolverInterface $resourceClassResolver)
25-
{
24+
public function __construct(
25+
private readonly ModelMetadata $modelMetadata,
26+
private readonly PropertyNameCollectionFactoryInterface $decorated,
27+
private readonly ResourceClassResolverInterface $resourceClassResolver,
28+
) {
2629
}
2730

2831
/**

src/Laravel/Eloquent/Metadata/Factory/Resource/EloquentResourceCollectionMetadataFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function create(string $resourceClass): ResourceMetadataCollection
4848
return $resourceMetadataCollection;
4949
}
5050

51-
foreach ($resourceMetadataCollection as $i => $resourceMetatada) {
52-
$operations = $resourceMetatada->getOperations();
51+
foreach ($resourceMetadataCollection as $i => $resourceMetadata) {
52+
$operations = $resourceMetadata->getOperations();
5353
foreach ($operations as $operationName => $operation) {
5454
if (!$operation->getProvider()) {
5555
$operation = $operation->withProvider($operation instanceof CollectionOperationInterface ? CollectionProvider::class : ItemProvider::class);
@@ -62,7 +62,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
6262
$operations->add($operationName, $operation);
6363
}
6464

65-
$resourceMetadataCollection[$i] = $resourceMetatada->withOperations($operations);
65+
$resourceMetadataCollection[$i] = $resourceMetadata->withOperations($operations);
6666
}
6767

6868
return $resourceMetadataCollection;

src/Laravel/Eloquent/Metadata/IdentifiersExtractor.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222

2323
final class IdentifiersExtractor implements IdentifiersExtractorInterface
2424
{
25-
public function __construct(private readonly IdentifiersExtractorInterface $inner)
26-
{
25+
public function __construct(
26+
private readonly IdentifiersExtractorInterface $inner,
27+
) {
2728
}
2829

2930
public function getIdentifiersFromItem(object $item, ?Operation $operation = null, array $context = []): array

src/Laravel/Eloquent/Metadata/ModelMetadata.php

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,21 @@ final class ModelMetadata
4646
];
4747

4848
/**
49-
* Get the first policy associated with this model.
50-
*
51-
* @param Model $model
52-
*
53-
* @return string
49+
* Gets the first policy associated with this model.
5450
*/
55-
public function getPolicy($model)
51+
public function getPolicy(Model $model): ?string
5652
{
5753
$policy = Gate::getPolicyFor($model::class);
5854

5955
return $policy ? $policy::class : null;
6056
}
6157

6258
/**
63-
* Get the column attributes for the given model.
59+
* Gets the column attributes for the given model.
6460
*
65-
* @param Model $model
66-
*
67-
* @return \Illuminate\Support\Collection<string, mixed>
61+
* @return Collection<string, mixed>
6862
*/
69-
public function getAttributes($model): Collection
63+
public function getAttributes(Model $model): Collection
7064
{
7165
$connection = $model->getConnection();
7266
$schema = $connection->getSchemaBuilder();
@@ -110,9 +104,9 @@ private function isColumnPrimaryKey(array $indexes, string $column): bool
110104
*
111105
* @param array<string, mixed> $columns
112106
*
113-
* @return \Illuminate\Support\Collection<int, mixed>
107+
* @return Collection<int, mixed>
114108
*/
115-
public function getVirtualAttributes(Model $model, $columns): Collection
109+
public function getVirtualAttributes(Model $model, array $columns): Collection
116110
{
117111
$class = new \ReflectionClass($model);
118112

@@ -149,9 +143,9 @@ public function getVirtualAttributes(Model $model, $columns): Collection
149143
}
150144

151145
/**
152-
* Get the relations from the given model.
146+
* Gets the relations from the given model.
153147
*
154-
* @return \Illuminate\Support\Collection<int, mixed>
148+
* @return Collection<int, mixed>
155149
*/
156150
public function getRelations(Model $model): Collection
157151
{
@@ -206,9 +200,9 @@ public function getRelations(Model $model): Collection
206200
}
207201

208202
/**
209-
* Get the Events that the model dispatches.
203+
* Gets the Events that the model dispatches.
210204
*
211-
* @return \Illuminate\Support\Collection<int, mixed>
205+
* @return Collection<int, mixed>
212206
*/
213207
public function getEvents(Model $model): Collection
214208
{
@@ -220,7 +214,7 @@ public function getEvents(Model $model): Collection
220214
}
221215

222216
/**
223-
* Get the cast type for the given column.
217+
* Gets the cast type for the given column.
224218
*/
225219
private function getCastType(string $column, Model $model): ?string
226220
{
@@ -236,9 +230,9 @@ private function getCastType(string $column, Model $model): ?string
236230
}
237231

238232
/**
239-
* Get the model casts, including any date casts.
233+
* Gets the model casts, including any date casts.
240234
*
241-
* @return \Illuminate\Support\Collection<string, mixed>
235+
* @return Collection<string, mixed>
242236
*/
243237
private function getCastsWithDates(Model $model): Collection
244238
{
@@ -250,11 +244,9 @@ private function getCastsWithDates(Model $model): Collection
250244
}
251245

252246
/**
253-
* Get the default value for the given column.
247+
* Gets the default value for the given column.
254248
*
255249
* @param array<string, mixed>&array{name: string, default: string} $column
256-
*
257-
* @return mixed|null
258250
*/
259251
private function getColumnDefault(array $column, Model $model): mixed
260252
{
@@ -268,7 +260,7 @@ private function getColumnDefault(array $column, Model $model): mixed
268260
}
269261

270262
/**
271-
* Determine if the given attribute is hidden.
263+
* Determines if the given attribute is hidden.
272264
*/
273265
private function attributeIsHidden(string $attribute, Model $model): bool
274266
{
@@ -284,7 +276,7 @@ private function attributeIsHidden(string $attribute, Model $model): bool
284276
}
285277

286278
/**
287-
* Determine if the given attribute is unique.
279+
* Determines if the given attribute is unique.
288280
*
289281
* @param array<int, array{columns: string[], unique: bool}> $indexes
290282
*/

0 commit comments

Comments
 (0)