Replaced regular callbacks by arrow functions when possible

This commit is contained in:
Alejandro Celaya 2019-12-29 23:16:55 +01:00
parent a830420d75
commit bfeb915cd2
19 changed files with 42 additions and 75 deletions

View File

@ -15,6 +15,4 @@ $em = $container->get(EntityManager::class);
$testHelper->createTestDb();
ApiTest\ApiTestCase::setApiClient($container->get('shlink_test_api_client'));
ApiTest\ApiTestCase::setSeedFixturesCallback(function () use ($testHelper, $em, $config) {
$testHelper->seedFixtures($em, $config['data_fixtures'] ?? []);
});
ApiTest\ApiTestCase::setSeedFixturesCallback(fn () => $testHelper->seedFixtures($em, $config['data_fixtures'] ?? []));

View File

@ -19,9 +19,7 @@ $swooleTestingPort = 9999;
$buildDbConnection = function (): array {
$driver = env('DB_DRIVER', 'sqlite');
$isCi = env('TRAVIS', false);
$getMysqlHost = function (string $driver) {
return sprintf('shlink_db%s', $driver === 'mysql' ? '' : '_maria');
};
$getMysqlHost = fn (string $driver) => sprintf('shlink_db%s', $driver === 'mysql' ? '' : '_maria');
$driverConfigMap = [
'sqlite' => [

View File

@ -46,8 +46,6 @@ class ListTagsCommand extends Command
return [['No tags yet']];
}
return map($tags, function (Tag $tag) {
return [(string) $tag];
});
return map($tags, fn (Tag $tag) => [(string) $tag]);
}
}

View File

@ -42,9 +42,9 @@ class ListShortUrlsCommandTest extends TestCase
$data[] = new ShortUrl('url_' . $i);
}
$this->shortUrlService->listShortUrls(Argument::cetera())->will(function () use (&$data) {
return new Paginator(new ArrayAdapter($data));
})->shouldBeCalledTimes(3);
$this->shortUrlService->listShortUrls(Argument::cetera())
->will(fn () => new Paginator(new ArrayAdapter($data)))
->shouldBeCalledTimes(3);
$this->commandTester->setInputs(['y', 'y', 'n']);
$this->commandTester->execute([]);

View File

@ -50,9 +50,7 @@ class GeolocationDbUpdaterTest extends TestCase
/** @test */
public function exceptionIsThrownWhenOlderDbDoesNotExistAndDownloadFails(): void
{
$mustBeUpdated = function () {
$this->assertTrue(true);
};
$mustBeUpdated = fn () => $this->assertTrue(true);
$prev = new RuntimeException('');
$fileExists = $this->dbUpdater->databaseFileExists()->willReturn(false);
@ -148,8 +146,6 @@ class GeolocationDbUpdaterTest extends TestCase
public function provideSmallDays(): iterable
{
return map(range(0, 34), function (int $days) {
return [$days];
});
return map(range(0, 34), fn (int $days) => [$days]);
}
}

View File

@ -75,9 +75,10 @@ class SimplifiedConfigParser
// This mainly allows deprecating keys and defining new ones that will replace the older and always take
// preference, while the old one keeps working for backwards compatibility if the new one is not provided.
$simplifiedConfigOrder = array_flip(array_keys(self::SIMPLIFIED_CONFIG_MAPPING));
uksort($configForExistingKeys, function (string $a, string $b) use ($simplifiedConfigOrder): int {
return $simplifiedConfigOrder[$a] - $simplifiedConfigOrder[$b];
});
uksort(
$configForExistingKeys,
fn (string $a, string $b): int => $simplifiedConfigOrder[$a] - $simplifiedConfigOrder[$b]
);
return $configForExistingKeys;
}

View File

@ -188,9 +188,7 @@ class ShortUrl extends AbstractEntity
$shortUrlTags = invoke($this->getTags(), '__toString');
$hasAllTags = count($shortUrlTags) === count($tags) && array_reduce(
$tags,
function (bool $hasAllTags, string $tag) use ($shortUrlTags) {
return $hasAllTags && contains($shortUrlTags, $tag);
},
fn (bool $hasAllTags, string $tag) => $hasAllTags && contains($shortUrlTags, $tag),
true
);

View File

@ -69,12 +69,10 @@ class ValidationException extends InvalidArgumentException implements ProblemDet
private function invalidElementsToString(): string
{
return reduce_left($this->getInvalidElements(), function ($messageSet, string $name, $_, string $acc) {
return $acc . sprintf(
"\n '%s' => %s",
$name,
is_array($messageSet) ? print_r($messageSet, true) : $messageSet
);
}, '');
return reduce_left($this->getInvalidElements(), fn ($messages, string $name, $_, string $acc) => $acc . sprintf(
"\n '%s' => %s",
$name,
is_array($messages) ? print_r($messages, true) : $messages
), '');
}
}

View File

@ -66,9 +66,7 @@ class VisitRepositoryTest extends DatabaseTestCase
public function provideBlockSize(): iterable
{
return map(range(1, 5), function (int $value) {
return [$value];
});
return map(range(1, 5), fn (int $value) => [$value]);
}
/** @test */

View File

