Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor code using arrow functions #3917

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions extensions/likes/src/Api/LoadLikesRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public static function countRelation(AbstractSerializeController $controller, mi
if ($data instanceof Discussion) {
// We do this because the ShowDiscussionController manipulates the posts
// in a way that some of them are just ids.
$loadable = $data->posts->filter(function ($post) {
return $post instanceof Post;
});
$loadable = $data->posts->filter(fn ($post) => $post instanceof Post);
} elseif ($data instanceof Collection) {
$loadable = $data;
} elseif ($data instanceof Post) {
Expand Down
8 changes: 2 additions & 6 deletions extensions/mentions/src/Api/LoadMentionedByRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public static function countRelation(AbstractSerializeController $controller, mi
if ($data instanceof Discussion) {
// We do this because the ShowDiscussionController manipulates the posts
// in a way that some of them are just ids.
$loadable = $data->posts->filter(function ($post) {
return $post instanceof Post;
});
$loadable = $data->posts->filter(fn ($post) => $post instanceof Post);

// firstPost and lastPost might have been included in the API response,
// so we have to make sure counts are also loaded for them.
Expand All @@ -71,9 +69,7 @@ public static function countRelation(AbstractSerializeController $controller, mi

if ($loadable) {
$loadable->loadCount([
'mentionedBy' => function ($query) use ($actor) {
return $query->whereVisibleTo($actor);
}
'mentionedBy' => fn ($query) => $query->whereVisibleTo($actor)
]);
}

Expand Down
12 changes: 3 additions & 9 deletions extensions/mentions/src/Job/SendMentionsNotificationsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ protected function notifyAboutUserMentions(Post $post, array $mentioned): void
$users = User::whereIn('id', $mentioned)
->with('groups')
->get()
->filter(function ($user) use ($post) {
return $post->isVisibleTo($user) && $user->id !== $post->user_id;
})
->filter(fn ($user) => $post->isVisibleTo($user) && $user->id !== $post->user_id)
->all();

$this->notifications->sync(new UserMentionedBlueprint($post), $users);
Expand All @@ -58,9 +56,7 @@ protected function notifyAboutPostMentions(Post $reply, array $mentioned): void
->whereIn('id', $mentioned)
->with('user.groups')
->get()
->filter(function (Post $post) use ($reply) {
return $post->user && $post->user_id !== $reply->user_id && $reply->isVisibleTo($post->user);
})
->filter(fn (Post $post) => $post->user && $post->user_id !== $reply->user_id && $reply->isVisibleTo($post->user))
->all();

foreach ($posts as $post) {
Expand All @@ -75,9 +71,7 @@ protected function notifyAboutGroupMentions(Post $post, array $mentioned): void
})
->with('groups')
->get()
->filter(function (User $user) use ($post) {
return $post->isVisibleTo($user) && $user->id !== $post->user_id;
})
->filter(fn (User $user) => $post->isVisibleTo($user) && $user->id !== $post->user_id)
->all();

$this->notifications->sync(new GroupMentionedBlueprint($post), $users);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ public function handle(CheckForUpdates $command): array
if (isset($mainPackageUpdate['latest-status']) && $mainPackageUpdate['latest-status'] === 'update-possible') {
$mainPackageUpdate['latest-major'] = $mainPackageUpdate['latest'];

$minorPackageUpdate = array_filter($secondOutput['installed'], function ($package) use ($mainPackageUpdate) {
return $package['name'] === $mainPackageUpdate['name'];
})[0] ?? null;
$minorPackageUpdate = array_filter(
$secondOutput['installed'],
fn ($package) => $package['name'] === $mainPackageUpdate['name']
)[0] ?? null;

if ($minorPackageUpdate) {
$mainPackageUpdate['latest-minor'] = $minorPackageUpdate['latest'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public function handle(FlarumUpdated|Updated $event): void
);

if ($event instanceof FlarumUpdated) {
$mapPackageName = function (array $package) {
return $package['name'];
};
$mapPackageName = fn (array $package) => $package['name'];

$previousPackages = array_map($mapPackageName, $previousUpdateCheck['updates']['installed']);
$lastPackages = array_map($mapPackageName, $lastUpdateCheck['updates']['installed']);
Expand Down
7 changes: 4 additions & 3 deletions extensions/package-manager/src/Settings/LastUpdateCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ public static function default(): array

public function getNewMajorVersion(): ?string
{
$core = Arr::first(Arr::get($this->get(), 'updates.installed', []), function ($package) {
return $package['name'] === 'flarum/core';
});
$core = Arr::first(
Arr::get($this->get(), 'updates.installed', []),
fn ($package) => $package['name'] === 'flarum/core'
);

return $core ? $core['latest-major'] : null;
}
Expand Down
18 changes: 10 additions & 8 deletions extensions/statistics/src/Api/Controller/ShowStatisticsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,20 @@ private function getResponse(?string $model, ?string $period, ?array $customDate

private function getLifetimeStatistics(): array
{
return $this->cache->remember('flarum-subscriptions.lifetime_stats', self::$lifetimeStatsCacheTtl, function () {
return array_map(function ($entity) {
return $entity[0]->count();
}, $this->entities);
});
return $this->cache->remember(
'flarum-subscriptions.lifetime_stats',
self::$lifetimeStatsCacheTtl,
fn () => array_map(fn ($entity) => $entity[0]->count(), $this->entities)
);
}

private function getTimedStatistics(string $model): array
{
return $this->cache->remember("flarum-subscriptions.timed_stats.$model", self::$lifetimeStatsCacheTtl, function () use ($model) {
return $this->getTimedCounts($this->entities[$model][0], $this->entities[$model][1]);
});
return $this->cache->remember(
"flarum-subscriptions.timed_stats.$model",
self::$lifetimeStatsCacheTtl,
fn () => $this->getTimedCounts($this->entities[$model][0], $this->entities[$model][1])
);
}

private function getTimedCounts(Builder $query, string $column, ?DateTime $startDate = null, ?DateTime $endDate = null): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,12 @@ public function __invoke(User $actor, Builder $query, string $ability): void
// not just one.
$query->where(function ($query) use ($actor, $permission) {
$query
->whereNotIn('discussions.id', function ($query) use ($actor, $permission) {
return $query->select('discussion_id')
->from('discussion_tag')
->whereNotIn('tag_id', function ($query) use ($actor, $permission) {
Tag::query()->setQuery($query->from('tags'))->whereHasPermission($actor, $permission)->select('tags.id');
});
})
->orWhere(function ($query) use ($actor, $permission) {
// Allow extensions a way to override scoping for any given permission.
$query->whereVisibleTo($actor, "{$permission}InRestrictedTags");
});
->whereNotIn('discussions.id', fn ($query) => $query->select('discussion_id')
->from('discussion_tag')
->whereNotIn('tag_id', function ($query) use ($actor, $permission) {
Tag::query()->setQuery($query->from('tags'))->whereHasPermission($actor, $permission)->select('tags.id');
}))
->orWhere(fn ($query) => $query->whereVisibleTo($actor, "{$permission}InRestrictedTags"));
});

// Hide discussions with no tags if the user doesn't have that global
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public function __invoke(FilterState $filter, QueryCriteria $queryCriteria): voi
return;
}

$filter->getQuery()->whereNotIn('discussions.id', function ($query) {
return $query->select('discussion_id')
->from('discussion_tag')
->whereIn('tag_id', Tag::where('is_hidden', 1)->pluck('id'));
});
$filter->getQuery()->whereNotIn(
'discussions.id',
fn ($query) => $query->select('discussion_id')
->from('discussion_tag')
->whereIn('tag_id', Tag::where('is_hidden', 1)->pluck('id'))
);
}
}
4 changes: 1 addition & 3 deletions extensions/tags/src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ public function scopeWhereHasPermission(Builder $query, User $user, string $curr
$allPermissions = $user->getPermissions();

$tagIdsWithPermission = collect($allPermissions)
->filter(function ($permission) use ($currPermission) {
return str_starts_with($permission, 'tag') && str_contains($permission, $currPermission);
})
->filter(fn ($permission) => str_starts_with($permission, 'tag') && str_contains($permission, $currPermission))
->map(function ($permission) {
$scopeFragment = explode('.', $permission, 2)[0];

Expand Down
7 changes: 4 additions & 3 deletions framework/core/src/Admin/Content/AdminPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public function __invoke(Document $document, Request $request): void
$document->payload['extensions'] = $this->extensions->getExtensions()->toArray();

$document->payload['displayNameDrivers'] = array_keys($this->container->make('flarum.user.display_name.supported_drivers'));
$document->payload['slugDrivers'] = array_map(function ($resourceDrivers) {
return array_keys($resourceDrivers);
}, $this->container->make('flarum.http.slugDrivers'));
$document->payload['slugDrivers'] = array_map(
fn ($resourceDrivers) => array_keys($resourceDrivers),
$this->container->make('flarum.http.slugDrivers')
);

$document->payload['phpVersion'] = $this->appInfo->identifyPHPVersion();
$document->payload['mysqlVersion'] = $this->appInfo->identifyDatabaseVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ protected function loadRelations(Collection $models, array $relations, ServerReq
}

if (! empty($addedRelations)) {
usort($addedRelations, function ($a, $b) {
return substr_count($a, '.') - substr_count($b, '.');
});
usort($addedRelations, fn ($a, $b) => substr_count($a, '.') - substr_count($b, '.'));

foreach ($addedRelations as $relation) {
if (str_contains($relation, '.')) {
Expand Down
10 changes: 4 additions & 6 deletions framework/core/src/Api/Controller/SetPermissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface

Permission::where('permission', $permission)->delete();

Permission::insert(array_map(function ($groupId) use ($permission) {
return [
'permission' => $permission,
'group_id' => $groupId
];
}, $groupIds));
Permission::insert(array_map(fn ($groupId) => [
'permission' => $permission,
'group_id' => $groupId
], $groupIds));

return new EmptyResponse(204);
}
Expand Down
22 changes: 13 additions & 9 deletions framework/core/src/Api/Controller/ShowDiscussionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ protected function data(ServerRequestInterface $request, Document $document): Di
$this->includePosts($discussion, $request, $postRelationships);
}

$this->loadRelations(new Collection([$discussion]), array_filter($include, function ($relationship) {
return ! Str::startsWith($relationship, 'posts');
}), $request);
$this->loadRelations(
new Collection([$discussion]),
array_filter($include, fn ($relationship) => ! Str::startsWith($relationship, 'posts')),
$request
);

return $discussion;
}
Expand Down Expand Up @@ -166,13 +168,15 @@ protected function getRelationCallablesToLoad(Collection $models): array

$postCallableRelationships = $this->getPostRelationships(array_keys($addedCallableRelations));

$relationCallables = array_intersect_key($addedCallableRelations, array_flip(array_map(function ($relation) {
return "posts.$relation";
}, $postCallableRelationships)));
$relationCallables = array_intersect_key(
$addedCallableRelations,
array_flip(array_map(fn ($relation) => "posts.$relation", $postCallableRelationships))
);

// remove posts. prefix from keys
return array_combine(array_map(function ($relation) {
return substr($relation, 6);
}, array_keys($relationCallables)), array_values($relationCallables));
return array_combine(
array_map(fn ($relation) => substr($relation, 6), array_keys($relationCallables)),
array_values($relationCallables)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ protected function data(ServerRequestInterface $request, Document $document): ar
{
RequestUtil::getActor($request)->assertAdmin();

$drivers = array_map(function ($driver) {
return self::$container->make($driver);
}, self::$container->make('mail.supported_drivers'));
$drivers = array_map(
fn ($driver) => self::$container->make($driver),
self::$container->make('mail.supported_drivers')
);

$settings = self::$container->make(SettingsRepositoryInterface::class);
$configured = self::$container->make('flarum.mail.configured_driver');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ protected function fromUser(Notification $notification): ?Relationship

protected function subject(Notification $notification): ?Relationship
{
return $this->hasOne($notification, function (Notification $notification) {
return static::$subjectSerializers[$notification->type];
});
return $this->hasOne(
$notification,
fn (Notification $notification) => static::$subjectSerializers[$notification->type]
);
}

public static function setSubjectSerializer(string $type, string $serializer): void
Expand Down
4 changes: 1 addition & 3 deletions framework/core/src/Database/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ public function __construct(array $attributes = [])
$this->attributes = array_merge($this->attributes, Arr::get(static::$defaults, $class, []));
}

$this->attributes = array_map(function ($item) {
return is_callable($item) ? $item($this) : $item;
}, $this->attributes);
$this->attributes = array_map(fn ($item) => is_callable($item) ? $item($this) : $item, $this->attributes);

parent::__construct($attributes);
}
Expand Down
7 changes: 4 additions & 3 deletions framework/core/src/Database/Console/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ protected function fire(): int

public function upgrade(): void
{
$this->container->bind(Builder::class, function ($container) {
return $container->make(ConnectionInterface::class)->getSchemaBuilder();
});
$this->container->bind(
Builder::class,
fn ($container) => $container->make(ConnectionInterface::class)->getSchemaBuilder()
);

$migrator = $this->container->make(Migrator::class);
$migrator->setOutput($this->output);
Expand Down
4 changes: 1 addition & 3 deletions framework/core/src/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public function loadAggregate($relations, $column, $function = null): self
return $this;
}

return $this->first()->withTableAlias(function () use ($relations, $column, $function) {
return parent::loadAggregate($relations, $column, $function);
});
return $this->first()->withTableAlias(fn () => parent::loadAggregate($relations, $column, $function));
}
}
4 changes: 1 addition & 3 deletions framework/core/src/Database/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ public function getMigrationFiles($path)
return [];
}

$files = array_map(function ($file) {
return str_replace('.php', '', basename($file));
}, $files);
$files = array_map(fn ($file) => str_replace('.php', '', basename($file)), $files);

// Once we have all of the formatted file names we will sort them and since
// they all start with a timestamp this should give us the migrations in
Expand Down
8 changes: 2 additions & 6 deletions framework/core/src/Extend/ApiSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ public function attributes(callable|string $callback): self
*/
public function hasOne(string $name, string $serializerClass): self
{
return $this->relationship($name, function (AbstractSerializer $serializer, $model) use ($serializerClass, $name) {
return $serializer->hasOne($model, $serializerClass, $name);
});
return $this->relationship($name, fn (AbstractSerializer $serializer, $model) => $serializer->hasOne($model, $serializerClass, $name));
}

/**
Expand All @@ -108,9 +106,7 @@ public function hasOne(string $name, string $serializerClass): self
*/
public function hasMany(string $name, string $serializerClass): self
{
return $this->relationship($name, function (AbstractSerializer $serializer, $model) use ($serializerClass, $name) {
return $serializer->hasMany($model, $serializerClass, $name);
});
return $this->relationship($name, fn (AbstractSerializer $serializer, $model) => $serializer->hasMany($model, $serializerClass, $name));
}

/**
Expand Down
8 changes: 2 additions & 6 deletions framework/core/src/Extend/Conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ class Conditional implements ExtenderInterface
*/
public function whenExtensionEnabled(string $extensionId, callable|string $extenders): self
{
return $this->when(function (ExtensionManager $extensions) use ($extensionId) {
return $extensions->isEnabled($extensionId);
}, $extenders);
return $this->when(fn (ExtensionManager $extensions) => $extensions->isEnabled($extensionId), $extenders);
}

/**
Expand All @@ -56,9 +54,7 @@ public function whenExtensionEnabled(string $extensionId, callable|string $exten
*/
public function whenExtensionDisabled(string $extensionId, callable|string $extenders): self
{
return $this->when(function (ExtensionManager $extensions) use ($extensionId) {
return ! $extensions->isEnabled($extensionId);
}, $extenders);
return $this->when(fn (ExtensionManager $extensions) => ! $extensions->isEnabled($extensionId), $extenders);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions framework/core/src/Extend/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public function schedule(string $command, callable|string $callback, array $args

public function extend(Container $container, Extension $extension = null): void
{
$container->extend('flarum.console.commands', function ($existingCommands) {
return array_merge($existingCommands, $this->addCommands);
});
$container->extend(
'flarum.console.commands',
fn ($existingCommands) => array_merge($existingCommands, $this->addCommands)
);

$container->extend('flarum.console.scheduled', function ($existingScheduled) use ($container) {
foreach ($this->scheduled as &$schedule) {
Expand Down
Loading