From 4168727f36b88a58e217e0ab7f0eb9c87a1dbb29 Mon Sep 17 00:00:00 2001 From: Yassine Guedidi Date: Sat, 5 Apr 2025 13:20:44 +0200 Subject: [PATCH] Modernize to PHP 7.4 --- rector.php | 2 +- src/Command/Import/ImportCommand.php | 4 +--- src/Controller/Api/EntryRestController.php | 4 +--- src/Controller/ConfigController.php | 4 +--- src/Doctrine/WallabagMigration.php | 4 +--- src/ExpressionLanguage/AuthenticatorProvider.php | 4 +--- src/Form/Type/Api/ClientType.php | 8 ++------ src/Helper/DownloadImages.php | 4 +--- src/Import/HtmlImport.php | 12 +++++------- src/Import/PocketHtmlImport.php | 12 +++++------- src/Repository/EntryRepository.php | 4 +--- tests/Controller/Api/TagRestControllerTest.php | 4 +--- tests/Import/ChromeImportTest.php | 4 +--- tests/Import/FirefoxImportTest.php | 4 +--- tests/Import/InstapaperImportTest.php | 4 +--- tests/Import/PocketHtmlImportTest.php | 4 +--- tests/Import/PocketImportTest.php | 8 ++------ tests/Import/ReadabilityImportTest.php | 4 +--- tests/Import/ShaarliImportTest.php | 4 +--- tests/Import/WallabagV1ImportTest.php | 4 +--- tests/Import/WallabagV2ImportTest.php | 4 +--- 21 files changed, 31 insertions(+), 75 deletions(-) diff --git a/rector.php b/rector.php index 831914c78..e6e48dac6 100644 --- a/rector.php +++ b/rector.php @@ -14,5 +14,5 @@ return RectorConfig::configure() ]) ->withImportNames(importShortClasses: false) ->withAttributesSets(doctrine: true) - ->withPhpSets(php73: true) + ->withPhpSets(php74: true) ->withTypeCoverageLevel(0); diff --git a/src/Command/Import/ImportCommand.php b/src/Command/Import/ImportCommand.php index 0005797a5..2d29979ae 100644 --- a/src/Command/Import/ImportCommand.php +++ b/src/Command/Import/ImportCommand.php @@ -107,9 +107,7 @@ class ImportCommand extends Command // Turning off doctrine default logs queries for saving memory $middlewares = $this->entityManager->getConnection()->getConfiguration()->getMiddlewares(); - $middlewaresWithoutLogging = array_filter($middlewares, function (Middleware $middleware) { - return !$middleware instanceof LoggingMiddleware; - }); + $middlewaresWithoutLogging = array_filter($middlewares, fn (Middleware $middleware) => !$middleware instanceof LoggingMiddleware); $this->entityManager->getConnection()->getConfiguration()->setMiddlewares($middlewaresWithoutLogging); if ($input->getOption('useUserId')) { diff --git a/src/Controller/Api/EntryRestController.php b/src/Controller/Api/EntryRestController.php index 15b6d1c0e..5406f5c75 100644 --- a/src/Controller/Api/EntryRestController.php +++ b/src/Controller/Api/EntryRestController.php @@ -132,9 +132,7 @@ class EntryRestController extends WallabagRestController } if (false === $returnId) { - $results = array_map(function ($v) { - return null !== $v; - }, $results); + $results = array_map(fn ($v) => null !== $v, $results); } $results = $this->replaceUrlHashes($results, $urlHashMap); diff --git a/src/Controller/ConfigController.php b/src/Controller/ConfigController.php index 792cc2494..677603818 100644 --- a/src/Controller/ConfigController.php +++ b/src/Controller/ConfigController.php @@ -367,9 +367,7 @@ class ConfigController extends AbstractController $backupCodes = (new BackupCodes())->toArray(); $backupCodesHashed = array_map( - function ($backupCode) { - return password_hash($backupCode, \PASSWORD_DEFAULT); - }, + fn ($backupCode) => password_hash($backupCode, \PASSWORD_DEFAULT), $backupCodes ); diff --git a/src/Doctrine/WallabagMigration.php b/src/Doctrine/WallabagMigration.php index 60d0a026c..4fcb445e6 100644 --- a/src/Doctrine/WallabagMigration.php +++ b/src/Doctrine/WallabagMigration.php @@ -89,9 +89,7 @@ abstract class WallabagMigration extends AbstractMigration */ protected function generateIdentifierName(array $columnNames, string $prefix = ''): string { - $hash = implode('', array_map(static function ($column): string { - return dechex(crc32($column)); - }, $columnNames)); + $hash = implode('', array_map(static fn ($column): string => dechex(crc32($column)), $columnNames)); return strtoupper(substr($prefix . '_' . $hash, 0, $this->platform->getMaxIdentifierLength())); } diff --git a/src/ExpressionLanguage/AuthenticatorProvider.php b/src/ExpressionLanguage/AuthenticatorProvider.php index 72487ad70..3c76b72fc 100644 --- a/src/ExpressionLanguage/AuthenticatorProvider.php +++ b/src/ExpressionLanguage/AuthenticatorProvider.php @@ -34,9 +34,7 @@ class AuthenticatorProvider implements ExpressionFunctionProviderInterface function (): void { throw new \Exception('Not supported'); }, - function (array $arguments, $uri) { - return $this->requestHtmlFunctionClient->request('GET', $uri)->getContent(); - } + fn (array $arguments, $uri) => $this->requestHtmlFunctionClient->request('GET', $uri)->getContent() ); } diff --git a/src/Form/Type/Api/ClientType.php b/src/Form/Type/Api/ClientType.php index 8986a04ff..accb8ee2d 100644 --- a/src/Form/Type/Api/ClientType.php +++ b/src/Form/Type/Api/ClientType.php @@ -28,12 +28,8 @@ class ClientType extends AbstractType $builder->get('redirect_uris') ->addModelTransformer(new CallbackTransformer( - function ($originalUri) { - return $originalUri; - }, - function ($submittedUri) { - return [$submittedUri]; - } + fn ($originalUri) => $originalUri, + fn ($submittedUri) => [$submittedUri] )) ; } diff --git a/src/Helper/DownloadImages.php b/src/Helper/DownloadImages.php index d7423d364..cb808f537 100644 --- a/src/Helper/DownloadImages.php +++ b/src/Helper/DownloadImages.php @@ -294,9 +294,7 @@ class DownloadImages preg_match_all($pattern, $srcsetAttribute, $matches); $srcset = \call_user_func_array('array_merge', $matches); - $srcsetUrls = array_map(function ($src) { - return trim(explode(' ', $src, 2)[0]); - }, $srcset); + $srcsetUrls = array_map(fn ($src) => trim(explode(' ', $src, 2)[0]), $srcset); $urls = array_merge($srcsetUrls, $urls); } diff --git a/src/Import/HtmlImport.php b/src/Import/HtmlImport.php index f54941ec2..607fd5da9 100644 --- a/src/Import/HtmlImport.php +++ b/src/Import/HtmlImport.php @@ -40,13 +40,11 @@ abstract class HtmlImport extends AbstractImport return false; } - $entries = $hrefs->each(function (Crawler $node) { - return [ - 'url' => $node->attr('href'), - 'tags' => $node->attr('tags'), - 'created_at' => $node->attr('add_date'), - ]; - }); + $entries = $hrefs->each(fn (Crawler $node) => [ + 'url' => $node->attr('href'), + 'tags' => $node->attr('tags'), + 'created_at' => $node->attr('add_date'), + ]); if ($this->producer) { $this->parseEntriesForProducer($entries); diff --git a/src/Import/PocketHtmlImport.php b/src/Import/PocketHtmlImport.php index b1c7e3edb..08c8da1e2 100644 --- a/src/Import/PocketHtmlImport.php +++ b/src/Import/PocketHtmlImport.php @@ -56,13 +56,11 @@ class PocketHtmlImport extends HtmlImport return false; } - $entries = $hrefs->each(function (Crawler $node) { - return [ - 'url' => $node->attr('href'), - 'tags' => $node->attr('tags'), - 'created_at' => $node->attr('time_added'), - ]; - }); + $entries = $hrefs->each(fn (Crawler $node) => [ + 'url' => $node->attr('href'), + 'tags' => $node->attr('tags'), + 'created_at' => $node->attr('time_added'), + ]); if ($this->producer) { $this->parseEntriesForProducer($entries); diff --git a/src/Repository/EntryRepository.php b/src/Repository/EntryRepository.php index 83b3c77a4..ded1e2d44 100644 --- a/src/Repository/EntryRepository.php +++ b/src/Repository/EntryRepository.php @@ -289,9 +289,7 @@ class EntryRepository extends ServiceEntityRepository if ('metadata' === $detail) { $fieldNames = $this->getClassMetadata()->getFieldNames(); - $fields = array_filter($fieldNames, function ($k) { - return 'content' !== $k; - }); + $fields = array_filter($fieldNames, fn ($k) => 'content' !== $k); $qb->select(\sprintf('partial e.{%s}', implode(',', $fields))); } diff --git a/tests/Controller/Api/TagRestControllerTest.php b/tests/Controller/Api/TagRestControllerTest.php index 71115f4a3..927a2a236 100644 --- a/tests/Controller/Api/TagRestControllerTest.php +++ b/tests/Controller/Api/TagRestControllerTest.php @@ -23,9 +23,7 @@ class TagRestControllerTest extends WallabagApiTestCase $this->assertArrayHasKey('label', $content[0]); $this->assertArrayHasKey('nbEntries', $content[0]); - $tagLabels = array_map(function ($i) { - return $i['label']; - }, $content); + $tagLabels = array_map(fn ($i) => $i['label'], $content); $this->assertNotContains($this->otherUserTagLabel, $tagLabels, 'There is a possible tag leak'); } diff --git a/tests/Import/ChromeImportTest.php b/tests/Import/ChromeImportTest.php index 35ab0aa67..558bfdec9 100644 --- a/tests/Import/ChromeImportTest.php +++ b/tests/Import/ChromeImportTest.php @@ -95,9 +95,7 @@ class ChromeImportTest extends TestCase $this->em ->expects($this->any()) ->method('persist') - ->with($this->callback(function ($persistedEntry) { - return (bool) $persistedEntry->isArchived(); - })); + ->with($this->callback(fn ($persistedEntry) => (bool) $persistedEntry->isArchived())); $res = $chromeImport->setMarkAsRead(true)->import(); diff --git a/tests/Import/FirefoxImportTest.php b/tests/Import/FirefoxImportTest.php index 7250c412b..5ed6cb550 100644 --- a/tests/Import/FirefoxImportTest.php +++ b/tests/Import/FirefoxImportTest.php @@ -95,9 +95,7 @@ class FirefoxImportTest extends TestCase $this->em ->expects($this->any()) ->method('persist') - ->with($this->callback(function ($persistedEntry) { - return (bool) $persistedEntry->isArchived(); - })); + ->with($this->callback(fn ($persistedEntry) => (bool) $persistedEntry->isArchived())); $res = $firefoxImport->setMarkAsRead(true)->import(); diff --git a/tests/Import/InstapaperImportTest.php b/tests/Import/InstapaperImportTest.php index 17a7b4e21..aaba729e4 100644 --- a/tests/Import/InstapaperImportTest.php +++ b/tests/Import/InstapaperImportTest.php @@ -97,9 +97,7 @@ class InstapaperImportTest extends TestCase $this->em ->expects($this->once()) ->method('persist') - ->with($this->callback(function ($persistedEntry) { - return (bool) $persistedEntry->isArchived(); - })); + ->with($this->callback(fn ($persistedEntry) => (bool) $persistedEntry->isArchived())); $res = $instapaperImport->setMarkAsRead(true)->import(); diff --git a/tests/Import/PocketHtmlImportTest.php b/tests/Import/PocketHtmlImportTest.php index 0f97a50c0..6bea2b98c 100644 --- a/tests/Import/PocketHtmlImportTest.php +++ b/tests/Import/PocketHtmlImportTest.php @@ -95,9 +95,7 @@ class PocketHtmlImportTest extends TestCase $this->em ->expects($this->any()) ->method('persist') - ->with($this->callback(function ($persistedEntry) { - return (bool) $persistedEntry->isArchived(); - })); + ->with($this->callback(fn ($persistedEntry) => (bool) $persistedEntry->isArchived())); $res = $pocketHtmlImport ->setMarkAsRead(true) diff --git a/tests/Import/PocketImportTest.php b/tests/Import/PocketImportTest.php index dc0880360..1b8828382 100644 --- a/tests/Import/PocketImportTest.php +++ b/tests/Import/PocketImportTest.php @@ -207,9 +207,7 @@ JSON $this->em ->expects($this->any()) ->method('persist') - ->with($this->callback(function ($persistedEntry) { - return (bool) $persistedEntry->isArchived() && (bool) $persistedEntry->isStarred(); - })); + ->with($this->callback(fn ($persistedEntry) => (bool) $persistedEntry->isArchived() && (bool) $persistedEntry->isStarred())); $entry = new Entry($this->user); @@ -299,9 +297,7 @@ JSON $this->em ->expects($this->any()) ->method('persist') - ->with($this->callback(function ($persistedEntry) { - return (bool) $persistedEntry->isArchived(); - })); + ->with($this->callback(fn ($persistedEntry) => (bool) $persistedEntry->isArchived())); $entry = new Entry($this->user); diff --git a/tests/Import/ReadabilityImportTest.php b/tests/Import/ReadabilityImportTest.php index 851130bf9..cc7b7d828 100644 --- a/tests/Import/ReadabilityImportTest.php +++ b/tests/Import/ReadabilityImportTest.php @@ -95,9 +95,7 @@ class ReadabilityImportTest extends TestCase $this->em ->expects($this->any()) ->method('persist') - ->with($this->callback(function ($persistedEntry) { - return (bool) $persistedEntry->isArchived(); - })); + ->with($this->callback(fn ($persistedEntry) => (bool) $persistedEntry->isArchived())); $res = $readabilityImport->setMarkAsRead(true)->import(); diff --git a/tests/Import/ShaarliImportTest.php b/tests/Import/ShaarliImportTest.php index f041e3005..7510a28e6 100644 --- a/tests/Import/ShaarliImportTest.php +++ b/tests/Import/ShaarliImportTest.php @@ -95,9 +95,7 @@ class ShaarliImportTest extends TestCase $this->em ->expects($this->any()) ->method('persist') - ->with($this->callback(function ($persistedEntry) { - return (bool) $persistedEntry->isArchived(); - })); + ->with($this->callback(fn ($persistedEntry) => (bool) $persistedEntry->isArchived())); $res = $shaarliImport ->setMarkAsRead(true) diff --git a/tests/Import/WallabagV1ImportTest.php b/tests/Import/WallabagV1ImportTest.php index f51a39edf..7b9cbe558 100644 --- a/tests/Import/WallabagV1ImportTest.php +++ b/tests/Import/WallabagV1ImportTest.php @@ -99,9 +99,7 @@ class WallabagV1ImportTest extends TestCase $this->em ->expects($this->any()) ->method('persist') - ->with($this->callback(function ($persistedEntry) { - return (bool) $persistedEntry->isArchived(); - })); + ->with($this->callback(fn ($persistedEntry) => (bool) $persistedEntry->isArchived())); $res = $wallabagV1Import->setMarkAsRead(true)->import(); diff --git a/tests/Import/WallabagV2ImportTest.php b/tests/Import/WallabagV2ImportTest.php index cfe8e95de..04a772f40 100644 --- a/tests/Import/WallabagV2ImportTest.php +++ b/tests/Import/WallabagV2ImportTest.php @@ -93,9 +93,7 @@ class WallabagV2ImportTest extends TestCase $this->em ->expects($this->any()) ->method('persist') - ->with($this->callback(function ($persistedEntry) { - return (bool) $persistedEntry->isArchived(); - })); + ->with($this->callback(fn ($persistedEntry) => (bool) $persistedEntry->isArchived())); $res = $wallabagV2Import->setMarkAsRead(true)->import();