@ -28,9 +28,9 @@ class DeleteShortUrlServiceTest extends TestCase
public function setUp(): void
{
$shortUrl = (new ShortUrl(''))->setVisits(new ArrayCollection(map(range(0, 10), function () {
return new Visit(new ShortUrl(''), Visitor::emptyInstance());
})));
$shortUrl = (new ShortUrl(''))->setVisits(
new ArrayCollection(map(range(0, 10), fn () => new Visit(new ShortUrl(''), Visitor::emptyInstance())))
);
$this->shortCode = $shortUrl->getShortCode();
$this->em = $this->prophesize(EntityManagerInterface::class);

View File

@ -44,7 +44,7 @@ class UrlShortenerTest extends TestCase
$this->em->beginTransaction()->willReturn(null);
$this->em->persist(Argument::any())->will(function ($arguments) {
/** @var ShortUrl $shortUrl */
$shortUrl = $arguments[0];
[$shortUrl] = $arguments;
$shortUrl->setId('10');
});
$repo = $this->prophesize(ShortUrlRepository::class);
@ -240,9 +240,7 @@ class UrlShortenerTest extends TestCase
'validUntil' => Chronos::parse('2017-01-01'),
'maxVisits' => 4,
]);
$tagsCollection = new ArrayCollection(array_map(function (string $tag) {
return new Tag($tag);
}, $tags));
$tagsCollection = new ArrayCollection(array_map(fn (string $tag) => new Tag($tag), $tags));
$expected = (new ShortUrl($url, $meta))->setTags($tagsCollection);
$repo = $this->prophesize(ShortUrlRepository::class);

View File

@ -40,9 +40,10 @@ class VisitServiceTest extends TestCase
/** @test */
public function locateVisitsIteratesAndLocatesUnlocatedVisits(): void
{
$unlocatedVisits = map(range(1, 200), function (int $i) {
return new Visit(new ShortUrl(sprintf('short_code_%s', $i)), Visitor::emptyInstance());
});
$unlocatedVisits = map(
range(1, 200),
fn (int $i) => new Visit(new ShortUrl(sprintf('short_code_%s', $i)), Visitor::emptyInstance())
);
$repo = $this->prophesize(VisitRepository::class);
$findUnlocatedVisits = $repo->findUnlocatedVisits(false)->willReturn($unlocatedVisits);
@ -55,9 +56,7 @@ class VisitServiceTest extends TestCase
$clear = $this->em->clear()->will(function () {
});
$this->visitService->locateUnlocatedVisits(function () {
return Location::emptyInstance();
}, function () {
$this->visitService->locateUnlocatedVisits(fn () => Location::emptyInstance(), function () {
$args = func_get_args();
$this->assertInstanceOf(VisitLocation::class, array_shift($args));

View File

@ -43,10 +43,7 @@ class VisitsTrackerTest extends TestCase
$repo->findOneBy(['shortCode' => $shortCode])->willReturn(new ShortUrl(''));
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledOnce();
$this->em->persist(Argument::that(function (Visit $visit) {
$visit->setId('1');
return $visit;
}))->shouldBeCalledOnce();
$this->em->persist(Argument::that(fn (Visit $visit) => $visit->setId('1')))->shouldBeCalledOnce();
$this->em->flush()->shouldBeCalledOnce();
$this->visitsTracker->track($shortCode, Visitor::emptyInstance());

View File

@ -43,9 +43,7 @@ class RequestToHttpAuthPlugin implements RequestToHttpAuthPluginInterface
{
return array_reduce(
self::SUPPORTED_AUTH_HEADERS,
function (bool $carry, string $header) use ($request) {
return $carry || $request->hasHeader($header);
},
fn (bool $carry, string $header) => $carry || $request->hasHeader($header),
false
);
}

View File

@ -11,8 +11,6 @@ class EmptyResponseImplicitOptionsMiddlewareFactory
{
public function __invoke()
{
return new ImplicitOptionsMiddleware(function () {
return new EmptyResponse();
});
return new ImplicitOptionsMiddleware(fn () => new EmptyResponse());
}
}

View File

@ -91,9 +91,7 @@ class CreateShortUrlActionTest extends ApiTestCase
public function provideMaxVisits(): array
{
return map(range(10, 15), function (int $i) {
return [$i];
});
return map(range(10, 15), fn (int $i) => [$i]);
}
/** @test */

View File

@ -34,9 +34,7 @@ class AuthenticationPluginManagerFactoryTest extends TestCase
private function getPlugins(AuthenticationPluginManager $pluginManager): array
{
return (function () {
return $this->services;
})->call($pluginManager);
return (fn () => $this->services)->call($pluginManager);
}
public function provideConfigs(): iterable

View File

@ -28,15 +28,13 @@ class ConfigProviderTest extends TestCase
/** @test */
public function routesAreProperlyPrefixed(): void
{
$configProvider = new ConfigProvider(function () {
return [
'routes' => [
['path' => '/foo'],
['path' => '/bar'],
['path' => '/baz/foo'],
],
];
});
$configProvider = new ConfigProvider(fn () => [
'routes' => [
['path' => '/foo'],
['path' => '/bar'],
['path' => '/baz/foo'],
],
]);
$config = $configProvider();

View File

@ -109,8 +109,6 @@ class AuthenticationMiddlewareTest extends TestCase
private function getDummyMiddleware(): MiddlewareInterface
{
return middleware(function () {
return new Response\EmptyResponse();
});
return middleware(fn () => new Response\EmptyResponse());
}
}