Skip to content

Commit

Permalink
OP-352 - Apply phpstan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonKostrubiec committed Jul 9, 2024
1 parent 855e5da commit c0863ec
Show file tree
Hide file tree
Showing 55 changed files with 246 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/Api/Resolver/FacetsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function resolve(array $data): array
}

return array_filter($adapter->getAggregations(), function ($facet) {
return [] !== $facet['buckets'] ?? [];
return [] !== $facet['buckets'];
});
}
}
7 changes: 6 additions & 1 deletion src/Context/TaxonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
9 changes: 5 additions & 4 deletions src/Controller/Action/Api/ListProductsByPartialNameAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
));
Expand Down
9 changes: 8 additions & 1 deletion src/Controller/Action/Shop/AbstractSearchAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, '[]'));

Expand Down
6 changes: 5 additions & 1 deletion src/Controller/Action/Shop/SiteWideProductsSearchAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/Response/DTO/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ?? '',
];
}
}
6 changes: 5 additions & 1 deletion src/EventListener/OrderProductsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
}
6 changes: 5 additions & 1 deletion src/EventListener/ProductTaxonIndexListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
4 changes: 4 additions & 0 deletions src/EventListener/ResourceIndexListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Facet/AttributeFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Facet/OptionFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
4 changes: 2 additions & 2 deletions src/Facet/PriceFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/Finder/NamedProductsFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/Finder/ProductAttributesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/Finder/ProductOptionsFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/Finder/ShopProductsFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/AbstractFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function configureOptions(OptionsResolver $resolver): void
]);
}

public function getBlockPrefix(): ?string
public function getBlockPrefix(): string
{
return '';
}
Expand Down
2 changes: 2 additions & 0 deletions src/Form/Type/ChoiceMapper/ProductAttributesMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/Form/Type/ChoiceMapper/ProductOptionsMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/PriceFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/Form/Type/ProductAttributesFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Form/Type/ProductOptionsFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit c0863ec

Please sign in to comment.