From c0863ec442ac4ae1b9e90c39c02b915b44fdaa3d Mon Sep 17 00:00:00 2001 From: Szymon Kostrubiec Date: Tue, 9 Jul 2024 09:33:22 +0200 Subject: [PATCH] OP-352 - Apply phpstan fixes --- src/Api/Resolver/FacetsResolver.php | 2 +- src/Context/TaxonContext.php | 7 ++++++- .../Api/ListProductsByPartialNameAction.php | 9 +++++---- .../Action/Shop/AbstractSearchAction.php | 9 ++++++++- .../Shop/SiteWideProductsSearchAction.php | 6 +++++- .../ShopProductListDataHandler.php | 5 +++-- .../ShopProductsSortDataHandler.php | 3 ++- src/Controller/Response/DTO/Item.php | 4 ++-- src/EventListener/OrderProductsListener.php | 6 +++++- .../ProductTaxonIndexListener.php | 6 +++++- src/EventListener/ResourceIndexListener.php | 4 ++++ src/Facet/AttributeFacet.php | 4 ++-- src/Facet/OptionFacet.php | 4 ++-- src/Facet/PriceFacet.php | 4 ++-- src/Finder/NamedProductsFinder.php | 2 ++ src/Finder/ProductAttributesFinder.php | 4 +++- src/Finder/ProductOptionsFinder.php | 4 +++- src/Finder/ShopProductsFinder.php | 1 + src/Form/Type/AbstractFilterType.php | 2 +- .../ChoiceMapper/ProductAttributesMapper.php | 2 ++ .../ChoiceMapper/ProductOptionsMapper.php | 1 + src/Form/Type/PriceFilterType.php | 2 +- src/Form/Type/ProductAttributesFilterType.php | 6 ++++-- src/Form/Type/ProductOptionsFilterType.php | 4 +++- src/Form/Type/SearchFacetsType.php | 4 +++- src/Model/SearchFacets.php | 6 +++--- src/PropertyBuilder/AttributeBuilder.php | 18 +++++++++++------- .../AttributeTaxonsBuilder.php | 2 +- src/PropertyBuilder/ChannelPricingBuilder.php | 5 ++++- src/PropertyBuilder/OptionBuilder.php | 13 +++++++++++-- .../ProductCreatedAtPropertyBuilder.php | 2 ++ .../ProductDescriptionBuilder.php | 5 ++++- src/PropertyBuilder/ProductNameBuilder.php | 5 ++++- .../ProductShortDescriptionBuilder.php | 5 ++++- .../ProductTaxonPositionPropertyBuilder.php | 13 ++++++++++--- .../SoldUnitsPropertyBuilder.php | 2 ++ .../AttributesTypeDateQueryBuilder.php | 2 +- .../AttributesTypeNumberQueryBuilder.php | 2 +- .../AttributesTypeTextQueryBuilder.php | 2 +- .../SiteWideFacetsQueryBuilder.php | 9 +++++++-- .../TaxonFacetsQueryBuilder.php | 1 + src/QueryBuilder/HasChannelQueryBuilder.php | 4 +++- .../HasPriceBetweenQueryBuilder.php | 19 +++++++++++++++---- .../ProductAttributesByTaxonQueryBuilder.php | 3 +++ .../ProductOptionsByTaxonQueryBuilder.php | 3 +++ .../SiteWideProductsQueryBuilder.php | 11 +++++++++-- .../TaxonProductsQueryBuilder.php | 19 +++++++++++++++---- src/Repository/OrderItemRepository.php | 7 +++++-- src/Repository/ProductAttributeRepository.php | 18 +++++++++++------- .../ProductAttributeValueRepository.php | 8 ++++++-- src/Repository/ProductOptionRepository.php | 12 +++++++----- src/Repository/ProductVariantRepository.php | 10 ++++++++-- src/Repository/TaxonRepository.php | 11 ++++++++--- .../Product/ChannelPricingTransformer.php | 6 +++++- src/Transformer/Product/ImageTransformer.php | 7 +++++-- 55 files changed, 246 insertions(+), 89 deletions(-) diff --git a/src/Api/Resolver/FacetsResolver.php b/src/Api/Resolver/FacetsResolver.php index da9bc7d3..40caa237 100644 --- a/src/Api/Resolver/FacetsResolver.php +++ b/src/Api/Resolver/FacetsResolver.php @@ -48,7 +48,7 @@ public function resolve(array $data): array } return array_filter($adapter->getAggregations(), function ($facet) { - return [] !== $facet['buckets'] ?? []; + return [] !== $facet['buckets']; }); } } diff --git a/src/Context/TaxonContext.php b/src/Context/TaxonContext.php index 15037b6d..1bfeda27 100644 --- a/src/Context/TaxonContext.php +++ b/src/Context/TaxonContext.php @@ -16,6 +16,7 @@ use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Locale\Context\LocaleContextInterface; use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; final class TaxonContext implements TaxonContextInterface @@ -29,11 +30,15 @@ public function __construct( public function getTaxon(): TaxonInterface { - $slug = $this->requestStack->getCurrentRequest()->get('slug'); + /** @var Request $request */ + $request = $this->requestStack->getCurrentRequest(); + + $slug = $request->get('slug'); $localeCode = $this->localeContext->getLocaleCode(); /** @var TaxonInterface $taxon */ $taxon = $this->taxonRepository->findOneBySlug($slug, $localeCode); + /** @phpstan-ignore-next-line */ if (null === $slug || null === $taxon) { throw new TaxonNotFoundException(); } diff --git a/src/Controller/Action/Api/ListProductsByPartialNameAction.php b/src/Controller/Action/Api/ListProductsByPartialNameAction.php index 283fba10..006f16f6 100644 --- a/src/Controller/Action/Api/ListProductsByPartialNameAction.php +++ b/src/Controller/Action/Api/ListProductsByPartialNameAction.php @@ -39,7 +39,8 @@ public function __invoke(Request $request): Response return new JsonResponse($itemsResponse->toArray()); } - $products = $this->namedProductsFinder->findByNamePart($request->query->get('query')); + /** @var array $products */ + $products = $this->namedProductsFinder->findByNamePart((string) $request->query->get('query')); /** @var ProductInterface $product */ foreach ($products as $product) { @@ -48,10 +49,10 @@ public function __invoke(Request $request): Response } $itemsResponse->addItem(new Item( - $productMainTaxon->getName(), - $product->getName(), + (string) $productMainTaxon->getName(), + (string) $product->getName(), $product->getShortDescription(), - $this->productSlugTransformer->transform($product), + (string) $this->productSlugTransformer->transform($product), $this->productChannelPriceTransformer->transform($product), $this->productImageTransformer->transform($product) )); diff --git a/src/Controller/Action/Shop/AbstractSearchAction.php b/src/Controller/Action/Shop/AbstractSearchAction.php index fbd0ceaa..a32d249f 100644 --- a/src/Controller/Action/Shop/AbstractSearchAction.php +++ b/src/Controller/Action/Shop/AbstractSearchAction.php @@ -14,6 +14,7 @@ use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\DataHandlerInterface; use BitBag\SyliusElasticsearchPlugin\Finder\ShopProductsFinderInterface; +use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; use Twig\Environment; @@ -30,9 +31,15 @@ public function __construct( protected function clearInvalidEntries(FormInterface $form, array $requestData): array { + /** @var FormError $error */ foreach ($form->getErrors(true) as $error) { + /** @var FormInterface $errorOrigin */ $errorOrigin = $error->getOrigin(); - $path = ($errorOrigin->getParent()->getPropertyPath() ?? '') . $errorOrigin->getPropertyPath(); + + /** @var FormInterface $parent */ + $parent = $errorOrigin->getParent(); + + $path = ($parent->getPropertyPath() ?? '') . $errorOrigin->getPropertyPath(); $keys = explode('][', trim($path, '[]')); diff --git a/src/Controller/Action/Shop/SiteWideProductsSearchAction.php b/src/Controller/Action/Shop/SiteWideProductsSearchAction.php index 6b47f321..a0df90dd 100644 --- a/src/Controller/Action/Shop/SiteWideProductsSearchAction.php +++ b/src/Controller/Action/Shop/SiteWideProductsSearchAction.php @@ -14,6 +14,7 @@ use BitBag\SyliusElasticsearchPlugin\Form\Type\SearchType; use BitBag\SyliusElasticsearchPlugin\Model\Search; +use BitBag\SyliusElasticsearchPlugin\Model\SearchBox; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -29,8 +30,11 @@ public function __invoke(Request $request): Response /** @var Search $search */ $search = $form->getData(); + /** @var SearchBox $searchBox */ + $searchBox = $search->getBox(); + $data = array_merge( - ['query' => $search->getBox()->getQuery()], + ['query' => $searchBox->getQuery()], ['facets' => $search->getFacets()], $this->dataHandler->retrieveData($request->query->all()), ); diff --git a/src/Controller/RequestDataHandler/ShopProductListDataHandler.php b/src/Controller/RequestDataHandler/ShopProductListDataHandler.php index bef607de..ebd3bcd4 100644 --- a/src/Controller/RequestDataHandler/ShopProductListDataHandler.php +++ b/src/Controller/RequestDataHandler/ShopProductListDataHandler.php @@ -32,10 +32,11 @@ public function __construct( public function retrieveData(array $requestData): array { + $data = []; $taxon = $this->taxonContext->getTaxon(); $data[$this->namePropertyPrefix] = (string) $requestData[$this->namePropertyPrefix]; - $data[$this->taxonsProperty] = strtolower($taxon->getCode()); + $data[$this->taxonsProperty] = strtolower((string) $taxon->getCode()); $data['taxon'] = $taxon; $data = array_merge( $data, @@ -77,7 +78,7 @@ private function handleAttributesPrefixedProperty( return; } - $attributeTypes = $this->getAttributeTypes($attributesDefinitions); + $attributeTypes = $this->getAttributeTypes((array) $attributesDefinitions); foreach ($requestData['attributes'] as $key => $value) { if (!is_array($value) || 0 !== strpos($key, $this->attributePropertyPrefix)) { diff --git a/src/Controller/RequestDataHandler/ShopProductsSortDataHandler.php b/src/Controller/RequestDataHandler/ShopProductsSortDataHandler.php index 1fd5c14c..87378153 100644 --- a/src/Controller/RequestDataHandler/ShopProductsSortDataHandler.php +++ b/src/Controller/RequestDataHandler/ShopProductsSortDataHandler.php @@ -37,11 +37,12 @@ public function retrieveData(array $requestData): array $availableSorters = [$this->soldUnitsProperty, $this->createdAtProperty, $this->pricePropertyPrefix]; $availableSorting = [self::SORT_ASC_INDEX, self::SORT_DESC_INDEX]; - if (!in_array($orderBy, $availableSorters) || !in_array($sort, $availableSorting)) { + if (!in_array($orderBy, $availableSorters, true) || !in_array($sort, $availableSorting, true)) { throw new UnexpectedValueException(); } if ($this->pricePropertyPrefix === $orderBy) { + /** @var string $channelCode */ $channelCode = $this->channelContext->getChannel()->getCode(); $orderBy = $this->channelPricingNameResolver->resolvePropertyName($channelCode); } diff --git a/src/Controller/Response/DTO/Item.php b/src/Controller/Response/DTO/Item.php index 1d6db042..1d75711c 100644 --- a/src/Controller/Response/DTO/Item.php +++ b/src/Controller/Response/DTO/Item.php @@ -77,10 +77,10 @@ public function toArray(): array return [ 'taxon_name' => $this->taxonName(), 'name' => $this->name(), - 'description' => $this->description() ?: '', + 'description' => $this->description() ?? '', 'slug' => $this->slug(), 'price' => $this->price(), - 'image' => $this->image() ?: '', + 'image' => $this->image() ?? '', ]; } } diff --git a/src/EventListener/OrderProductsListener.php b/src/EventListener/OrderProductsListener.php index a15e8844..0cde92a5 100644 --- a/src/EventListener/OrderProductsListener.php +++ b/src/EventListener/OrderProductsListener.php @@ -16,6 +16,7 @@ use FOS\ElasticaBundle\Persister\ObjectPersisterInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\OrderItemInterface; +use Sylius\Component\Resource\Model\ResourceInterface; use Symfony\Component\EventDispatcher\GenericEvent; use Webmozart\Assert\Assert; @@ -34,7 +35,10 @@ public function updateOrderProducts(GenericEvent $event): void /** @var OrderItemInterface $orderItem */ foreach ($order->getItems() as $orderItem) { - $this->resourceRefresher->refresh($orderItem->getProduct(), $this->productPersister); + /** @var ResourceInterface $product */ + $product = $orderItem->getProduct(); + + $this->resourceRefresher->refresh($product, $this->productPersister); } } } diff --git a/src/EventListener/ProductTaxonIndexListener.php b/src/EventListener/ProductTaxonIndexListener.php index efd31e1d..a84e0a87 100644 --- a/src/EventListener/ProductTaxonIndexListener.php +++ b/src/EventListener/ProductTaxonIndexListener.php @@ -15,6 +15,7 @@ use BitBag\SyliusElasticsearchPlugin\Refresher\ResourceRefresherInterface; use FOS\ElasticaBundle\Persister\ObjectPersisterInterface; use Sylius\Component\Core\Model\ProductTaxonInterface; +use Sylius\Component\Resource\Model\ResourceInterface; final class ProductTaxonIndexListener { @@ -26,6 +27,9 @@ public function __construct( public function updateIndex(ProductTaxonInterface $productTaxon): void { - $this->resourceRefresher->refresh($productTaxon->getProduct(), $this->objectPersister); + /** @var ResourceInterface $product */ + $product = $productTaxon->getProduct(); + + $this->resourceRefresher->refresh($product, $this->objectPersister); } } diff --git a/src/EventListener/ResourceIndexListener.php b/src/EventListener/ResourceIndexListener.php index 3cf5faca..eb501433 100644 --- a/src/EventListener/ResourceIndexListener.php +++ b/src/EventListener/ResourceIndexListener.php @@ -41,21 +41,25 @@ public function updateIndex(GenericEvent $event): void $method = $config[self::GET_PARENT_METHOD_KEY] ?? null; if (null !== $method && method_exists($resource, $method)) { + /** @phpstan-ignore-next-line */ $resource = $resource->$method(); } if ($resource instanceof $config[self::MODEL_KEY]) { + /** @phpstan-ignore-next-line */ $this->resourceRefresher->refresh($resource, $config[self::SERVICE_ID_KEY]); } if ($resource instanceof ProductInterface || $resource instanceof ProductVariantInterface) { if (ProductAttribute::class === $config[self::MODEL_KEY]) { foreach ($this->attributeRepository->findAll() as $attribute) { + /** @var ResourceInterface $attribute */ $this->resourceRefresher->refresh($attribute, $config[self::SERVICE_ID_KEY]); } } if (ProductOption::class === $config[self::MODEL_KEY]) { foreach ($this->optionRepository->findAll() as $option) { + /** @var ResourceInterface $option */ $this->resourceRefresher->refresh($option, $config[self::SERVICE_ID_KEY]); } } diff --git a/src/Facet/AttributeFacet.php b/src/Facet/AttributeFacet.php index f6fe4845..1e83f43e 100644 --- a/src/Facet/AttributeFacet.php +++ b/src/Facet/AttributeFacet.php @@ -52,14 +52,14 @@ public function getBucketLabel(array $bucket): string public function getLabel(): string { - return $this->getProductAttribute()->getName(); + return (string) $this->getProductAttribute()->getName(); } private function getFieldName(): string { return sprintf( '%s_%s.keyword', - $this->attributeNameResolver->resolvePropertyName($this->getProductAttribute()->getCode()), + $this->attributeNameResolver->resolvePropertyName((string) $this->getProductAttribute()->getCode()), $this->localeContext->getLocaleCode() ); } diff --git a/src/Facet/OptionFacet.php b/src/Facet/OptionFacet.php index a1431d9c..6ff99ca2 100644 --- a/src/Facet/OptionFacet.php +++ b/src/Facet/OptionFacet.php @@ -49,11 +49,11 @@ public function getBucketLabel(array $bucket): string public function getLabel(): string { - return $this->productOption->getName(); + return (string) $this->productOption->getName(); } private function getFieldName(): string { - return $this->optionNameResolver->resolvePropertyName($this->productOption->getCode()) . '.keyword'; + return $this->optionNameResolver->resolvePropertyName((string) $this->productOption->getCode()) . '.keyword'; } } diff --git a/src/Facet/PriceFacet.php b/src/Facet/PriceFacet.php index 0fddbc25..faea18be 100644 --- a/src/Facet/PriceFacet.php +++ b/src/Facet/PriceFacet.php @@ -36,7 +36,7 @@ public function __construct( public function getAggregation(): AbstractAggregation { $priceFieldName = $this->channelPricingNameResolver->resolvePropertyName( - $this->shopperContext->getChannel()->getCode() + (string) $this->shopperContext->getChannel()->getCode() ); $histogram = new Histogram(self::FACET_ID, $priceFieldName, $this->interval); $histogram->setMinimumDocumentCount(1); @@ -47,7 +47,7 @@ public function getAggregation(): AbstractAggregation public function getQuery(array $selectedBuckets): AbstractQuery { $priceFieldName = $this->channelPricingNameResolver->resolvePropertyName( - $this->shopperContext->getChannel()->getCode() + (string) $this->shopperContext->getChannel()->getCode() ); $query = new BoolQuery(); foreach ($selectedBuckets as $selectedBucket) { diff --git a/src/Finder/NamedProductsFinder.php b/src/Finder/NamedProductsFinder.php index 94871c7e..dc7ba371 100644 --- a/src/Finder/NamedProductsFinder.php +++ b/src/Finder/NamedProductsFinder.php @@ -13,6 +13,7 @@ namespace BitBag\SyliusElasticsearchPlugin\Finder; use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface; +use Elastica\Query\AbstractQuery; use FOS\ElasticaBundle\Finder\FinderInterface; final class NamedProductsFinder implements NamedProductsFinderInterface @@ -26,6 +27,7 @@ public function __construct( public function findByNamePart(string $namePart): ?array { $data = ['query' => $namePart]; + /** @var AbstractQuery $query */ $query = $this->queryBuilder->buildQuery($data); return $this->productsFinder->find($query); diff --git a/src/Finder/ProductAttributesFinder.php b/src/Finder/ProductAttributesFinder.php index 2c2b4c28..ad2326c9 100644 --- a/src/Finder/ProductAttributesFinder.php +++ b/src/Finder/ProductAttributesFinder.php @@ -13,6 +13,7 @@ namespace BitBag\SyliusElasticsearchPlugin\Finder; use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface; +use Elastica\Query\AbstractQuery; use FOS\ElasticaBundle\Finder\FinderInterface; use Sylius\Component\Core\Model\TaxonInterface; @@ -28,8 +29,9 @@ public function __construct( public function findByTaxon(TaxonInterface $taxon): ?array { $data = []; - $data[$this->taxonsProperty] = strtolower($taxon->getCode()); + $data[$this->taxonsProperty] = strtolower((string) $taxon->getCode()); + /** @var AbstractQuery $query */ $query = $this->attributesByTaxonQueryBuilder->buildQuery($data); return $this->attributesFinder->find($query, 20); diff --git a/src/Finder/ProductOptionsFinder.php b/src/Finder/ProductOptionsFinder.php index bcf122ed..8deee9f1 100644 --- a/src/Finder/ProductOptionsFinder.php +++ b/src/Finder/ProductOptionsFinder.php @@ -13,6 +13,7 @@ namespace BitBag\SyliusElasticsearchPlugin\Finder; use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface; +use Elastica\Query\AbstractQuery; use FOS\ElasticaBundle\Finder\FinderInterface; use Sylius\Component\Core\Model\TaxonInterface; @@ -28,8 +29,9 @@ public function __construct( public function findByTaxon(TaxonInterface $taxon): ?array { $data = []; - $data[$this->taxonsProperty] = strtolower($taxon->getCode()); + $data[$this->taxonsProperty] = strtolower((string) $taxon->getCode()); + /** @var AbstractQuery $query */ $query = $this->productOptionsByTaxonQueryBuilder->buildQuery($data); return $this->optionsFinder->find($query, 20); diff --git a/src/Finder/ShopProductsFinder.php b/src/Finder/ShopProductsFinder.php index bc3cafb4..3a869a03 100644 --- a/src/Finder/ShopProductsFinder.php +++ b/src/Finder/ShopProductsFinder.php @@ -31,6 +31,7 @@ public function __construct( public function find(array $data): Pagerfanta { + /** @var Query\BoolQuery $boolQuery */ $boolQuery = $this->queryBuilder->buildQuery($data); if (array_key_exists('facets', $data) && is_array($data['facets'])) { diff --git a/src/Form/Type/AbstractFilterType.php b/src/Form/Type/AbstractFilterType.php index 39f91a2c..cc2de73b 100644 --- a/src/Form/Type/AbstractFilterType.php +++ b/src/Form/Type/AbstractFilterType.php @@ -26,7 +26,7 @@ public function configureOptions(OptionsResolver $resolver): void ]); } - public function getBlockPrefix(): ?string + public function getBlockPrefix(): string { return ''; } diff --git a/src/Form/Type/ChoiceMapper/ProductAttributesMapper.php b/src/Form/Type/ChoiceMapper/ProductAttributesMapper.php index ec57788b..8b2f27dd 100644 --- a/src/Form/Type/ChoiceMapper/ProductAttributesMapper.php +++ b/src/Form/Type/ChoiceMapper/ProductAttributesMapper.php @@ -17,6 +17,7 @@ use BitBag\SyliusElasticsearchPlugin\Formatter\StringFormatterInterface; use BitBag\SyliusElasticsearchPlugin\Repository\ProductAttributeValueRepositoryInterface; use Sylius\Component\Attribute\AttributeType\SelectAttributeType; +use Sylius\Component\Core\Model\Taxon; use Sylius\Component\Locale\Context\LocaleContextInterface; use Sylius\Component\Product\Model\ProductAttributeInterface; @@ -47,6 +48,7 @@ public function mapToChoices(ProductAttributeInterface $productAttribute): array return $choices; } + /** @var Taxon $taxon */ $taxon = $this->taxonContext->getTaxon(); $attributeValues = $this->productAttributeValueRepository->getUniqueAttributeValues($productAttribute, $taxon); diff --git a/src/Form/Type/ChoiceMapper/ProductOptionsMapper.php b/src/Form/Type/ChoiceMapper/ProductOptionsMapper.php index 2284c3f8..d2913686 100644 --- a/src/Form/Type/ChoiceMapper/ProductOptionsMapper.php +++ b/src/Form/Type/ChoiceMapper/ProductOptionsMapper.php @@ -31,6 +31,7 @@ public function mapToChoices(ProductOptionInterface $productOption): array array_walk( $productOptionValues, function (ProductOptionValueInterface $productOptionValue) use (&$choices): void { + /** @var string $value */ $value = $productOptionValue->getValue(); $choices[$value] = $this->stringFormatter->formatToLowercaseWithoutSpaces($value); } diff --git a/src/Form/Type/PriceFilterType.php b/src/Form/Type/PriceFilterType.php index b3ab1131..40c20f1c 100644 --- a/src/Form/Type/PriceFilterType.php +++ b/src/Form/Type/PriceFilterType.php @@ -70,7 +70,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ], ]) ->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { - if (!empty($event->getData())) { + if (null !== $event->getData()) { $data = []; foreach ($event->getData() as $key => $item) { $data[$key] = trim($item); diff --git a/src/Form/Type/ProductAttributesFilterType.php b/src/Form/Type/ProductAttributesFilterType.php index 410a1c06..387cf604 100644 --- a/src/Form/Type/ProductAttributesFilterType.php +++ b/src/Form/Type/ProductAttributesFilterType.php @@ -31,8 +31,10 @@ public function __construct( public function buildForm(FormBuilderInterface $builder, array $attributes): void { - foreach ($this->productAttributesContext->getAttributes() as $productAttribute) { - if (in_array($productAttribute->getCode(), $this->excludedAttributes)) { + /** @var array $options */ + $options = $this->productAttributesContext->getAttributes(); + foreach ($options as $productAttribute) { + if (in_array($productAttribute->getCode(), $this->excludedAttributes, true)) { continue; } diff --git a/src/Form/Type/ProductOptionsFilterType.php b/src/Form/Type/ProductOptionsFilterType.php index ec93c8fc..075b9c7e 100644 --- a/src/Form/Type/ProductOptionsFilterType.php +++ b/src/Form/Type/ProductOptionsFilterType.php @@ -30,7 +30,9 @@ public function __construct( public function buildForm(FormBuilderInterface $builder, array $options): void { - foreach ($this->productOptionsContext->getOptions() as $productOption) { + /** @var array $options */ + $options = $this->productOptionsContext->getOptions(); + foreach ($options as $productOption) { $name = $this->optionNameResolver->resolvePropertyName($productOption->getCode()); $choices = $this->productOptionsMapper->mapToChoices($productOption); $choices = array_unique($choices); diff --git a/src/Form/Type/SearchFacetsType.php b/src/Form/Type/SearchFacetsType.php index 1011dbfc..97f56c57 100644 --- a/src/Form/Type/SearchFacetsType.php +++ b/src/Form/Type/SearchFacetsType.php @@ -17,6 +17,7 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Webmozart\Assert\Assert; final class SearchFacetsType extends AbstractType { @@ -33,7 +34,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void foreach ($facetData['buckets'] as $bucket) { $choices[$facet->getBucketLabel($bucket)] = $bucket['key']; } - if (!empty($choices)) { +// Assert::notEmpty($choices); + if ([] !== $choices) { $builder ->add( $facetId, diff --git a/src/Model/SearchFacets.php b/src/Model/SearchFacets.php index f275cf95..f7e55f14 100644 --- a/src/Model/SearchFacets.php +++ b/src/Model/SearchFacets.php @@ -18,7 +18,7 @@ final class SearchFacets implements Iterator { private array $selectedBuckets = []; - public function __get(string $facetId) + public function __get(string $facetId): array { if (!array_key_exists($facetId, $this->selectedBuckets)) { return []; @@ -27,7 +27,7 @@ public function __get(string $facetId) return $this->selectedBuckets[$facetId]; } - public function __set(string $facetId, $selectedBuckets) + public function __set(string $facetId, string $selectedBuckets): void { $this->selectedBuckets[$facetId] = $selectedBuckets; } @@ -68,7 +68,7 @@ public function valid(): bool { $key = key($this->selectedBuckets); - return null !== $key && false !== $key; + return null !== $key; } /** diff --git a/src/PropertyBuilder/AttributeBuilder.php b/src/PropertyBuilder/AttributeBuilder.php index 540113ba..8b60284e 100644 --- a/src/PropertyBuilder/AttributeBuilder.php +++ b/src/PropertyBuilder/AttributeBuilder.php @@ -49,22 +49,25 @@ function (ProductInterface $product, Document $document): void { private function resolveProductAttributes(ProductInterface $product, Document $document): void { + /** @var ProductAttributeValue $productAttribute */ foreach ($product->getAttributes() as $productAttribute) { $attribute = $productAttribute->getAttribute(); - if (!$attribute) { + if (null === $attribute) { continue; } - $this->processAttribute($attribute, $productAttribute, $document); } } private function resolveProductAttribute( array $attributeConfiguration, - $attributeValue, + mixed $attributeValue, ProductAttributeValue $productAttribute ): array { - if (SelectAttributeType::TYPE === $productAttribute->getAttribute()->getType()) { + /** @var AttributeInterface $attribute */ + $attribute = $productAttribute->getAttribute(); + + if (SelectAttributeType::TYPE === $attribute->getType()) { $choices = $attributeConfiguration['choices']; if (is_array($attributeValue)) { foreach ($attributeValue as $i => $item) { @@ -87,8 +90,8 @@ private function resolveProductAttribute( break; case $attributeValue instanceof \DateTime: - $attributeFormat = $productAttribute->getAttribute()->getConfiguration()['format'] ?? null; - $defaultFormat = DateAttributeType::TYPE === $productAttribute->getAttribute()->getStorageType() ? + $attributeFormat = $attribute->getConfiguration()['format'] ?? null; + $defaultFormat = DateAttributeType::TYPE === $attribute->getStorageType() ? self::DEFAULT_DATE_FORMAT : self::DEFAULT_DATE_TIME_FORMAT ; @@ -109,6 +112,7 @@ private function processAttribute( ProductAttributeValue $productAttribute, Document $document ): void { + /** @var string $attributeCode */ $attributeCode = $attribute->getCode(); $attributeConfiguration = $attribute->getConfiguration(); @@ -122,7 +126,7 @@ private function processAttribute( $productAttribute ); - $values = in_array($attribute->getStorageType(), [DateAttributeType::TYPE, DatetimeAttributeType::TYPE]) ? + $values = in_array($attribute->getStorageType(), [DateAttributeType::TYPE, DatetimeAttributeType::TYPE], true) ? ($values[0] ?? $values) : $values ; diff --git a/src/PropertyBuilder/AttributeTaxonsBuilder.php b/src/PropertyBuilder/AttributeTaxonsBuilder.php index 9f087a7c..009168dc 100644 --- a/src/PropertyBuilder/AttributeTaxonsBuilder.php +++ b/src/PropertyBuilder/AttributeTaxonsBuilder.php @@ -34,7 +34,7 @@ public function consumeEvent(PostTransformEvent $event): void if (!$documentAttribute instanceof AttributeInterface || !$documentAttribute instanceof ProductAttributeInterface - || in_array($documentAttribute->getCode(), $this->excludedAttributes) + || in_array($documentAttribute->getCode(), $this->excludedAttributes, true) ) { return; } diff --git a/src/PropertyBuilder/ChannelPricingBuilder.php b/src/PropertyBuilder/ChannelPricingBuilder.php index 0c98d253..ab557845 100644 --- a/src/PropertyBuilder/ChannelPricingBuilder.php +++ b/src/PropertyBuilder/ChannelPricingBuilder.php @@ -39,8 +39,11 @@ function (ProductInterface $product, Document $document): void { $productVariant = $product->getVariants()->first(); foreach ($productVariant->getChannelPricings() as $channelPricing) { + /** @var string $channelCode */ + $channelCode = $channelPricing->getChannelCode(); + $propertyName = $this->channelPricingNameResolver - ->resolvePropertyName($channelPricing->getChannelCode()); + ->resolvePropertyName($channelCode); $document->set($propertyName, $channelPricing->getPrice()); } diff --git a/src/PropertyBuilder/OptionBuilder.php b/src/PropertyBuilder/OptionBuilder.php index acc6f6ce..6c19b757 100644 --- a/src/PropertyBuilder/OptionBuilder.php +++ b/src/PropertyBuilder/OptionBuilder.php @@ -17,6 +17,7 @@ use Elastica\Document; use FOS\ElasticaBundle\Event\PostTransformEvent; use Sylius\Component\Core\Model\ProductInterface; +use Sylius\Component\Product\Model\ProductOptionInterface; final class OptionBuilder extends AbstractBuilder { @@ -41,10 +42,18 @@ private function resolveProductOptions(ProductInterface $product, Document $docu { foreach ($product->getVariants() as $productVariant) { foreach ($productVariant->getOptionValues() as $productOptionValue) { - $optionCode = $productOptionValue->getOption()->getCode(); + /** @var ProductOptionInterface $option */ + $option = $productOptionValue->getOption(); + + /** @var string $optionCode */ + $optionCode = $option->getCode(); + + /** @var string $value */ + $value = $productOptionValue->getValue(); + $index = $this->optionNameResolver->resolvePropertyName($optionCode); $options = $document->has($index) ? $document->get($index) : []; - $value = $this->stringFormatter->formatToLowercaseWithoutSpaces($productOptionValue->getValue()); + $value = $this->stringFormatter->formatToLowercaseWithoutSpaces($value); $options[] = $value; $document->set($index, array_values(array_unique($options))); diff --git a/src/PropertyBuilder/ProductCreatedAtPropertyBuilder.php b/src/PropertyBuilder/ProductCreatedAtPropertyBuilder.php index cf844413..e53d43ff 100644 --- a/src/PropertyBuilder/ProductCreatedAtPropertyBuilder.php +++ b/src/PropertyBuilder/ProductCreatedAtPropertyBuilder.php @@ -12,6 +12,7 @@ namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder; +use ECSPrefix20220606\Webmozart\Assert\Assert; use Elastica\Document; use FOS\ElasticaBundle\Event\PostTransformEvent; use Sylius\Component\Core\Model\ProductInterface; @@ -29,6 +30,7 @@ public function consumeEvent(PostTransformEvent $event): void $event, ProductInterface::class, function (ProductInterface $product, Document $document): void { + Assert::notNull($product->getCreatedAt()); $createdAt = (int) $product->getCreatedAt()->format('U'); $document->set($this->createdAtProperty, $createdAt); diff --git a/src/PropertyBuilder/ProductDescriptionBuilder.php b/src/PropertyBuilder/ProductDescriptionBuilder.php index 37d92e09..060e16e6 100644 --- a/src/PropertyBuilder/ProductDescriptionBuilder.php +++ b/src/PropertyBuilder/ProductDescriptionBuilder.php @@ -33,8 +33,11 @@ public function consumeEvent(PostTransformEvent $event): void function (ProductInterface $product, Document $document): void { /** @var ProductTranslationInterface $productTranslation */ foreach ($product->getTranslations() as $productTranslation) { + /** @var string $locale */ + $locale = $productTranslation->getLocale(); + $propertyName = $this->productDescriptionNameResolver->resolvePropertyName( - $productTranslation->getLocale() + $locale ); $document->set($propertyName, $productTranslation->getDescription()); } diff --git a/src/PropertyBuilder/ProductNameBuilder.php b/src/PropertyBuilder/ProductNameBuilder.php index 97328762..71a306fc 100644 --- a/src/PropertyBuilder/ProductNameBuilder.php +++ b/src/PropertyBuilder/ProductNameBuilder.php @@ -33,8 +33,11 @@ public function consumeEvent(PostTransformEvent $event): void function (ProductInterface $product, Document $document): void { /** @var ProductTranslationInterface $productTranslation */ foreach ($product->getTranslations() as $productTranslation) { + /** @var string $locale */ + $locale = $productTranslation->getLocale(); + $propertyName = $this->productNameNameResolver - ->resolvePropertyName($productTranslation->getLocale()); + ->resolvePropertyName($locale); $document->set($propertyName, $productTranslation->getName()); } diff --git a/src/PropertyBuilder/ProductShortDescriptionBuilder.php b/src/PropertyBuilder/ProductShortDescriptionBuilder.php index 158be691..8e247567 100644 --- a/src/PropertyBuilder/ProductShortDescriptionBuilder.php +++ b/src/PropertyBuilder/ProductShortDescriptionBuilder.php @@ -33,8 +33,11 @@ public function consumeEvent(PostTransformEvent $event): void function (ProductInterface $product, Document $document): void { /** @var ProductTranslationInterface $productTranslation */ foreach ($product->getTranslations() as $productTranslation) { + /** @var string $locale */ + $locale = $productTranslation->getLocale(); + $propertyName = $this->productShortDescriptionNameResolver->resolvePropertyName( - $productTranslation->getLocale() + $locale ); $document->set($propertyName, $productTranslation->getShortDescription()); } diff --git a/src/PropertyBuilder/ProductTaxonPositionPropertyBuilder.php b/src/PropertyBuilder/ProductTaxonPositionPropertyBuilder.php index b88fd649..ca820ced 100644 --- a/src/PropertyBuilder/ProductTaxonPositionPropertyBuilder.php +++ b/src/PropertyBuilder/ProductTaxonPositionPropertyBuilder.php @@ -16,6 +16,7 @@ use Elastica\Document; use FOS\ElasticaBundle\Event\PostTransformEvent; use Sylius\Component\Core\Model\ProductInterface; +use Sylius\Component\Core\Model\TaxonInterface; final class ProductTaxonPositionPropertyBuilder extends AbstractBuilder { @@ -30,10 +31,16 @@ public function consumeEvent(PostTransformEvent $event): void $event, ProductInterface::class, function (ProductInterface $product, Document $document): void { - foreach ($product->getProductTaxons() as $taxon) { + foreach ($product->getProductTaxons() as $productTaxon) { + /** @var TaxonInterface $taxon */ + $taxon = $productTaxon->getTaxon(); + + /** @var string $code */ + $code = $taxon->getCode(); + $document->set( - $this->taxonPositionNameResolver->resolvePropertyName($taxon->getTaxon()->getCode()), - $taxon->getPosition() + $this->taxonPositionNameResolver->resolvePropertyName($code), + $productTaxon->getPosition() ); } } diff --git a/src/PropertyBuilder/SoldUnitsPropertyBuilder.php b/src/PropertyBuilder/SoldUnitsPropertyBuilder.php index b74682d1..aaeceba8 100644 --- a/src/PropertyBuilder/SoldUnitsPropertyBuilder.php +++ b/src/PropertyBuilder/SoldUnitsPropertyBuilder.php @@ -16,6 +16,7 @@ use Elastica\Document; use FOS\ElasticaBundle\Event\PostTransformEvent; use Sylius\Component\Core\Model\ProductInterface; +use Sylius\Component\Core\Model\ProductVariantInterface; final class SoldUnitsPropertyBuilder extends AbstractBuilder { @@ -33,6 +34,7 @@ public function consumeEvent(PostTransformEvent $event): void function (ProductInterface $product, Document $document): void { $soldUnits = 0; + /** @var ProductVariantInterface $productVariant */ foreach ($product->getVariants() as $productVariant) { $soldUnits += $this->orderItemRepository->countByVariant($productVariant); } diff --git a/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeDateQueryBuilder.php b/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeDateQueryBuilder.php index 357869b6..5e2758b1 100644 --- a/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeDateQueryBuilder.php +++ b/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeDateQueryBuilder.php @@ -24,7 +24,7 @@ class AttributesTypeDateQueryBuilder implements AttributesQueryBuilderCollectorI public function supports(string $type): bool { - return in_array($type, self::AVAILABLE_ATTRIBUTES_TYPE); + return in_array($type, self::AVAILABLE_ATTRIBUTES_TYPE, true); } public function buildQuery(array $data, string $localCode): BoolQuery diff --git a/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeNumberQueryBuilder.php b/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeNumberQueryBuilder.php index 3af17e03..1c4d5937 100644 --- a/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeNumberQueryBuilder.php +++ b/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeNumberQueryBuilder.php @@ -25,7 +25,7 @@ class AttributesTypeNumberQueryBuilder implements AttributesQueryBuilderCollecto public function supports(string $type): bool { - return in_array($type, self::AVAILABLE_ATTRIBUTES_TYPE); + return in_array($type, self::AVAILABLE_ATTRIBUTES_TYPE, true); } public function buildQuery(array $data, string $localCode): BoolQuery diff --git a/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeTextQueryBuilder.php b/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeTextQueryBuilder.php index cc3cc9e8..02e8bc61 100644 --- a/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeTextQueryBuilder.php +++ b/src/QueryBuilder/AttributesQueryBuilder/AttributesTypeTextQueryBuilder.php @@ -25,7 +25,7 @@ class AttributesTypeTextQueryBuilder implements AttributesQueryBuilderCollectorI public function supports(string $type): bool { - return in_array($type, self::AVAILABLE_ATTRIBUTES_TYPE); + return in_array($type, self::AVAILABLE_ATTRIBUTES_TYPE, true); } public function buildQuery(array $data, string $localCode): BoolQuery diff --git a/src/QueryBuilder/FormQueryBuilder/SiteWideFacetsQueryBuilder.php b/src/QueryBuilder/FormQueryBuilder/SiteWideFacetsQueryBuilder.php index eb563524..81921303 100644 --- a/src/QueryBuilder/FormQueryBuilder/SiteWideFacetsQueryBuilder.php +++ b/src/QueryBuilder/FormQueryBuilder/SiteWideFacetsQueryBuilder.php @@ -16,6 +16,7 @@ use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface; use Elastica\Query; use Symfony\Component\Form\FormEvent; +use Webmozart\Assert\Assert; final class SiteWideFacetsQueryBuilder implements SiteWideFacetsQueryBuilderInterface { @@ -29,12 +30,16 @@ public function getQuery(FormEvent $event): Query { /** @var Search $data */ $data = $event->getData(); + $box = $data->getBox(); + Assert::notNull($box); + + /** @var Query\BoolQuery $boolQuery */ $boolQuery = $this->queryBuilder->buildQuery([ - 'query' => $data['box']['query'] ?? '', + 'query' => $box->getQuery() ?? '', ]); - foreach ($data['facets'] ?? [] as $facetId => $selectedBuckets) { + foreach ($data->getFacets() as $facetId => $selectedBuckets) { if (!$selectedBuckets) { continue; } diff --git a/src/QueryBuilder/FormQueryBuilder/TaxonFacetsQueryBuilder.php b/src/QueryBuilder/FormQueryBuilder/TaxonFacetsQueryBuilder.php index fc4e1a03..3378c9de 100644 --- a/src/QueryBuilder/FormQueryBuilder/TaxonFacetsQueryBuilder.php +++ b/src/QueryBuilder/FormQueryBuilder/TaxonFacetsQueryBuilder.php @@ -34,6 +34,7 @@ public function getQuery(FormEvent $event, string $namePropertyPrefix): Query $data = $this->shopProductListDataHandler->retrieveData($eventData); + /** @var Query\BoolQuery $boolQuery */ $boolQuery = $this->searchProductsQueryBuilder->buildQuery($data); foreach ($data['facets'] ?? [] as $facetId => $selectedBuckets) { diff --git a/src/QueryBuilder/HasChannelQueryBuilder.php b/src/QueryBuilder/HasChannelQueryBuilder.php index 99d39eb3..e09cdc40 100644 --- a/src/QueryBuilder/HasChannelQueryBuilder.php +++ b/src/QueryBuilder/HasChannelQueryBuilder.php @@ -27,7 +27,9 @@ public function __construct( public function buildQuery(array $data): ?AbstractQuery { $channelQuery = new Terms($this->channelsProperty); - $channelQuery->setTerms([strtolower($this->channelContext->getChannel()->getCode())]); + /** @var string $channelCode */ + $channelCode = $this->channelContext->getChannel()->getCode(); + $channelQuery->setTerms([strtolower($channelCode)]); return $channelQuery; } diff --git a/src/QueryBuilder/HasPriceBetweenQueryBuilder.php b/src/QueryBuilder/HasPriceBetweenQueryBuilder.php index 4efdb367..20f51abf 100644 --- a/src/QueryBuilder/HasPriceBetweenQueryBuilder.php +++ b/src/QueryBuilder/HasPriceBetweenQueryBuilder.php @@ -22,6 +22,7 @@ use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Currency\Context\CurrencyContextInterface; use Sylius\Component\Currency\Converter\CurrencyConverterInterface; +use Webmozart\Assert\Assert; final class HasPriceBetweenQueryBuilder implements QueryBuilderInterface { @@ -39,9 +40,10 @@ public function buildQuery(array $data): ?AbstractQuery $dataMinPrice = $this->getDataByKey($data, $this->priceNameResolver->resolveMinPriceName()); $dataMaxPrice = $this->getDataByKey($data, $this->priceNameResolver->resolveMaxPriceName()); - $minPrice = $dataMinPrice ? $this->resolveBasePrice($dataMinPrice) : null; - $maxPrice = $dataMaxPrice ? $this->resolveBasePrice($dataMaxPrice) : null; + $minPrice = (null !== $dataMinPrice) ? $this->resolveBasePrice($dataMinPrice) : null; + $maxPrice = (null !== $dataMaxPrice) ? $this->resolveBasePrice($dataMaxPrice) : null; + /** @var string $channelCode */ $channelCode = $this->channelContext->getChannel()->getCode(); $propertyName = $this->channelPricingNameResolver->resolvePropertyName($channelCode); $rangeQuery = new Range(); @@ -62,8 +64,13 @@ private function resolveBasePrice(string $price): int $price = $this->convertFromString($price); /** @var ChannelInterface $channel */ $channel = $this->channelContext->getChannel(); + $channelBaseCurrency = $channel->getBaseCurrency(); + + Assert::notNull($channelBaseCurrency); + $currentCurrencyCode = $this->currencyContext->getCurrencyCode(); - $channelBaseCurrencyCode = $channel->getBaseCurrency()->getCode(); + /** @var string $channelBaseCurrencyCode */ + $channelBaseCurrencyCode = $channelBaseCurrency->getCode(); if ($currentCurrencyCode !== $channelBaseCurrencyCode) { $price = $this->currencyConverter->convert($price, $currentCurrencyCode, $channelBaseCurrencyCode); @@ -80,7 +87,10 @@ private function convertFromString(string $price): int $transformer = new SyliusMoneyTransformer(2, false, NumberFormatter::ROUND_HALFUP, 100); - return $transformer->reverseTransform($price); + /** @var int $convertedPrice */ + $convertedPrice = $transformer->reverseTransform($price); + + return $convertedPrice; } private function getDataByKey(array $data, ?string $key = null): ?string @@ -90,6 +100,7 @@ private function getDataByKey(array $data, ?string $key = null): ?string private function getQueryParamValue(?int $min, ?int $max): ?array { + $paramValue = null; foreach (['gte' => $min, 'lte' => $max] as $key => $value) { if (null !== $value) { $paramValue[$key] = $value; diff --git a/src/QueryBuilder/ProductAttributesByTaxonQueryBuilder.php b/src/QueryBuilder/ProductAttributesByTaxonQueryBuilder.php index afde00fc..0c5c3ce9 100644 --- a/src/QueryBuilder/ProductAttributesByTaxonQueryBuilder.php +++ b/src/QueryBuilder/ProductAttributesByTaxonQueryBuilder.php @@ -14,6 +14,7 @@ use Elastica\Query\AbstractQuery; use Elastica\Query\BoolQuery; +use Webmozart\Assert\Assert; final class ProductAttributesByTaxonQueryBuilder implements QueryBuilderInterface { @@ -27,6 +28,8 @@ public function buildQuery(array $data): ?AbstractQuery $boolQuery = new BoolQuery(); $taxonQuery = $this->hasTaxonQueryBuilder->buildQuery($data); + Assert::notNull($taxonQuery); + $boolQuery->addMust($taxonQuery); return $boolQuery; diff --git a/src/QueryBuilder/ProductOptionsByTaxonQueryBuilder.php b/src/QueryBuilder/ProductOptionsByTaxonQueryBuilder.php index 42c87cec..8d36c970 100644 --- a/src/QueryBuilder/ProductOptionsByTaxonQueryBuilder.php +++ b/src/QueryBuilder/ProductOptionsByTaxonQueryBuilder.php @@ -12,6 +12,7 @@ namespace BitBag\SyliusElasticsearchPlugin\QueryBuilder; +use ECSPrefix20220606\Webmozart\Assert\Assert; use Elastica\Query\AbstractQuery; use Elastica\Query\BoolQuery; @@ -27,6 +28,8 @@ public function buildQuery(array $data): ?AbstractQuery $boolQuery = new BoolQuery(); $taxonQuery = $this->hasTaxonQueryBuilder->buildQuery($data); + Assert::notNull($taxonQuery); + $boolQuery->addMust($taxonQuery); return $boolQuery; diff --git a/src/QueryBuilder/SiteWideProductsQueryBuilder.php b/src/QueryBuilder/SiteWideProductsQueryBuilder.php index 0b83d2c5..6b14e572 100644 --- a/src/QueryBuilder/SiteWideProductsQueryBuilder.php +++ b/src/QueryBuilder/SiteWideProductsQueryBuilder.php @@ -14,6 +14,7 @@ use Elastica\Query\AbstractQuery; use Elastica\Query\BoolQuery; +use Webmozart\Assert\Assert; final class SiteWideProductsQueryBuilder implements QueryBuilderInterface { @@ -28,8 +29,14 @@ public function buildQuery(array $data): ?AbstractQuery { $boolQuery = new BoolQuery(); - $boolQuery->addMust($this->isEnabledQueryBuilder->buildQuery([])); - $boolQuery->addMust($this->hasChannelQueryBuilder->buildQuery([])); + $isEnabledQuery = $this->isEnabledQueryBuilder->buildQuery([]); + $hasChannelQuery = $this->hasChannelQueryBuilder->buildQuery([]); + + Assert::notNull($isEnabledQuery); + Assert::notNull($hasChannelQuery); + + $boolQuery->addMust($isEnabledQuery); + $boolQuery->addMust($hasChannelQuery); $nameQuery = $this->containsNameQueryBuilder->buildQuery($data); $this->addMustIfNotNull($nameQuery, $boolQuery); diff --git a/src/QueryBuilder/TaxonProductsQueryBuilder.php b/src/QueryBuilder/TaxonProductsQueryBuilder.php index d81132d9..cc562af5 100644 --- a/src/QueryBuilder/TaxonProductsQueryBuilder.php +++ b/src/QueryBuilder/TaxonProductsQueryBuilder.php @@ -14,6 +14,7 @@ use Elastica\Query\AbstractQuery; use Elastica\Query\BoolQuery; +use Webmozart\Assert\Assert; final class TaxonProductsQueryBuilder implements QueryBuilderInterface { @@ -34,8 +35,14 @@ public function buildQuery(array $data): AbstractQuery { $boolQuery = new BoolQuery(); - $boolQuery->addMust($this->isEnabledQueryBuilder->buildQuery($data)); - $boolQuery->addMust($this->hasChannelQueryBuilder->buildQuery($data)); + $isEnabledQuery = $this->isEnabledQueryBuilder->buildQuery($data); + $hasChannelQuery = $this->hasChannelQueryBuilder->buildQuery($data); + + Assert::notNull($isEnabledQuery); + Assert::notNull($hasChannelQuery); + + $boolQuery->addMust($isEnabledQuery); + $boolQuery->addMust($hasChannelQuery); $nameQuery = $this->containsNameQueryBuilder->buildQuery($data); $this->addMustIfNotNull($nameQuery, $boolQuery); @@ -57,7 +64,9 @@ private function resolveOptionQuery(BoolQuery $boolQuery, array $data): void foreach ($data as $key => $value) { if (0 === strpos($key, $this->optionPropertyPrefix) && 0 < count($value)) { $optionQuery = $this->hasOptionsQueryBuilder->buildQuery(['option' => $key, 'option_values' => $value]); - $boolQuery->addMust($optionQuery); + if (null !== $optionQuery) { + $boolQuery->addMust($optionQuery); + } } } } @@ -67,7 +76,9 @@ private function resolveAttributeQuery(BoolQuery $boolQuery, array $data): void foreach ($data as $key => $value) { if (0 === strpos($key, $this->attributePropertyPrefix) && 0 < count($value)) { $optionQuery = $this->hasAttributesQueryBuilder->buildQuery(['attribute' => $key, 'attribute_values' => $value]); - $boolQuery->addMust($optionQuery); + if (null !== $optionQuery) { + $boolQuery->addMust($optionQuery); + } } } } diff --git a/src/Repository/OrderItemRepository.php b/src/Repository/OrderItemRepository.php index 5ff4dfe1..6c263411 100644 --- a/src/Repository/OrderItemRepository.php +++ b/src/Repository/OrderItemRepository.php @@ -25,11 +25,14 @@ public function __construct( public function countByVariant(ProductVariantInterface $variant, array $orderStates = []): int { - if (empty($orderStates)) { + if ([] === $orderStates) { $orderStates = [OrderInterface::STATE_CANCELLED, OrderInterface::STATE_CART]; } - return (int) ($this->baseOrderItemRepository + /** @var EntityRepository $baseOrderItemRepository */ + $baseOrderItemRepository = $this->baseOrderItemRepository; + + return (int) ($baseOrderItemRepository ->createQueryBuilder('oi') ->select('SUM(oi.quantity)') ->join('oi.order', 'o') diff --git a/src/Repository/ProductAttributeRepository.php b/src/Repository/ProductAttributeRepository.php index 2255c193..b8656a3b 100644 --- a/src/Repository/ProductAttributeRepository.php +++ b/src/Repository/ProductAttributeRepository.php @@ -12,20 +12,22 @@ namespace BitBag\SyliusElasticsearchPlugin\Repository; -use Doctrine\ORM\QueryBuilder; +use Doctrine\ORM\EntityRepository; use Sylius\Component\Resource\Repository\RepositoryInterface; class ProductAttributeRepository implements ProductAttributeRepositoryInterface { public function __construct( - private RepositoryInterface $productAttributeRepository + private RepositoryInterface|EntityRepository $productAttributeRepository ) { } public function getAttributeTypeByName(string $attributeName): string { - /** @var QueryBuilder $queryBuilder */ - $queryBuilder = $this->productAttributeRepository->createQueryBuilder('p'); + /** @var EntityRepository $productAttributeRepository */ + $productAttributeRepository = $this->productAttributeRepository; + + $queryBuilder = $productAttributeRepository->createQueryBuilder('p'); $result = $queryBuilder ->select('p.type') @@ -39,13 +41,15 @@ public function getAttributeTypeByName(string $attributeName): string public function findAllWithTranslations(?string $locale): array { - /** @var QueryBuilder $queryBuilder */ - $queryBuilder = $this->productAttributeRepository->createQueryBuilder('o'); + /** @var EntityRepository $productAttributeRepository */ + $productAttributeRepository = $this->productAttributeRepository; + + $queryBuilder = $productAttributeRepository->createQueryBuilder('p'); if (null !== $locale) { $queryBuilder ->addSelect('translation') - ->leftJoin('o.translations', 'translation', 'ot') + ->leftJoin('o.translations', 'ot') ->andWhere('translation.locale = :locale') ->setParameter('locale', $locale) ; diff --git a/src/Repository/ProductAttributeValueRepository.php b/src/Repository/ProductAttributeValueRepository.php index 4e0730ad..8cf3d53d 100644 --- a/src/Repository/ProductAttributeValueRepository.php +++ b/src/Repository/ProductAttributeValueRepository.php @@ -12,6 +12,7 @@ namespace BitBag\SyliusElasticsearchPlugin\Repository; +use Doctrine\ORM\EntityRepository; use Sylius\Component\Attribute\Model\AttributeInterface; use Sylius\Component\Product\Repository\ProductAttributeValueRepositoryInterface as BaseAttributeValueRepositoryInterface; use Sylius\Component\Taxonomy\Model\Taxon; @@ -19,14 +20,17 @@ class ProductAttributeValueRepository implements ProductAttributeValueRepositoryInterface { public function __construct( - private BaseAttributeValueRepositoryInterface $baseAttributeValueRepository, + private BaseAttributeValueRepositoryInterface|EntityRepository $baseAttributeValueRepository, private bool $includeAllDescendants ) { } public function getUniqueAttributeValues(AttributeInterface $productAttribute, Taxon $taxon): array { - $queryBuilder = $this->baseAttributeValueRepository->createQueryBuilder('o'); + /** @var EntityRepository $baseAttributeValueRepository */ + $baseAttributeValueRepository = $this->baseAttributeValueRepository; + + $queryBuilder = $baseAttributeValueRepository->createQueryBuilder('o'); /** @var string|null $storageType */ $storageType = $productAttribute->getStorageType(); diff --git a/src/Repository/ProductOptionRepository.php b/src/Repository/ProductOptionRepository.php index df513c81..4f83c075 100644 --- a/src/Repository/ProductOptionRepository.php +++ b/src/Repository/ProductOptionRepository.php @@ -12,25 +12,27 @@ namespace BitBag\SyliusElasticsearchPlugin\Repository; -use Doctrine\ORM\QueryBuilder; +use Doctrine\ORM\EntityRepository; use Sylius\Component\Resource\Repository\RepositoryInterface; class ProductOptionRepository implements ProductOptionRepositoryInterface { public function __construct( - private RepositoryInterface $productOptionRepository + private RepositoryInterface|EntityRepository $productOptionRepository ) { } public function findAllWithTranslations(?string $locale): array { - /** @var QueryBuilder $queryBuilder */ - $queryBuilder = $this->productOptionRepository->createQueryBuilder('o'); + /** @var EntityRepository $productOptionRepository */ + $productOptionRepository = $this->productOptionRepository; + + $queryBuilder = $productOptionRepository->createQueryBuilder('o'); if (null !== $locale) { $queryBuilder ->addSelect('translation') - ->leftJoin('o.translations', 'translation', 'ot') + ->leftJoin('o.translations', 'ot') ->andWhere('translation.locale = :locale') ->setParameter('locale', $locale) ; diff --git a/src/Repository/ProductVariantRepository.php b/src/Repository/ProductVariantRepository.php index c050f9bf..e84fc109 100644 --- a/src/Repository/ProductVariantRepository.php +++ b/src/Repository/ProductVariantRepository.php @@ -26,7 +26,10 @@ public function __construct( public function findOneByOptionValue(ProductOptionValueInterface $productOptionValue): ?ProductVariantInterface { - return $this->baseProductVariantRepository->createQueryBuilder('o') + /** @var EntityRepository $baseProductVariantRepository */ + $baseProductVariantRepository = $this->baseProductVariantRepository; + + return $baseProductVariantRepository->createQueryBuilder('o') ->where(':optionValue MEMBER OF o.optionValues') ->setParameter('optionValue', $productOptionValue) ->getQuery() @@ -37,7 +40,10 @@ public function findOneByOptionValue(ProductOptionValueInterface $productOptionV public function findByOptionValue(ProductOptionValueInterface $productOptionValue): array { - return $this->baseProductVariantRepository->createQueryBuilder('o') + /** @var EntityRepository $baseProductVariantRepository */ + $baseProductVariantRepository = $this->baseProductVariantRepository; + + return $baseProductVariantRepository->createQueryBuilder('o') ->where(':optionValue MEMBER OF o.optionValues') ->setParameter('optionValue', $productOptionValue) ->getQuery() diff --git a/src/Repository/TaxonRepository.php b/src/Repository/TaxonRepository.php index 07de826b..76f9a0f2 100644 --- a/src/Repository/TaxonRepository.php +++ b/src/Repository/TaxonRepository.php @@ -30,15 +30,20 @@ public function __construct( public function getTaxonsByAttributeViaProduct(AttributeInterface $attribute): array { - return $this->baseTaxonRepository + /** @var EntityRepository $taxonRepository */ + $taxonRepository = $this->baseTaxonRepository; + + /** @var EntityRepository $productRepository */ + $productRepository = $this->productRepository; + + return $taxonRepository ->createQueryBuilder('t') ->distinct(true) ->select('t') ->leftJoin($this->productTaxonEntityClass, 'pt', Join::WITH, 'pt.taxon = t.id') ->where( 'pt.product IN(' . - $this - ->productRepository->createQueryBuilder('p') + $productRepository->createQueryBuilder('p') ->leftJoin($this->productAttributeEntityClass, 'pav', Join::WITH, 'pav.subject = p.id') ->where('pav.attribute = :attribute') ->getQuery() diff --git a/src/Transformer/Product/ChannelPricingTransformer.php b/src/Transformer/Product/ChannelPricingTransformer.php index 084da818..682cc2c2 100644 --- a/src/Transformer/Product/ChannelPricingTransformer.php +++ b/src/Transformer/Product/ChannelPricingTransformer.php @@ -20,6 +20,7 @@ use Sylius\Component\Core\Model\ProductVariantInterface; use Sylius\Component\Locale\Context\LocaleContextInterface; use Sylius\Component\Product\Resolver\ProductVariantResolverInterface; +use Webmozart\Assert\Assert; final class ChannelPricingTransformer implements TransformerInterface { @@ -33,7 +34,7 @@ public function __construct( public function transform(ProductInterface $product): ?string { - /** @var ChannelInterface|null $channel */ + /** @var ChannelInterface $channel */ $channel = $this->channelContext->getChannel(); if (null === $channelBaseCurrency = $channel->getBaseCurrency()) { @@ -53,6 +54,9 @@ public function transform(ProductInterface $product): ?string return null; } + Assert::integer($productVariantPricing->getPrice()); + Assert::string($channelBaseCurrency->getCode()); + return $this->moneyFormatter->format( $productVariantPricing->getPrice(), $channelBaseCurrency->getCode(), diff --git a/src/Transformer/Product/ImageTransformer.php b/src/Transformer/Product/ImageTransformer.php index 2cfdfbe7..2f793dcd 100644 --- a/src/Transformer/Product/ImageTransformer.php +++ b/src/Transformer/Product/ImageTransformer.php @@ -39,8 +39,11 @@ public function transform(ProductInterface $product): ?string /** @var ImageInterface $productImage */ $productImage = $productThumbnails->first(); - if ($this->canImageBeFiltered($productImage->getPath())) { - return $this->imagineFilter->getUrlOfFilteredImage($productImage->getPath(), self::SYLIUS_THUMBNAIL_FILTER); + /** @var string $path */ + $path = $productImage->getPath(); + + if ($this->canImageBeFiltered($path)) { + return $this->imagineFilter->getUrlOfFilteredImage($path, self::SYLIUS_THUMBNAIL_FILTER); } return $this->imagesPath . $productImage->getPath();