Skip to content

Commit 38a7fd5

Browse files
committed
Use arrow functions where possible
1 parent d8c9388 commit 38a7fd5

File tree

9 files changed

+18
-63
lines changed

9 files changed

+18
-63
lines changed

src/Collection.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ class Collection extends \Illuminate\Support\Collection implements DataInterface
1616
public function toJsonApiArray(): array
1717
{
1818
return array_map(
19-
function ($value) {
20-
return $value instanceof DataInterface ? $value->toJsonApiArray() : $value;
21-
},
19+
static fn ($value) => $value instanceof DataInterface ? $value->toJsonApiArray() : $value,
2220
$this->items
2321
);
2422
}

src/DocumentFactory.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,8 @@ public function make(DataInterface $data): DocumentInterface
3737
private function getIncluded(DataInterface $data): Collection
3838
{
3939
return Collection::wrap($data)
40-
->flatMap(
41-
function (ItemInterface $item) {
42-
return $this->getIncludedFromItem($item);
43-
}
44-
)
45-
->unique(
46-
static function (ItemInterface $item) {
47-
return sprintf('%s:%s', $item->getType(), $item->getId());
48-
}
49-
)
40+
->flatMap(fn (ItemInterface $item) => $this->getIncludedFromItem($item))
41+
->unique(static fn (ItemInterface $item) => sprintf('%s:%s', $item->getType(), $item->getId()))
5042
->values();
5143
}
5244

@@ -70,16 +62,8 @@ static function ($relationship) {
7062
return Collection::wrap($relationship->getIncluded());
7163
}
7264
)
73-
->flatMap(
74-
function (ItemInterface $item) {
75-
return Collection::wrap($item)->merge($this->getIncludedFromItem($item));
76-
}
77-
)
78-
->filter(
79-
function (ItemInterface $item) {
80-
return $this->itemCanBeIncluded($item);
81-
}
82-
);
65+
->flatMap(fn (ItemInterface $item) => Collection::wrap($item)->merge($this->getIncludedFromItem($item)))
66+
->filter(fn (ItemInterface $item) => $this->itemCanBeIncluded($item));
8367
}
8468

8569
/**

src/Item.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ public function fill(array $attributes)
8383
*/
8484
public function forceFill(array $attributes)
8585
{
86-
return static::unguarded(function () use ($attributes) {
87-
return $this->fill($attributes);
88-
});
86+
return static::unguarded(fn () => $this->fill($attributes));
8987
}
9088

9189
/**
@@ -118,9 +116,10 @@ public static function hydrate(array $items): array
118116
{
119117
$instance = new static();
120118

121-
return array_map(static function ($item) use ($instance) {
122-
return $instance->newInstance($item);
123-
}, $items);
119+
return array_map(
120+
static fn ($item) => $instance->newInstance($item),
121+
$items
122+
);
124123
}
125124

126125
/**

src/Links.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ public function offsetUnset($offset)
118118
public function toArray()
119119
{
120120
return array_map(
121-
static function (?Link $link) {
122-
return $link ? $link->toArray() : null;
123-
},
121+
static fn (?Link $link) => $link ? $link->toArray() : null,
124122
$this->links
125123
);
126124
}

src/Parsers/CollectionParser.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ public function parse($data): Collection
3737
}
3838

3939
return Collection::make($data)
40-
->map(
41-
function ($item) {
42-
return $this->itemParser->parse($item);
43-
}
44-
);
40+
->map(fn ($item) => $this->itemParser->parse($item));
4541
}
4642
}

src/Parsers/DocumentParser.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,7 @@ private function getDocument($data): DocumentInterface
202202
*/
203203
private function linkRelationships(Collection $items): void
204204
{
205-
$keyedItems = $items->keyBy(
206-
function (ItemInterface $item) {
207-
return $this->getItemKey($item);
208-
}
209-
);
205+
$keyedItems = $items->keyBy(fn (ItemInterface $item) => $this->getItemKey($item));
210206

211207
$items->each(
212208
function (ItemInterface $item) use ($keyedItems) {
@@ -232,9 +228,7 @@ function (ItemInterface $item) use ($keyedItems) {
232228
}
233229

234230
$relation->setIncluded(
235-
$relatedCollection->map(function (ItemInterface $relatedItem) use ($keyedItems) {
236-
return $this->getItem($keyedItems, $relatedItem) ?? $relatedItem;
237-
})
231+
$relatedCollection->map(fn (ItemInterface $relatedItem) => $this->getItem($keyedItems, $relatedItem) ?? $relatedItem)
238232
);
239233
}
240234
}
@@ -270,10 +264,6 @@ private function getItemKey(ItemInterface $item): string
270264
*/
271265
private function getDuplicateItems(Collection $items): Collection
272266
{
273-
$valueRetriever = function (ItemInterface $item) {
274-
return $this->getItemKey($item);
275-
};
276-
277-
return $items->duplicates($valueRetriever);
267+
return $items->duplicates(fn (ItemInterface $item) => $this->getItemKey($item));
278268
}
279269
}

src/Parsers/ErrorCollectionParser.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ public function parse($data): ErrorCollection
4141

4242
return new ErrorCollection(
4343
array_map(
44-
function ($error) {
45-
return $this->errorParser->parse($error);
46-
},
44+
fn ($error) => $this->errorParser->parse($error),
4745
$data
4846
)
4947
);

src/Parsers/ItemParser.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,7 @@ private function parseRelationshipData($data): DataInterface
167167
{
168168
if (is_array($data)) {
169169
return Collection::make($data)
170-
->map(
171-
function ($identifier) {
172-
return $this->parseRelationshipData($identifier);
173-
}
174-
);
170+
->map(fn ($identifier) => $this->parseRelationshipData($identifier));
175171
}
176172

177173
if (!is_object($data)) {

src/Parsers/LinksParser.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ public function parse($data, string $source): Links
5757

5858
return new Links(
5959
Collection::wrap((array) $data)
60-
->map(
61-
function ($link, $name) {
62-
return $this->buildLink($link, $name);
63-
}
64-
)
60+
->map(fn ($link, $name) => $this->buildLink($link, $name))
6561
->toArray()
6662
);
6763
}

0 commit comments

Comments
 (0)