Skip to content

Commit

Permalink
Refactor: Use fn where possible (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarlepp committed Feb 14, 2020
1 parent 0b0b995 commit 48df51c
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 65 deletions.
12 changes: 5 additions & 7 deletions src/Command/ApiKey/ListApiKeysCommand.php
Expand Up @@ -103,13 +103,11 @@ private function getRows(): array
*/
private function getFormatterApiKey(): Closure
{
$userGroupFormatter = static function (UserGroup $userGroup): string {
return sprintf(
'%s (%s)',
$userGroup->getName(),
$userGroup->getRole()->getId()
);
};
$userGroupFormatter = fn (UserGroup $userGroup): string => sprintf(
'%s (%s)',
$userGroup->getName(),
$userGroup->getRole()->getId()
);

return function (ApiKey $apiToken) use ($userGroupFormatter): array {
return [
Expand Down
14 changes: 6 additions & 8 deletions src/Command/User/ListUserGroupsCommand.php
Expand Up @@ -99,14 +99,12 @@ private function getRows(): array
*/
private function getFormatterUserGroup(): Closure
{
$userFormatter = static function (User $user): string {
return sprintf(
'%s %s <%s>',
$user->getFirstName(),
$user->getLastName(),
$user->getEmail()
);
};
$userFormatter = fn (User $user): string => sprintf(
'%s %s <%s>',
$user->getFirstName(),
$user->getLastName(),
$user->getEmail()
);

return static function (UserGroup $userGroup) use ($userFormatter): array {
return [
Expand Down
12 changes: 5 additions & 7 deletions src/Command/User/ListUsersCommand.php
Expand Up @@ -104,13 +104,11 @@ private function getRows(): array
*/
private function getFormatterUser(): Closure
{
$userGroupFormatter = static function (UserGroup $userGroup): string {
return sprintf(
'%s (%s)',
$userGroup->getName(),
$userGroup->getRole()->getId()
);
};
$userGroupFormatter = fn (UserGroup $userGroup): string => sprintf(
'%s (%s)',
$userGroup->getName(),
$userGroup->getRole()->getId()
);

return function (User $user) use ($userGroupFormatter): array {
return [
Expand Down
4 changes: 1 addition & 3 deletions src/Command/Utils/CheckDependencies.php
Expand Up @@ -131,9 +131,7 @@ private function getNamespaceDirectories(): array
*
* @return string
*/
$closure = static function (SplFileInfo $fileInfo): string {
return $fileInfo->getPath();
};
$closure = fn (SplFileInfo $fileInfo): string => $fileInfo->getPath();

/** @var Traversable $iterator */
$iterator = $finder->getIterator();
Expand Down
10 changes: 2 additions & 8 deletions src/DTO/RestDto.php
Expand Up @@ -86,9 +86,7 @@ public function getId(): ?string
*/
public function getVisited(): array
{
return array_filter($this->visited, static function (string $property) {
return $property !== 'id';
});
return array_filter($this->visited, fn (string $property): bool => $property !== 'id');
}

/**
Expand Down Expand Up @@ -201,11 +199,7 @@ private function getGetterMethod(RestDtoInterface $dto, string $property): ?stri
'has' . ucfirst($property),
];

$filter = static function (string $method) use ($dto): bool {
return method_exists($dto, $method);
};

$getterMethods = array_filter($getters, $filter);
$getterMethods = array_filter($getters, fn (string $method): bool => method_exists($dto, $method));

return $this->validateGetterMethod($property, $getterMethods);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Entity/Traits/LogRequestProcessRequestTrait.php
Expand Up @@ -512,8 +512,7 @@ private function determineParameters(Request $request): array
/** @var array<string, mixed> $output */
$output = JSON::decode($rawContent, true);
} catch (JsonException $error) {
(static function (Throwable $error): void {
})($error);
(fn (Throwable $error): Throwable => $error)($error);

// Oh noes content isn't JSON so just parse it
$output = [];
Expand Down
11 changes: 6 additions & 5 deletions src/Rest/RepositoryHelper.php
Expand Up @@ -368,13 +368,14 @@ private static function getParameters(
try {
$value = array_map([UuidHelper::class, 'getBytes'], $value);
} catch (InvalidUuidStringException $exception) {
(static function (Throwable $exception): void {
})($exception);
(fn (Throwable $exception): Throwable => $exception)($exception);
}

$parameters[] = array_map(static function (string $value) use ($queryBuilder): Literal {
return $queryBuilder->expr()->literal(is_numeric($value) ? (int)$value : $value);
}, $value);
$parameters[] = array_map(
fn (string $value): Literal =>
$queryBuilder->expr()->literal(is_numeric($value) ? (int)$value : $value),
$value
);
}

return $parameters;
Expand Down
11 changes: 1 addition & 10 deletions src/Rest/RequestHandler.php
Expand Up @@ -253,17 +253,8 @@ private static function checkSearchTerms($searchTerms): void
*/
private static function normalizeSearchTerms(array $searchTerms): array
{
/**
* Lambda function to normalize JSON search terms.
*
* @param string|array $terms
*/
$iterator = static function (array &$terms): void {
$terms = array_unique(array_values(array_filter($terms)));
};

// Normalize user input, note that this support array and string formats on value
array_walk($searchTerms, $iterator);
array_walk($searchTerms, fn (array $terms): array => array_unique(array_values(array_filter($terms))));

return $searchTerms;
}
Expand Down
7 changes: 1 addition & 6 deletions src/Rest/ResponseHandler.php
Expand Up @@ -198,12 +198,7 @@ private function checkPopulateAll(
// Set all associations to be populated
if ($populateAll && count($populate) === 0) {
$associations = $restResource->getAssociations();

$iterator = static function (string $assocName) use ($entityName): string {
return $entityName . '.' . $assocName;
};

$populate = array_map($iterator, $associations);
$populate = array_map(fn (string $assocName): string => $entityName . '.' . $assocName, $associations);
}

return $populate;
Expand Down
10 changes: 3 additions & 7 deletions src/Rest/SearchTerm.php
Expand Up @@ -124,13 +124,9 @@ private static function getColumnIterator(string $term, int $mode): Closure
*
* @return string[]
*/
return static function (string $column) use ($term, $mode): array {
if (strpos($column, '.') === false) {
$column = 'entity.' . $column;
}

return [$column, 'like', self::getTerm($mode, $term)];
};
return fn (string $column): array => [
strpos($column, '.') === false ? 'entity.' . $column : $column, 'like', self::getTerm($mode, $term)
];
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Rest/UuidHelper.php
Expand Up @@ -61,8 +61,7 @@ public static function getType(string $value): ?string
$output = UuidBinaryOrderedTimeType::NAME;
} catch (InvalidUuidStringException $exception) {
// ok, so now we know that value isn't uuid
(static function (Throwable $exception): void {
})($exception);
(fn (Throwable $exception): Throwable => $exception)($exception);
}

return $output;
Expand Down

0 comments on commit 48df51c

Please sign in to comment.