From 2e4132c5677ee89c147f38792cfeab97182c915a Mon Sep 17 00:00:00 2001 From: Luc Wollants Date: Fri, 23 Aug 2024 15:23:11 +0200 Subject: [PATCH 1/9] Create `LoggerAwareTrait` with non-nullable `$logger` --- src/LoggerAwareTrait.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/LoggerAwareTrait.php diff --git a/src/LoggerAwareTrait.php b/src/LoggerAwareTrait.php new file mode 100644 index 00000000..7581048a --- /dev/null +++ b/src/LoggerAwareTrait.php @@ -0,0 +1,17 @@ +logger = $logger; + } +} From 9f7bd535f9460246bf96d2041aaa29e71247cd3c Mon Sep 17 00:00:00 2001 From: Luc Wollants Date: Fri, 23 Aug 2024 15:23:51 +0200 Subject: [PATCH 2/9] Use new `LoggerAwareTrait` --- src/AMQP/EventBusForwardingConsumer.php | 2 +- src/Http/Authentication/Auth0/Auth0TokenGenerator.php | 2 +- src/Http/Authentication/AuthenticateRequest.php | 2 +- src/JsonDocument/TransformingJsonDocumentIndexService.php | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/AMQP/EventBusForwardingConsumer.php b/src/AMQP/EventBusForwardingConsumer.php index 92c8bb59..e22429e5 100644 --- a/src/AMQP/EventBusForwardingConsumer.php +++ b/src/AMQP/EventBusForwardingConsumer.php @@ -11,10 +11,10 @@ use Broadway\EventHandling\EventBus; use CultuurNet\UDB3\Search\Deserializer\DeserializerLocatorInterface; use CultuurNet\UDB3\Search\Deserializer\DeserializerNotFoundException; +use CultuurNet\UDB3\Search\LoggerAwareTrait; use PhpAmqpLib\Channel\AMQPChannel; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; -use Psr\Log\LoggerAwareTrait; use Psr\Log\NullLogger; use Ramsey\Uuid\Uuid; use Throwable; diff --git a/src/Http/Authentication/Auth0/Auth0TokenGenerator.php b/src/Http/Authentication/Auth0/Auth0TokenGenerator.php index 921f505b..81f42d2a 100644 --- a/src/Http/Authentication/Auth0/Auth0TokenGenerator.php +++ b/src/Http/Authentication/Auth0/Auth0TokenGenerator.php @@ -7,12 +7,12 @@ use CultuurNet\UDB3\Search\Http\Authentication\Token\Token; use CultuurNet\UDB3\Search\Http\Authentication\Token\TokenGenerator; use CultuurNet\UDB3\Search\Json; +use CultuurNet\UDB3\Search\LoggerAwareTrait; use DateTimeImmutable; use GuzzleHttp\Client; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Psr7\Request; use Psr\Log\LoggerAwareInterface; -use Psr\Log\LoggerAwareTrait; use Psr\Log\NullLogger; final class Auth0TokenGenerator implements TokenGenerator, LoggerAwareInterface diff --git a/src/Http/Authentication/AuthenticateRequest.php b/src/Http/Authentication/AuthenticateRequest.php index ccbaf56a..b84fcdd5 100644 --- a/src/Http/Authentication/AuthenticateRequest.php +++ b/src/Http/Authentication/AuthenticateRequest.php @@ -14,6 +14,7 @@ use CultuurNet\UDB3\Search\Http\Authentication\ApiProblems\RemovedApiKey; use CultuurNet\UDB3\Search\Http\Authentication\Token\ManagementTokenProvider; use CultuurNet\UDB3\Search\Http\DefaultQuery\DefaultQueryRepository; +use CultuurNet\UDB3\Search\LoggerAwareTrait; use Exception; use GuzzleHttp\Exception\ConnectException; use ICultureFeed; @@ -25,7 +26,6 @@ use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Log\LoggerAwareInterface; -use Psr\Log\LoggerAwareTrait; use Psr\Log\NullLogger; final class AuthenticateRequest implements MiddlewareInterface, LoggerAwareInterface diff --git a/src/JsonDocument/TransformingJsonDocumentIndexService.php b/src/JsonDocument/TransformingJsonDocumentIndexService.php index bdded0be..a6d9081d 100644 --- a/src/JsonDocument/TransformingJsonDocumentIndexService.php +++ b/src/JsonDocument/TransformingJsonDocumentIndexService.php @@ -4,10 +4,10 @@ namespace CultuurNet\UDB3\Search\JsonDocument; -use Exception; +use CultuurNet\UDB3\Search\LoggerAwareTrait; use CultuurNet\UDB3\Search\ReadModel\DocumentRepository; +use Exception; use Psr\Log\LoggerAwareInterface; -use Psr\Log\LoggerAwareTrait; use Psr\Log\NullLogger; final class TransformingJsonDocumentIndexService implements From c17c90befc45106ab85aa7bd4241815c77090152 Mon Sep 17 00:00:00 2001 From: Luc Wollants Date: Fri, 23 Aug 2024 15:24:15 +0200 Subject: [PATCH 3/9] Make return of `getApplication` non-nullable --- app/Console/AbstractCommand.php | 22 ++++++++++++++++++++ app/Console/AbstractElasticSearchCommand.php | 6 +----- app/Console/MigrateElasticSearchCommand.php | 3 +-- 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 app/Console/AbstractCommand.php diff --git a/app/Console/AbstractCommand.php b/app/Console/AbstractCommand.php new file mode 100644 index 00000000..2826422d --- /dev/null +++ b/app/Console/AbstractCommand.php @@ -0,0 +1,22 @@ +client = $client; } - - protected function getElasticSearchClient(): Client { return $this->client; } - protected function getLogger(OutputInterface $output): ConsoleLogger { $output = clone $output; diff --git a/app/Console/MigrateElasticSearchCommand.php b/app/Console/MigrateElasticSearchCommand.php index 20675090..71b1872a 100644 --- a/app/Console/MigrateElasticSearchCommand.php +++ b/app/Console/MigrateElasticSearchCommand.php @@ -4,13 +4,12 @@ namespace CultuurNet\UDB3\SearchService\Console; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -final class MigrateElasticSearchCommand extends Command +final class MigrateElasticSearchCommand extends AbstractCommand { /** * @inheritdoc From c4a4ee9dd3236b6434fa1b1fdc6df6dc9d158d13 Mon Sep 17 00:00:00 2001 From: Luc Wollants Date: Fri, 23 Aug 2024 15:25:08 +0200 Subject: [PATCH 4/9] Hint `withLocalTimeRangeFilter` on non-nullable parameters --- src/ElasticSearch/Offer/ElasticSearchOfferQueryBuilder.php | 2 +- src/Offer/OfferQueryBuilderInterface.php | 4 ++-- tests/Http/MockOfferQueryBuilder.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ElasticSearch/Offer/ElasticSearchOfferQueryBuilder.php b/src/ElasticSearch/Offer/ElasticSearchOfferQueryBuilder.php index 5ef3585f..63fa51dc 100644 --- a/src/ElasticSearch/Offer/ElasticSearchOfferQueryBuilder.php +++ b/src/ElasticSearch/Offer/ElasticSearchOfferQueryBuilder.php @@ -151,7 +151,7 @@ public function withDateRangeFilter( return $this->withDateRangeQuery('dateRange', $from, $to); } - public function withLocalTimeRangeFilter(int $localTimeFrom = null, int $localTimeTo = null): self + public function withLocalTimeRangeFilter(int $localTimeFrom, int $localTimeTo): self { $this->guardNaturalIntegerRange('localTime', new Time($localTimeFrom), new Time($localTimeTo)); return $this->withRangeQuery('localTimeRange', $localTimeFrom, $localTimeTo); diff --git a/src/Offer/OfferQueryBuilderInterface.php b/src/Offer/OfferQueryBuilderInterface.php index 9b9b5a3d..c5c68cca 100644 --- a/src/Offer/OfferQueryBuilderInterface.php +++ b/src/Offer/OfferQueryBuilderInterface.php @@ -61,8 +61,8 @@ public function withDateRangeFilter( ): OfferQueryBuilderInterface; public function withLocalTimeRangeFilter( - int $localTimeFrom = null, - int $localTimeTo = null + int $localTimeFrom, + int $localTimeTo ): OfferQueryBuilderInterface; public function withStatusFilter(Status ...$statuses): OfferQueryBuilderInterface; diff --git a/tests/Http/MockOfferQueryBuilder.php b/tests/Http/MockOfferQueryBuilder.php index e6bd8d52..387c4521 100644 --- a/tests/Http/MockOfferQueryBuilder.php +++ b/tests/Http/MockOfferQueryBuilder.php @@ -139,7 +139,7 @@ public function withDateRangeFilter(DateTimeImmutable $from = null, DateTimeImmu return $c; } - public function withLocalTimeRangeFilter(int $localTimeFrom = null, int $localTimeTo = null): self + public function withLocalTimeRangeFilter(int $localTimeFrom, int $localTimeTo): self { $c = clone $this; $c->mockQuery['localTimeRange']['from'] = $localTimeFrom; From 4c5b4be64128f9de35fd3686ab3658b0e8712867 Mon Sep 17 00:00:00 2001 From: Luc Wollants Date: Fri, 23 Aug 2024 15:25:36 +0200 Subject: [PATCH 5/9] Validate keys when present --- src/Http/OrganizerSearchController.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Http/OrganizerSearchController.php b/src/Http/OrganizerSearchController.php index 0058d5ad..71d295f6 100644 --- a/src/Http/OrganizerSearchController.php +++ b/src/Http/OrganizerSearchController.php @@ -67,9 +67,11 @@ public function __construct( public function __invoke(ApiRequestInterface $request): ResponseInterface { - $this->organizerParameterWhiteList->guardAgainstUnsupportedParameters( - $request->getQueryParamsKeys() - ); + $keys = $request->getQueryParamsKeys(); + + if ($keys) { + $this->organizerParameterWhiteList->guardAgainstUnsupportedParameters($keys); + } $start = new Start((int) $request->getQueryParam('start', 0)); $limit = new Limit((int) $request->getQueryParam('limit', 30)); From 66429c46901a97378bb640fd44788495423be487 Mon Sep 17 00:00:00 2001 From: Luc Wollants Date: Fri, 23 Aug 2024 15:25:50 +0200 Subject: [PATCH 6/9] Make non-nullable --- src/ElasticSearch/ElasticSearchPagedResultSetFactory.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ElasticSearch/ElasticSearchPagedResultSetFactory.php b/src/ElasticSearch/ElasticSearchPagedResultSetFactory.php index bd208e98..41bead49 100644 --- a/src/ElasticSearch/ElasticSearchPagedResultSetFactory.php +++ b/src/ElasticSearch/ElasticSearchPagedResultSetFactory.php @@ -16,8 +16,7 @@ final class ElasticSearchPagedResultSetFactory implements ElasticSearchPagedResu { private AggregationTransformerInterface $aggregationTransformer; - private ?ElasticSearchResponseValidatorInterface $responseValidator; - + private ElasticSearchResponseValidatorInterface $responseValidator; public function __construct( AggregationTransformerInterface $aggregationTransformer, From f8b8b2dd8d033f51c45152fc3e0a9c12a3a5eddb Mon Sep 17 00:00:00 2001 From: Luc Wollants Date: Fri, 23 Aug 2024 15:26:59 +0200 Subject: [PATCH 7/9] Handle null type --- tests/Http/Parameters/ArrayParameterBagAdapterTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Http/Parameters/ArrayParameterBagAdapterTest.php b/tests/Http/Parameters/ArrayParameterBagAdapterTest.php index d6a72876..33a41263 100644 --- a/tests/Http/Parameters/ArrayParameterBagAdapterTest.php +++ b/tests/Http/Parameters/ArrayParameterBagAdapterTest.php @@ -514,8 +514,10 @@ private function assertArrayContentsAreEqual(array $expected, array $actual): vo } - private function assertDateTimeEquals(DateTimeImmutable $expected, DateTimeImmutable $actual): void + private function assertDateTimeEquals(DateTimeImmutable $expected, ?DateTimeImmutable $actual): void { + $this->assertNotNull($actual); + $this->assertEquals( $expected->format(DateTime::ATOM), $actual->format(DateTime::ATOM) From 9ef710297fd92d36a1eecb4696eae34616c46329 Mon Sep 17 00:00:00 2001 From: Luc Wollants Date: Fri, 23 Aug 2024 15:27:09 +0200 Subject: [PATCH 8/9] Up Stan to level 8 --- phpstan.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index cc93062f..23b2fa71 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 7 + level: 8 checkMissingIterableValueType: false checkGenericClassInNonGenericObjectType: false paths: From 1f5b0fc90d2a2572475eee8bcd00d4ecc5921d6a Mon Sep 17 00:00:00 2001 From: Luc Wollants Date: Mon, 26 Aug 2024 08:54:49 +0200 Subject: [PATCH 9/9] Make if statement more explicit --- src/Http/OrganizerSearchController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/OrganizerSearchController.php b/src/Http/OrganizerSearchController.php index 71d295f6..f697908a 100644 --- a/src/Http/OrganizerSearchController.php +++ b/src/Http/OrganizerSearchController.php @@ -69,7 +69,7 @@ public function __invoke(ApiRequestInterface $request): ResponseInterface { $keys = $request->getQueryParamsKeys(); - if ($keys) { + if ($keys !== null) { $this->organizerParameterWhiteList->guardAgainstUnsupportedParameters($keys); }