Skip to content

Commit

Permalink
feat(qa): Add missing @throws annotation (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Jun 16, 2023
1 parent 96a52d1 commit f29f165
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 0 deletions.
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ parameters:
level: 5
paths:
- src
exceptions:
check:
missingCheckedExceptionInThrows: true
tooWideThrowType: true
implicitThrows: false
uncheckedExceptionClasses:
- LogicException
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,13 +68,19 @@ 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__);

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__);
Expand Down
6 changes: 6 additions & 0 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions src/IndexBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace JoliCode\Elastically;

use Elastica\Exception\ExceptionInterface;
use Elastica\Exception\RuntimeException;
use Elastica\Reindex;
use Elastica\Request;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/IndexNameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace JoliCode\Elastically;

use Elastica\Exception\ExceptionInterface;
use Elastica\Exception\RuntimeException;

class IndexNameMapper
Expand All @@ -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);
Expand All @@ -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])) {
Expand Down
25 changes: 25 additions & 0 deletions src/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -50,6 +54,9 @@ public function scheduleIndex($index, ElasticaDocument $document)
$this->flushIfNeeded();
}

/**
* @throws ExceptionInterface
*/
public function scheduleDelete($index, string $id)
{
$document = new Document($id);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -79,6 +92,9 @@ public function scheduleCreate($index, ElasticaDocument $document)
$this->flushIfNeeded();
}

/**
* @throws ExceptionInterface
*/
public function flush(): ?Bulk\ResponseSet
{
if (!$this->currentBulk) {
Expand Down Expand Up @@ -107,13 +123,19 @@ public function getQueueSize()
return \count($this->currentBulk->getActions());
}

/**
* @throws ExceptionInterface
*/
public function refresh($index)
{
$indexName = $index instanceof Index ? $index->getName() : $index;

$this->client->getIndex($indexName)->refresh();
}

/**
* @throws ExceptionInterface
*/
public function setBulkMaxSize(int $bulkMaxSize): void
{
$this->bulkMaxSize = $bulkMaxSize;
Expand Down Expand Up @@ -144,6 +166,9 @@ protected function getCurrentBulk(): Bulk
return $this->currentBulk;
}

/**
* @throws ExceptionInterface
*/
protected function flushIfNeeded(): void
{
if ($this->getQueueSize() >= $this->bulkMaxSize) {
Expand Down
5 changes: 5 additions & 0 deletions src/Mapping/MappingProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

namespace JoliCode\Elastically\Mapping;

use Elastica\Exception\ExceptionInterface;

interface MappingProviderInterface
{
/**
* @throws ExceptionInterface
*/
public function provideMapping(string $indexName, array $context = []): ?array;
}
4 changes: 4 additions & 0 deletions src/Mapping/YamlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand Down
9 changes: 9 additions & 0 deletions src/Messenger/IndexationRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -107,6 +112,10 @@ public function __invoke(IndexationRequestInterface $message)
}
}

/**
* @throws UnrecoverableMessageHandlingException
* @throws ExceptionInterface
*/
private function schedule(Indexer $indexer, IndexationRequest $indexationRequest)
{
try {
Expand Down
7 changes: 7 additions & 0 deletions src/Messenger/IndexationRequestSpoolSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +40,9 @@ public function onException()
$this->wasExceptionThrown = true;
}

/**
* @throws TransportException
*/
public function onTerminate()
{
if ($this->wasExceptionThrown) {
Expand All @@ -61,6 +65,9 @@ public function onTerminate()
$this->bus->dispatch($message);
}

/**
* @throws TransportException
*/
public function onResponse(ResponseEvent $event)
{
if (method_exists($event, 'isMainRequest')) {
Expand Down
Loading

0 comments on commit f29f165

Please sign in to comment.