From f29f1659f37d54e73081fb1889945947f31c1ca6 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 16 Jun 2023 11:36:14 +0200 Subject: [PATCH] feat(qa): Add missing @throws annotation (#171) Cf https://github.com/jolicode/elastically/pull/160#issuecomment-1593688166 Refs: #160 --- phpstan-baseline.neon | 10 ++++++++ phpstan.neon | 7 ++++++ .../ElasticallyExtension.php | 3 +++ src/Client.php | 7 ++++++ src/Index.php | 6 +++++ src/IndexBuilder.php | 19 ++++++++++++++ src/IndexNameMapper.php | 7 ++++++ src/Indexer.php | 25 +++++++++++++++++++ src/Mapping/MappingProviderInterface.php | 5 ++++ src/Mapping/YamlProvider.php | 4 +++ src/Messenger/IndexationRequestHandler.php | 9 +++++++ .../IndexationRequestSpoolSubscriber.php | 7 ++++++ src/ResultSetBuilder.php | 22 ++++++++++++++++ src/Transport/HttpClientTransport.php | 11 ++++++++ 14 files changed, 142 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c94f6e3..7ce80e3 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -14,3 +14,13 @@ parameters: message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#" count: 1 path: src/Bridge/Symfony/DependencyInjection/Configuration.php + + - # We need https://github.com/ruflin/Elastica/pull/2176 + message: "#^Method JoliCode\\\\Elastically\\\\IndexBuilder\\:\\:slowDownRefresh\\(\\) has Elastica\\\\Exception\\\\ExceptionInterface in PHPDoc @throws tag but it's not thrown\\.$#" + count: 1 + path: src/IndexBuilder.php + + - # We need https://github.com/ruflin/Elastica/pull/2176 + message: "#^Method JoliCode\\\\Elastically\\\\IndexBuilder\\:\\:speedUpRefresh\\(\\) has Elastica\\\\Exception\\\\ExceptionInterface in PHPDoc @throws tag but it's not thrown\\.$#" + count: 1 + path: src/IndexBuilder.php diff --git a/phpstan.neon b/phpstan.neon index bc0f2c3..76b314e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,3 +5,10 @@ parameters: level: 5 paths: - src + exceptions: + check: + missingCheckedExceptionInThrows: true + tooWideThrowType: true + implicitThrows: false + uncheckedExceptionClasses: + - LogicException diff --git a/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php b/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php index 3d521c7..cf178f7 100644 --- a/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php +++ b/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php @@ -24,6 +24,9 @@ class ElasticallyExtension extends Extension { + /** + * @throws \Exception + */ public function load(array $configs, ContainerBuilder $container): void { $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs); diff --git a/src/Client.php b/src/Client.php index 3862376..19b22e3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -12,6 +12,7 @@ namespace JoliCode\Elastically; use Elastica\Client as ElasticaClient; +use Elastica\Exception\ExceptionInterface; use Psr\Log\LoggerInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\SerializerInterface; @@ -67,6 +68,9 @@ public function getPrefixedIndex(string $name): string return $this->indexNameMapper->getPrefixedIndex($name); } + /** + * @throws ExceptionInterface + */ public function getIndexNameFromClass(string $className): string { trigger_deprecation('jolicode/elastically', '1.4.0', 'Method %s() is deprecated. Use %s::%s() instead.', __METHOD__, IndexNameMapper::class, __FUNCTION__); @@ -74,6 +78,9 @@ public function getIndexNameFromClass(string $className): string return $this->indexNameMapper->getIndexNameFromClass($className); } + /** + * @throws ExceptionInterface + */ public function getClassFromIndexName(string $indexName): string { trigger_deprecation('jolicode/elastically', '1.4.0', 'Method %s() is deprecated. Use %s::%s() instead.', __METHOD__, IndexNameMapper::class, __FUNCTION__); diff --git a/src/Index.php b/src/Index.php index 1e5f3d5..66f2dba 100644 --- a/src/Index.php +++ b/src/Index.php @@ -11,9 +11,11 @@ namespace JoliCode\Elastically; +use Elastica\Exception\ExceptionInterface; use Elastica\Index as ElasticaIndex; use Elastica\ResultSet\BuilderInterface; use Elastica\Search; +use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface; class Index extends ElasticaIndex { @@ -26,6 +28,10 @@ public function __construct(Client $client, string $name, ResultSetBuilder $resu $this->resultSetBuilder = $resultSetBuilder; } + /** + * @throws ExceptionInterface + * @throws SerializerExceptionInterface + */ public function getModel($id) { $document = $this->getDocument($id); diff --git a/src/IndexBuilder.php b/src/IndexBuilder.php index f77ddf9..3c5b056 100644 --- a/src/IndexBuilder.php +++ b/src/IndexBuilder.php @@ -11,6 +11,7 @@ namespace JoliCode\Elastically; +use Elastica\Exception\ExceptionInterface; use Elastica\Exception\RuntimeException; use Elastica\Reindex; use Elastica\Request; @@ -32,6 +33,9 @@ public function __construct(MappingProviderInterface $mappingProvider, Client $c $this->indexNameMapper = $indexNameMapper; } + /** + * @throws ExceptionInterface + */ public function createIndex(string $indexName, array $context = []): Index { $mapping = $this->mappingProvider->provideMapping($indexName, $context); @@ -48,6 +52,9 @@ public function createIndex(string $indexName, array $context = []): Index return $index; } + /** + * @throws ExceptionInterface + */ public function markAsLive(Index $index, string $indexName): Response { $indexPrefixedName = $this->indexNameMapper->getPrefixedIndex($indexName); @@ -60,16 +67,25 @@ public function markAsLive(Index $index, string $indexName): Response return $this->client->request('_aliases', Request::POST, $data); } + /** + * @throws ExceptionInterface + */ public function slowDownRefresh(Index $index): void { $index->getSettings()->setRefreshInterval('60s'); } + /** + * @throws ExceptionInterface + */ public function speedUpRefresh(Index $index): void { $index->getSettings()->setRefreshInterval('1s'); } + /** + * @throws ExceptionInterface + */ public function migrate(Index $currentIndex, array $params = [], array $context = []): Index { $pureIndexName = $this->indexNameMapper->getPureIndexName($currentIndex->getName()); @@ -96,6 +112,9 @@ public function migrate(Index $currentIndex, array $params = [], array $context return $newIndex; } + /** + * @throws ExceptionInterface + */ public function purgeOldIndices(string $indexName, bool $dryRun = false): array { $indexName = $this->indexNameMapper->getPrefixedIndex($indexName); diff --git a/src/IndexNameMapper.php b/src/IndexNameMapper.php index 3effb4e..7d151da 100644 --- a/src/IndexNameMapper.php +++ b/src/IndexNameMapper.php @@ -11,6 +11,7 @@ namespace JoliCode\Elastically; +use Elastica\Exception\ExceptionInterface; use Elastica\Exception\RuntimeException; class IndexNameMapper @@ -33,6 +34,9 @@ public function getPrefixedIndex(string $name): string return $name; } + /** + * @throws ExceptionInterface + */ public function getIndexNameFromClass(string $className): string { $indexName = array_search($className, $this->indexClassMapping, true); @@ -44,6 +48,9 @@ public function getIndexNameFromClass(string $className): string return $this->getPrefixedIndex($indexName); } + /** + * @throws ExceptionInterface + */ public function getClassFromIndexName(string $indexName): string { if (!isset($this->indexClassMapping[$indexName])) { diff --git a/src/Indexer.php b/src/Indexer.php index 268ce9c..d0ff713 100644 --- a/src/Indexer.php +++ b/src/Indexer.php @@ -13,6 +13,7 @@ use Elastica\Bulk; use Elastica\Document as ElasticaDocument; +use Elastica\Exception\ExceptionInterface; use Elastica\Index; use JoliCode\Elastically\Model\Document; use JoliCode\Elastically\Serializer\ContextBuilderInterface; @@ -40,6 +41,9 @@ public function __construct(Client $client, SerializerInterface $serializer, int $this->contextBuilder = $contextBuilder ?? new StaticContextBuilder(); } + /** + * @throws ExceptionInterface + */ public function scheduleIndex($index, ElasticaDocument $document) { $document->setIndex($index instanceof Index ? $index->getName() : $index); @@ -50,6 +54,9 @@ public function scheduleIndex($index, ElasticaDocument $document) $this->flushIfNeeded(); } + /** + * @throws ExceptionInterface + */ public function scheduleDelete($index, string $id) { $document = new Document($id); @@ -59,6 +66,9 @@ public function scheduleDelete($index, string $id) $this->flushIfNeeded(); } + /** + * @throws ExceptionInterface + */ public function scheduleUpdate($index, ElasticaDocument $document) { $document->setIndex($index instanceof Index ? $index->getName() : $index); @@ -69,6 +79,9 @@ public function scheduleUpdate($index, ElasticaDocument $document) $this->flushIfNeeded(); } + /** + * @throws ExceptionInterface + */ public function scheduleCreate($index, ElasticaDocument $document) { $document->setIndex($index instanceof Index ? $index->getName() : $index); @@ -79,6 +92,9 @@ public function scheduleCreate($index, ElasticaDocument $document) $this->flushIfNeeded(); } + /** + * @throws ExceptionInterface + */ public function flush(): ?Bulk\ResponseSet { if (!$this->currentBulk) { @@ -107,6 +123,9 @@ public function getQueueSize() return \count($this->currentBulk->getActions()); } + /** + * @throws ExceptionInterface + */ public function refresh($index) { $indexName = $index instanceof Index ? $index->getName() : $index; @@ -114,6 +133,9 @@ public function refresh($index) $this->client->getIndex($indexName)->refresh(); } + /** + * @throws ExceptionInterface + */ public function setBulkMaxSize(int $bulkMaxSize): void { $this->bulkMaxSize = $bulkMaxSize; @@ -144,6 +166,9 @@ protected function getCurrentBulk(): Bulk return $this->currentBulk; } + /** + * @throws ExceptionInterface + */ protected function flushIfNeeded(): void { if ($this->getQueueSize() >= $this->bulkMaxSize) { diff --git a/src/Mapping/MappingProviderInterface.php b/src/Mapping/MappingProviderInterface.php index be820a0..e43dc37 100644 --- a/src/Mapping/MappingProviderInterface.php +++ b/src/Mapping/MappingProviderInterface.php @@ -11,7 +11,12 @@ namespace JoliCode\Elastically\Mapping; +use Elastica\Exception\ExceptionInterface; + interface MappingProviderInterface { + /** + * @throws ExceptionInterface + */ public function provideMapping(string $indexName, array $context = []): ?array; } diff --git a/src/Mapping/YamlProvider.php b/src/Mapping/YamlProvider.php index 3043918..411199e 100644 --- a/src/Mapping/YamlProvider.php +++ b/src/Mapping/YamlProvider.php @@ -12,6 +12,7 @@ namespace JoliCode\Elastically\Mapping; use Elastica\Exception\InvalidException; +use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Parser; final class YamlProvider implements MappingProviderInterface @@ -25,6 +26,9 @@ public function __construct(string $configurationDirectory, Parser $parser = nul $this->parser = $parser ?? new Parser(); } + /** + * @throws ParseException + */ public function provideMapping(string $indexName, array $context = []): ?array { $fileName = $context['filename'] ?? ($indexName . '_mapping.yaml'); diff --git a/src/Messenger/IndexationRequestHandler.php b/src/Messenger/IndexationRequestHandler.php index be0ac8f..241bd0e 100644 --- a/src/Messenger/IndexationRequestHandler.php +++ b/src/Messenger/IndexationRequestHandler.php @@ -12,6 +12,7 @@ namespace JoliCode\Elastically\Messenger; use Elastica\Exception\Bulk\ResponseException; +use Elastica\Exception\ExceptionInterface; use Elastica\Exception\RuntimeException; use JoliCode\Elastically\Client; use JoliCode\Elastically\Indexer; @@ -54,6 +55,10 @@ public function __construct(Client $client, MessageBusInterface $bus, DocumentEx $this->client->setLogger(new NullLogger()); } + /** + * @throws ExceptionInterface + * @throws UnrecoverableMessageHandlingException + */ public function __invoke(IndexationRequestInterface $message) { $messages = []; @@ -107,6 +112,10 @@ public function __invoke(IndexationRequestInterface $message) } } + /** + * @throws UnrecoverableMessageHandlingException + * @throws ExceptionInterface + */ private function schedule(Indexer $indexer, IndexationRequest $indexationRequest) { try { diff --git a/src/Messenger/IndexationRequestSpoolSubscriber.php b/src/Messenger/IndexationRequestSpoolSubscriber.php index 745ad8c..cd87621 100644 --- a/src/Messenger/IndexationRequestSpoolSubscriber.php +++ b/src/Messenger/IndexationRequestSpoolSubscriber.php @@ -15,6 +15,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\Messenger\Exception\TransportException; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\Transport\TransportInterface; use Symfony\Contracts\Service\ResetInterface; @@ -39,6 +40,9 @@ public function onException() $this->wasExceptionThrown = true; } + /** + * @throws TransportException + */ public function onTerminate() { if ($this->wasExceptionThrown) { @@ -61,6 +65,9 @@ public function onTerminate() $this->bus->dispatch($message); } + /** + * @throws TransportException + */ public function onResponse(ResponseEvent $event) { if (method_exists($event, 'isMainRequest')) { diff --git a/src/ResultSetBuilder.php b/src/ResultSetBuilder.php index 355414c..28d0831 100644 --- a/src/ResultSetBuilder.php +++ b/src/ResultSetBuilder.php @@ -12,12 +12,14 @@ namespace JoliCode\Elastically; use Elastica\Document as ElasticaDocument; +use Elastica\Exception\ExceptionInterface; use Elastica\Exception\RuntimeException; use Elastica\Query; use Elastica\Response; use Elastica\ResultSet; use Elastica\ResultSet\BuilderInterface; use JoliCode\Elastically\Serializer\ContextBuilderInterface; +use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; class ResultSetBuilder implements BuilderInterface @@ -36,6 +38,10 @@ public function __construct(IndexNameMapper $indexNameMapper, ContextBuilderInte $this->denormalizer = $denormalizer; } + /** + * @throws ExceptionInterface + * @throws SerializerExceptionInterface + */ public function buildResultSet(Response $response, Query $query): ResultSet { $data = $response->getData(); @@ -54,11 +60,19 @@ public function buildResultSet(Response $response, Query $query): ResultSet return new ResultSet($response, $query, $results); } + /** + * @throws ExceptionInterface + * @throws SerializerExceptionInterface + */ public function buildModelFromIndexAndData(string $indexName, $source) { return $this->buildModel($source, $indexName, []); } + /** + * @throws ExceptionInterface + * @throws SerializerExceptionInterface + */ public function buildModelFromDocument(ElasticaDocument $document) { return $this->buildModel($document->getData(), $document->getIndex(), [ @@ -66,6 +80,10 @@ public function buildModelFromDocument(ElasticaDocument $document) ]); } + /** + * @throws ExceptionInterface + * @throws SerializerExceptionInterface + */ private function buildModelFromResult(Result $result) { if (!$result->getIndex()) { @@ -77,6 +95,10 @@ private function buildModelFromResult(Result $result) ]); } + /** + * @throws ExceptionInterface + * @throws SerializerExceptionInterface + */ private function buildModel($source, string $indexName, array $context) { if (!$source) { diff --git a/src/Transport/HttpClientTransport.php b/src/Transport/HttpClientTransport.php index f01f07b..667627a 100644 --- a/src/Transport/HttpClientTransport.php +++ b/src/Transport/HttpClientTransport.php @@ -14,6 +14,7 @@ use Elastica\Connection; use Elastica\Exception\Connection\HttpException; use Elastica\Exception\ConnectionException; +use Elastica\Exception\ExceptionInterface; use Elastica\Exception\PartialShardFailureException; use Elastica\Exception\ResponseException; use Elastica\JSON; @@ -24,7 +25,10 @@ use Elastica\Util; use Symfony\Component\HttpClient\Exception\ClientException; use Symfony\Component\HttpClient\Exception\ServerException; +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -48,6 +52,13 @@ public function __construct(HttpClientInterface $client, string $scheme = 'http' $this->scheme = $scheme; } + /** + * @throws ExceptionInterface + * @throws TransportExceptionInterface + * @throws ClientExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ServerExceptionInterface + */ public function exec(Request $request, array $params): Response { $connection = $this->getConnection();