From 981c46ef033d592bd2a84f6d12919ce4c9b4eb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Mon, 1 Jul 2024 14:34:04 +0200 Subject: [PATCH] Up the code style to PHP 8.1 --- src/Config/Index.php | 71 ++++++------------- src/Config/IndexableResource.php | 37 ++++------ src/Controller/Action/SearchAction.php | 35 ++------- src/DataMapper/ImageUrlsDataMapper.php | 19 ++--- src/DataMapper/Product/PriceDataMapper.php | 5 +- .../Product/TaxonCodesDataMapper.php | 5 +- src/DataMapper/ResourceNameDataMapper.php | 5 +- src/DataMapper/UrlDataMapper.php | 5 +- .../Doctrine/EntityChangeSubscriber.php | 5 +- .../InjectConfigurationSubscriber.php | 9 +-- src/Indexer/AbstractIndexer.php | 6 +- src/Indexer/DefaultIndexer.php | 57 ++++----------- src/Javascript/Autocomplete/Source.php | 8 +-- .../Autocomplete/SourcesResolver.php | 24 +++---- src/Message/Command/EntityAwareCommand.php | 2 +- src/Message/Command/IndexEntities.php | 9 +-- src/Message/Handler/IndexEntitiesHandler.php | 5 +- src/Message/Handler/IndexEntityHandler.php | 5 +- src/Message/Handler/IndexHandler.php | 5 +- src/Message/Handler/IndexResourceHandler.php | 5 +- src/Message/Handler/RemoveEntityHandler.php | 5 +- .../IndexSettings/IndexSettingsProvider.php | 13 ++-- src/Resolver/IndexName/IndexNameResolver.php | 15 +--- src/Resolver/SortBy/SortBy.php | 8 +-- src/Resolver/SortBy/SortByResolver.php | 22 +----- src/Settings/SortableReplica.php | 13 +--- .../AbstractResourceUrlGenerator.php | 5 +- .../CompositeResourceUrlGenerator.php | 2 +- 28 files changed, 102 insertions(+), 303 deletions(-) diff --git a/src/Config/Index.php b/src/Config/Index.php index 05b6ba1..034ae68 100644 --- a/src/Config/Index.php +++ b/src/Config/Index.php @@ -7,49 +7,31 @@ use Setono\SyliusMeilisearchPlugin\Document\Document; use Setono\SyliusMeilisearchPlugin\Exception\NonExistingResourceException; use Setono\SyliusMeilisearchPlugin\Indexer\IndexerInterface; -use Webmozart\Assert\Assert; -final class Index +final class Index implements \Stringable { - /** - * This is the name you gave the index in the configuration. - * This name is also used when resolving the final index name in Meilisearch, so do not change this unless you know what you're doing - */ - public string $name; - - /** - * This is the FQCN for the document that is mapped to an index in Algolia. - * If you are indexing products this could be Setono\SyliusMeilisearchPlugin\Document\Product - * - * @var class-string - */ - public string $document; - - public IndexerInterface $indexer; - - /** - * An array of resources, indexed by the resource name - * - * @var array - */ - public array $resources; - - /** @var non-empty-string|null */ - public ?string $prefix; - - /** - * @param class-string $document - * @param array $resources - */ public function __construct( - string $name, - string $document, - IndexerInterface $indexer, - array $resources, - string $prefix = null, + /** + * This is the name you gave the index in the configuration. + * This name is also used when resolving the final index name in Meilisearch, so do not change this unless you know what you're doing + */ + public readonly string $name, + /** + * This is the FQCN for the document that is mapped to an index in Algolia. + * If you are indexing products this could be Setono\SyliusMeilisearchPlugin\Document\Product + * + * @var class-string $document + */ + public readonly string $document, + public readonly IndexerInterface $indexer, + /** + * An array of resources, indexed by the resource name + * + * @var array $resources + */ + public readonly array $resources, + public readonly ?string $prefix = null, ) { - Assert::stringNotEmpty($name); - if (!is_a($document, Document::class, true)) { throw new \InvalidArgumentException(sprintf( 'The document class %s MUST be an instance of %s', @@ -57,18 +39,9 @@ public function __construct( Document::class, )); } - - $this->name = $name; - $this->document = $document; - $this->indexer = $indexer; - $this->resources = $resources; - $this->prefix = '' === $prefix ? null : $prefix; } - /** - * @param string|IndexableResource $resource - */ - public function hasResource($resource): bool + public function hasResource(string|IndexableResource $resource): bool { if ($resource instanceof IndexableResource) { $resource = $resource->name; diff --git a/src/Config/IndexableResource.php b/src/Config/IndexableResource.php index 7347213..beffe1f 100644 --- a/src/Config/IndexableResource.php +++ b/src/Config/IndexableResource.php @@ -5,32 +5,24 @@ namespace Setono\SyliusMeilisearchPlugin\Config; use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface; -use Webmozart\Assert\Assert; /** * This class represents a Sylius resource that is indexable */ -final class IndexableResource +final class IndexableResource implements \Stringable { - /** - * This is the name of the Sylius resource, e.g. 'sylius.product' - */ - public string $name; - - /** - * This is the FQCN for the resource - * - * @var class-string - */ - public string $class; - - /** - * @param class-string $class - */ - public function __construct(string $name, string $class) - { - Assert::stringNotEmpty($name); - + public function __construct( + /** + * This is the name of the Sylius resource, e.g. 'sylius.product' + */ + public readonly string $name, + /** + * This is the FQCN for the resource + * + * @var class-string $class + */ + public readonly string $class, + ) { if (!is_a($class, IndexableInterface::class, true)) { throw new \InvalidArgumentException(sprintf( 'The document class %s MUST be an instance of %s', @@ -38,9 +30,6 @@ public function __construct(string $name, string $class) IndexableInterface::class, )); } - - $this->name = $name; - $this->class = $class; } /** diff --git a/src/Controller/Action/SearchAction.php b/src/Controller/Action/SearchAction.php index 1e10413..2c3e2a0 100644 --- a/src/Controller/Action/SearchAction.php +++ b/src/Controller/Action/SearchAction.php @@ -18,36 +18,15 @@ final class SearchAction { - private Environment $twig; - - private IndexNameResolverInterface $indexNameResolver; - - private TaxonRepositoryInterface $taxonRepository; - - private LocaleContextInterface $localeContext; - - private EventDispatcherInterface $eventDispatcher; - - private IndexRegistry $indexRegistry; - - private SortByResolverInterface $sortByResolver; - public function __construct( - Environment $twig, - IndexNameResolverInterface $indexNameResolver, - TaxonRepositoryInterface $taxonRepository, - LocaleContextInterface $localeContext, - EventDispatcherInterface $eventDispatcher, - IndexRegistry $indexableResourceRegistry, - SortByResolverInterface $sortByResolver, + private readonly Environment $twig, + private readonly IndexNameResolverInterface $indexNameResolver, + private readonly TaxonRepositoryInterface $taxonRepository, + private readonly LocaleContextInterface $localeContext, + private readonly EventDispatcherInterface $eventDispatcher, + private readonly IndexRegistry $indexRegistry, + private readonly SortByResolverInterface $sortByResolver, ) { - $this->twig = $twig; - $this->indexNameResolver = $indexNameResolver; - $this->taxonRepository = $taxonRepository; - $this->localeContext = $localeContext; - $this->eventDispatcher = $eventDispatcher; - $this->indexRegistry = $indexableResourceRegistry; - $this->sortByResolver = $sortByResolver; } public function __invoke(string $slug): Response diff --git a/src/DataMapper/ImageUrlsDataMapper.php b/src/DataMapper/ImageUrlsDataMapper.php index 41b67ab..f991f2b 100644 --- a/src/DataMapper/ImageUrlsDataMapper.php +++ b/src/DataMapper/ImageUrlsDataMapper.php @@ -15,24 +15,15 @@ final class ImageUrlsDataMapper implements DataMapperInterface { - private CacheManager $cacheManager; - - /** @var array, string> */ - private array $resourceToFilterSetMapping; - - private string $defaultFilterSet; - /** * @param array, string> $resourceToFilterSetMapping */ public function __construct( - CacheManager $cacheManager, - array $resourceToFilterSetMapping = [], // todo add this to the plugin configuration - string $defaultFilterSet = 'sylius_large', + private readonly CacheManager $cacheManager, + private readonly array $resourceToFilterSetMapping = [], + // todo add this to the plugin configuration + private readonly string $defaultFilterSet = 'sylius_large', ) { - $this->cacheManager = $cacheManager; - $this->resourceToFilterSetMapping = $resourceToFilterSetMapping; - $this->defaultFilterSet = $defaultFilterSet; } public function map( @@ -51,7 +42,7 @@ public function map( foreach ($source->getImages() as $image) { $imageUrls[] = $this->cacheManager->getBrowserPath( (string) $image->getPath(), - $this->resourceToFilterSetMapping[get_class($source)] ?? $this->defaultFilterSet, + $this->resourceToFilterSetMapping[$source::class] ?? $this->defaultFilterSet, [], null, UrlGeneratorInterface::ABSOLUTE_PATH, diff --git a/src/DataMapper/Product/PriceDataMapper.php b/src/DataMapper/Product/PriceDataMapper.php index ea90189..036e751 100644 --- a/src/DataMapper/Product/PriceDataMapper.php +++ b/src/DataMapper/Product/PriceDataMapper.php @@ -24,11 +24,8 @@ final class PriceDataMapper implements DataMapperInterface { use FormatAmountTrait; - private ChannelRepositoryInterface $channelRepository; - - public function __construct(ChannelRepositoryInterface $channelRepository) + public function __construct(private readonly ChannelRepositoryInterface $channelRepository) { - $this->channelRepository = $channelRepository; } /** diff --git a/src/DataMapper/Product/TaxonCodesDataMapper.php b/src/DataMapper/Product/TaxonCodesDataMapper.php index ec232ab..5f1929f 100644 --- a/src/DataMapper/Product/TaxonCodesDataMapper.php +++ b/src/DataMapper/Product/TaxonCodesDataMapper.php @@ -15,11 +15,8 @@ final class TaxonCodesDataMapper implements DataMapperInterface { - private bool $includeDescendants; - - public function __construct(bool $includeDescendants) + public function __construct(private readonly bool $includeDescendants) { - $this->includeDescendants = $includeDescendants; } /** diff --git a/src/DataMapper/ResourceNameDataMapper.php b/src/DataMapper/ResourceNameDataMapper.php index 45009fc..3789f90 100644 --- a/src/DataMapper/ResourceNameDataMapper.php +++ b/src/DataMapper/ResourceNameDataMapper.php @@ -11,11 +11,8 @@ final class ResourceNameDataMapper implements DataMapperInterface { - private IndexRegistry $indexRegistry; - - public function __construct(IndexRegistry $indexRegistry) + public function __construct(private readonly IndexRegistry $indexRegistry) { - $this->indexRegistry = $indexRegistry; } public function map(ResourceInterface $source, Document $target, IndexScope $indexScope, array $context = []): void diff --git a/src/DataMapper/UrlDataMapper.php b/src/DataMapper/UrlDataMapper.php index 9b81f5c..ad1ab07 100644 --- a/src/DataMapper/UrlDataMapper.php +++ b/src/DataMapper/UrlDataMapper.php @@ -13,11 +13,8 @@ final class UrlDataMapper implements DataMapperInterface { - private ResourceUrlGeneratorInterface $urlGenerator; - - public function __construct(ResourceUrlGeneratorInterface $urlGenerator) + public function __construct(private readonly ResourceUrlGeneratorInterface $urlGenerator) { - $this->urlGenerator = $urlGenerator; } /** diff --git a/src/EventSubscriber/Doctrine/EntityChangeSubscriber.php b/src/EventSubscriber/Doctrine/EntityChangeSubscriber.php index 592c30d..e04e4cd 100644 --- a/src/EventSubscriber/Doctrine/EntityChangeSubscriber.php +++ b/src/EventSubscriber/Doctrine/EntityChangeSubscriber.php @@ -14,11 +14,8 @@ final class EntityChangeSubscriber implements EventSubscriber { - private MessageBusInterface $commandBus; - - public function __construct(MessageBusInterface $commandBus) + public function __construct(private readonly MessageBusInterface $commandBus) { - $this->commandBus = $commandBus; } public function getSubscribedEvents(): array diff --git a/src/EventSubscriber/InjectConfigurationSubscriber.php b/src/EventSubscriber/InjectConfigurationSubscriber.php index f5ab7d1..1171e11 100644 --- a/src/EventSubscriber/InjectConfigurationSubscriber.php +++ b/src/EventSubscriber/InjectConfigurationSubscriber.php @@ -19,11 +19,8 @@ final class InjectConfigurationSubscriber implements EventSubscriberInterface */ private array $tags = []; - private Environment $twig; - - public function __construct(Environment $twig) + public function __construct(private readonly Environment $twig) { - $this->twig = $twig; } public static function getSubscribedEvents(): array @@ -64,8 +61,8 @@ public function inject(ResponseEvent $event): void // this 'if' has been copied from \Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse() if ($response->isRedirection() || 'html' !== $request->getRequestFormat() || - false !== stripos($response->headers->get('Content-Disposition', ''), 'attachment;') || - ($response->headers->has('Content-Type') && strpos($response->headers->get('Content-Type', ''), 'html') === false) + false !== stripos((string) $response->headers->get('Content-Disposition', ''), 'attachment;') || + ($response->headers->has('Content-Type') && !str_contains((string) $response->headers->get('Content-Type', ''), 'html')) ) { return; } diff --git a/src/Indexer/AbstractIndexer.php b/src/Indexer/AbstractIndexer.php index 3fbc7f6..fb7afcc 100644 --- a/src/Indexer/AbstractIndexer.php +++ b/src/Indexer/AbstractIndexer.php @@ -28,10 +28,10 @@ public function indexEntities(array $entities): void $type = null; foreach ($entities as $entity) { if ($type === null) { - $type = get_class($entity); + $type = $entity::class; } - if ($type !== get_class($entity)) { + if ($type !== $entity::class) { throw new \InvalidArgumentException('All the entities must be of the same type'); } @@ -44,7 +44,7 @@ public function indexEntities(array $entities): void public function removeEntity(IndexableInterface $entity): void { - $this->removeEntityWithId($entity->getId(), get_class($entity)); + $this->removeEntityWithId($entity->getId(), $entity::class); } public function removeEntityWithId(mixed $id, string $type): void diff --git a/src/Indexer/DefaultIndexer.php b/src/Indexer/DefaultIndexer.php index d6bb86c..a434aa6 100644 --- a/src/Indexer/DefaultIndexer.php +++ b/src/Indexer/DefaultIndexer.php @@ -36,55 +36,24 @@ class DefaultIndexer extends AbstractIndexer { use ORMTrait; - protected IndexScopeProviderInterface $indexScopeProvider; - - protected IndexNameResolverInterface $indexNameResolver; - - protected IndexSettingsProviderInterface $indexSettingsProvider; - - protected DataMapperInterface $dataMapper; - - protected MessageBusInterface $commandBus; - - protected NormalizerInterface $normalizer; - - protected IndexRegistry $indexRegistry; - - protected DoctrineFilterInterface $doctrineFilter; - - protected ObjectFilterInterface $objectFilter; - - /** @var list */ - protected array $normalizationGroups; - /** * @param list $normalizationGroups */ public function __construct( ManagerRegistry $managerRegistry, - IndexScopeProviderInterface $indexScopeProvider, - IndexNameResolverInterface $indexNameResolver, - IndexSettingsProviderInterface $indexSettingsProvider, - DataMapperInterface $dataMapper, - MessageBusInterface $commandBus, - NormalizerInterface $normalizer, - Client $client, - IndexRegistry $indexRegistry, - DoctrineFilterInterface $doctrineFilter, - ObjectFilterInterface $objectFilter, - array $normalizationGroups = ['setono:sylius-meilisearch:document'], + protected readonly IndexScopeProviderInterface $indexScopeProvider, + protected readonly IndexNameResolverInterface $indexNameResolver, + protected readonly IndexSettingsProviderInterface $indexSettingsProvider, + protected readonly DataMapperInterface $dataMapper, + protected readonly MessageBusInterface $commandBus, + protected readonly NormalizerInterface $normalizer, + protected readonly Client $client, + protected readonly IndexRegistry $indexRegistry, + protected readonly DoctrineFilterInterface $doctrineFilter, + protected readonly ObjectFilterInterface $objectFilter, + protected readonly array $normalizationGroups = ['setono:sylius-meilisearch:document'], ) { $this->managerRegistry = $managerRegistry; - $this->indexScopeProvider = $indexScopeProvider; - $this->indexNameResolver = $indexNameResolver; - $this->indexSettingsProvider = $indexSettingsProvider; - $this->dataMapper = $dataMapper; - $this->commandBus = $commandBus; - $this->normalizer = $normalizer; - $this->indexRegistry = $indexRegistry; - $this->doctrineFilter = $doctrineFilter; - $this->objectFilter = $objectFilter; - $this->normalizationGroups = $normalizationGroups; } public function index(Index|string $index): void @@ -239,7 +208,7 @@ protected function normalize(Document $document): array protected function prepareIndex(string $indexName, IndexSettings $indexSettings): SearchIndex { - $index = $this->algoliaClient->initIndex($indexName); + $index = $this->client->initIndex($indexName); Assert::isInstanceOf($index, SearchIndex::class); // if the index already exists we don't want to override any settings. TODO why don't we want that? Should we make a command that resets settings to application defaults? We also need to take into account the forwardToReplicas option below @@ -262,7 +231,7 @@ protected function prepareIndex(string $indexName, IndexSettings $indexSettings) continue; } - $replicaIndex = $this->algoliaClient->initIndex($replica->name); + $replicaIndex = $this->client->initIndex($replica->name); Assert::isInstanceOf($replicaIndex, SearchIndex::class); $replicaIndexSettings = clone $indexSettings; diff --git a/src/Javascript/Autocomplete/Source.php b/src/Javascript/Autocomplete/Source.php index cf57f40..8631ef5 100644 --- a/src/Javascript/Autocomplete/Source.php +++ b/src/Javascript/Autocomplete/Source.php @@ -8,16 +8,10 @@ final class Source { - public string $sourceId; - - public string $indexName; - public Settings $params; - public function __construct(string $sourceId, string $indexName, Settings $params = null) + public function __construct(public string $sourceId, public string $indexName, Settings $params = null) { - $this->sourceId = $sourceId; - $this->indexName = $indexName; $this->params = $params ?? new Settings(); } } diff --git a/src/Javascript/Autocomplete/SourcesResolver.php b/src/Javascript/Autocomplete/SourcesResolver.php index 85cb33e..4a08630 100644 --- a/src/Javascript/Autocomplete/SourcesResolver.php +++ b/src/Javascript/Autocomplete/SourcesResolver.php @@ -9,25 +9,17 @@ final class SourcesResolver implements SourcesResolverInterface { - private IndexRegistry $indexRegistry; - - private IndexNameResolverInterface $indexNameResolver; - - /** - * These are the search indexes defined in the configuration of the plugin (inside setono_sylius_meilisearch.search.indexes) - * - * @var list - */ - private array $searchIndexes; - /** * @param list $searchIndexes */ - public function __construct(IndexRegistry $indexRegistry, IndexNameResolverInterface $indexNameResolver, array $searchIndexes) - { - $this->indexRegistry = $indexRegistry; - $this->indexNameResolver = $indexNameResolver; - $this->searchIndexes = $searchIndexes; + public function __construct( + private readonly IndexRegistry $indexRegistry, + private readonly IndexNameResolverInterface $indexNameResolver, + /** + * These are the search indexes defined in the configuration of the plugin (inside setono_sylius_meilisearch.search.indexes) + */ + private readonly array $searchIndexes, + ) { } public function getSources(): array diff --git a/src/Message/Command/EntityAwareCommand.php b/src/Message/Command/EntityAwareCommand.php index 6df299d..25028ec 100644 --- a/src/Message/Command/EntityAwareCommand.php +++ b/src/Message/Command/EntityAwareCommand.php @@ -16,7 +16,7 @@ abstract class EntityAwareCommand implements CommandInterface public function __construct(IndexableInterface $resource) { - $this->entityClass = get_class($resource); + $this->entityClass = $resource::class; $this->entityId = $resource->getId(); } } diff --git a/src/Message/Command/IndexEntities.php b/src/Message/Command/IndexEntities.php index 0e8ab1c..27e26a6 100644 --- a/src/Message/Command/IndexEntities.php +++ b/src/Message/Command/IndexEntities.php @@ -8,17 +8,10 @@ final class IndexEntities implements CommandInterface { - public IndexableResource $resource; - - /** @var non-empty-list */ - public array $ids; - /** * @param non-empty-list $ids */ - public function __construct(IndexableResource $resource, array $ids) + public function __construct(public IndexableResource $resource, public array $ids) { - $this->resource = $resource; - $this->ids = $ids; } } diff --git a/src/Message/Handler/IndexEntitiesHandler.php b/src/Message/Handler/IndexEntitiesHandler.php index 3ad45a5..7151795 100644 --- a/src/Message/Handler/IndexEntitiesHandler.php +++ b/src/Message/Handler/IndexEntitiesHandler.php @@ -10,11 +10,8 @@ final class IndexEntitiesHandler { - private IndexRegistry $indexRegistry; - - public function __construct(IndexRegistry $indexRegistry) + public function __construct(private readonly IndexRegistry $indexRegistry) { - $this->indexRegistry = $indexRegistry; } public function __invoke(IndexEntities $message): void diff --git a/src/Message/Handler/IndexEntityHandler.php b/src/Message/Handler/IndexEntityHandler.php index bdf8514..24c6edf 100644 --- a/src/Message/Handler/IndexEntityHandler.php +++ b/src/Message/Handler/IndexEntityHandler.php @@ -10,11 +10,8 @@ final class IndexEntityHandler { - private IndexRegistry $indexRegistry; - - public function __construct(IndexRegistry $indexRegistry) + public function __construct(private readonly IndexRegistry $indexRegistry) { - $this->indexRegistry = $indexRegistry; } public function __invoke(IndexEntity $message): void diff --git a/src/Message/Handler/IndexHandler.php b/src/Message/Handler/IndexHandler.php index be1477d..58ffbf5 100644 --- a/src/Message/Handler/IndexHandler.php +++ b/src/Message/Handler/IndexHandler.php @@ -11,11 +11,8 @@ final class IndexHandler { - private IndexRegistry $indexRegistry; - - public function __construct(IndexRegistry $indexRegistry) + public function __construct(private readonly IndexRegistry $indexRegistry) { - $this->indexRegistry = $indexRegistry; } public function __invoke(Index $message): void diff --git a/src/Message/Handler/IndexResourceHandler.php b/src/Message/Handler/IndexResourceHandler.php index 63600f2..b3291bc 100644 --- a/src/Message/Handler/IndexResourceHandler.php +++ b/src/Message/Handler/IndexResourceHandler.php @@ -12,11 +12,8 @@ final class IndexResourceHandler { - private IndexRegistry $indexRegistry; - - public function __construct(IndexRegistry $indexRegistry) + public function __construct(private readonly IndexRegistry $indexRegistry) { - $this->indexRegistry = $indexRegistry; } public function __invoke(IndexResource $message): void diff --git a/src/Message/Handler/RemoveEntityHandler.php b/src/Message/Handler/RemoveEntityHandler.php index 3197c73..6844e7b 100644 --- a/src/Message/Handler/RemoveEntityHandler.php +++ b/src/Message/Handler/RemoveEntityHandler.php @@ -10,11 +10,8 @@ final class RemoveEntityHandler { - private IndexRegistry $indexRegistry; - - public function __construct(IndexRegistry $indexRegistry) + public function __construct(private readonly IndexRegistry $indexRegistry) { - $this->indexRegistry = $indexRegistry; } public function __invoke(RemoveEntity $message): void diff --git a/src/Provider/IndexSettings/IndexSettingsProvider.php b/src/Provider/IndexSettings/IndexSettingsProvider.php index 8ffa948..41b3071 100644 --- a/src/Provider/IndexSettings/IndexSettingsProvider.php +++ b/src/Provider/IndexSettings/IndexSettingsProvider.php @@ -12,16 +12,11 @@ final class IndexSettingsProvider implements IndexSettingsProviderInterface { - private ReplicaIndexNameResolverInterface $replicaIndexNameResolver; - - private IndexNameResolverInterface $indexNameResolver; - public function __construct( - IndexNameResolverInterface $indexNameResolver, - ReplicaIndexNameResolverInterface $replicaIndexNameResolver, - ) { - $this->replicaIndexNameResolver = $replicaIndexNameResolver; - $this->indexNameResolver = $indexNameResolver; + private readonly IndexNameResolverInterface $indexNameResolver, + private readonly ReplicaIndexNameResolverInterface $replicaIndexNameResolver, + ) + { } // todo implement an easier way to set settings decoupled from the document. This could be by dispatching an IndexSettingsEvent diff --git a/src/Resolver/IndexName/IndexNameResolver.php b/src/Resolver/IndexName/IndexNameResolver.php index 8db3fad..003bc3c 100644 --- a/src/Resolver/IndexName/IndexNameResolver.php +++ b/src/Resolver/IndexName/IndexNameResolver.php @@ -16,20 +16,11 @@ */ final class IndexNameResolver implements IndexNameResolverInterface { - private IndexRegistry $indexRegistry; - - private IndexScopeProviderInterface $indexScopeProvider; - - private string $environment; - public function __construct( - IndexRegistry $indexRegistry, - IndexScopeProviderInterface $indexScopeProvider, - string $environment, + private readonly IndexRegistry $indexRegistry, + private readonly IndexScopeProviderInterface $indexScopeProvider, + private readonly string $environment, ) { - $this->indexRegistry = $indexRegistry; - $this->indexScopeProvider = $indexScopeProvider; - $this->environment = $environment; } public function resolve($resource): string diff --git a/src/Resolver/SortBy/SortBy.php b/src/Resolver/SortBy/SortBy.php index e18a8f7..4c839d7 100644 --- a/src/Resolver/SortBy/SortBy.php +++ b/src/Resolver/SortBy/SortBy.php @@ -6,14 +6,8 @@ final class SortBy implements \JsonSerializable { - public string $label; - - public string $index; - - public function __construct(string $label, string $index) + public function __construct(public string $label, public string $index) { - $this->label = $label; - $this->index = $index; } /** diff --git a/src/Resolver/SortBy/SortByResolver.php b/src/Resolver/SortBy/SortByResolver.php index 1f3b965..f0d6ed0 100644 --- a/src/Resolver/SortBy/SortByResolver.php +++ b/src/Resolver/SortBy/SortByResolver.php @@ -12,29 +12,13 @@ final class SortByResolver implements SortByResolverInterface { - private IndexNameResolverInterface $indexNameResolver; - - private ReplicaIndexNameResolverInterface $replicaIndexNameResolver; - - private TranslatorInterface $translator; - - private LocaleContextInterface $localeContext; - - public function __construct( - IndexNameResolverInterface $indexNameResolver, - ReplicaIndexNameResolverInterface $replicaIndexNameResolver, - TranslatorInterface $translator, - LocaleContextInterface $localeContext, - ) { - $this->indexNameResolver = $indexNameResolver; - $this->replicaIndexNameResolver = $replicaIndexNameResolver; - $this->translator = $translator; - $this->localeContext = $localeContext; + public function __construct(private readonly IndexNameResolverInterface $indexNameResolver, private readonly ReplicaIndexNameResolverInterface $replicaIndexNameResolver, private readonly TranslatorInterface $translator, private readonly LocaleContextInterface $localeContext) + { } public function resolveFromIndexableResource(Index $index, string $locale = null): array { - $locale = $locale ?? $this->localeContext->getLocaleCode(); + $locale ??= $this->localeContext->getLocaleCode(); $indexName = $this->indexNameResolver->resolve($index); diff --git a/src/Settings/SortableReplica.php b/src/Settings/SortableReplica.php index c2e301e..38de62f 100644 --- a/src/Settings/SortableReplica.php +++ b/src/Settings/SortableReplica.php @@ -4,19 +4,10 @@ namespace Setono\SyliusMeilisearchPlugin\Settings; -final class SortableReplica +final class SortableReplica implements \Stringable { - public string $name; - - public string $attribute; - - public string $order; - - public function __construct(string $name, string $attribute, string $order) + public function __construct(public string $name, public string $attribute, public string $order) { - $this->name = $name; - $this->attribute = $attribute; - $this->order = $order; } public function ranking(): string diff --git a/src/UrlGenerator/AbstractResourceUrlGenerator.php b/src/UrlGenerator/AbstractResourceUrlGenerator.php index 214c7cc..302280c 100644 --- a/src/UrlGenerator/AbstractResourceUrlGenerator.php +++ b/src/UrlGenerator/AbstractResourceUrlGenerator.php @@ -8,10 +8,7 @@ abstract class AbstractResourceUrlGenerator implements ResourceUrlGeneratorInterface { - protected UrlGeneratorInterface $urlGenerator; - - public function __construct(UrlGeneratorInterface $urlGenerator) + public function __construct(protected UrlGeneratorInterface $urlGenerator) { - $this->urlGenerator = $urlGenerator; } } diff --git a/src/UrlGenerator/CompositeResourceUrlGenerator.php b/src/UrlGenerator/CompositeResourceUrlGenerator.php index 7787ad7..51918ec 100644 --- a/src/UrlGenerator/CompositeResourceUrlGenerator.php +++ b/src/UrlGenerator/CompositeResourceUrlGenerator.php @@ -24,7 +24,7 @@ public function generate(ResourceInterface $resource, array $context = []): stri } } - throw new \RuntimeException(sprintf('No url generators supports the given resource %s', get_class($resource))); + throw new \RuntimeException(sprintf('No url generators supports the given resource %s', $resource::class)); } public function supports(ResourceInterface $resource, array $context = []): bool