Skip to content

Commit 3fa4b54

Browse files
committed
fix: phpmd errors
1 parent c998a52 commit 3fa4b54

30 files changed

+322
-161
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ test.phpstan: ## Run PHPStan
117117
${COMPOSER} phpstan
118118

119119
test.phpmd: ## Run PHPMD
120-
${COMPOSER} phpmd || true
120+
${COMPOSER} phpmd
121121

122122
test.phpunit: ## Run PHPUnit
123123
${COMPOSER} phpunit

phpmd.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<rule ref="rulesets/naming.xml/ShortClassName"/>
4545
<rule ref="rulesets/naming.xml/ShortVariable">
4646
<properties>
47-
<property name="exceptions" value="id,em,qb"/>
47+
<property name="exceptions" value="id,em,qb,io"/>
4848
</properties>
4949
</rule>
5050
<rule ref="rulesets/naming.xml/ShortMethodName"/>

src/AutoMapper/ProductAttributeValueConfiguration.php

+20-14
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,7 @@ public function process(MapperGeneratorMetadataInterface $metadata): void
5252
throw new RuntimeException('Undefined product attribute value reader');
5353
}
5454

55-
$metadata->forMember('value', function (ProductAttributeValueInterface $productAttributeValue) {
56-
if (null === $productAttributeValue->getType()) {
57-
return null;
58-
}
59-
if (!\array_key_exists($productAttributeValue->getType(), $this->productAttributeValueReaders)) {
60-
// @phpstan-ignore-next-line The logger can't be null here
61-
$this->logger->alert(sprintf('Missing product attribute value reader for "%s" type', $productAttributeValue->getType()));
62-
63-
return null;
64-
}
65-
$reader = $this->productAttributeValueReaders[$productAttributeValue->getType()];
66-
67-
return $reader->getValue($productAttributeValue);
68-
});
55+
$metadata->forMember('value', [$this, 'getProductAttributeValue']);
6956
}
7057

7158
public function getSource(): string
@@ -77,4 +64,23 @@ public function getTarget(): string
7764
{
7865
return $this->configuration->getTargetClass('product_attribute');
7966
}
67+
68+
/**
69+
* @return array|string|null
70+
*/
71+
public function getProductAttributeValue(ProductAttributeValueInterface $productAttributeValue)
72+
{
73+
if (null === $productAttributeValue->getType()) {
74+
return null;
75+
}
76+
if (!\array_key_exists($productAttributeValue->getType(), $this->productAttributeValueReaders)) {
77+
// @phpstan-ignore-next-line The logger can't be null here
78+
$this->logger->alert(sprintf('Missing product attribute value reader for "%s" type', $productAttributeValue->getType()));
79+
80+
return null;
81+
}
82+
$reader = $this->productAttributeValueReaders[$productAttributeValue->getType()];
83+
84+
return $reader->getValue($productAttributeValue);
85+
}
8086
}

src/AutoMapper/ProductAttributeValueReader/SelectReader.php

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function __construct(TranslationLocaleProviderInterface $localeProvider)
2525
$this->defaultLocaleCode = $localeProvider->getDefaultLocaleCode();
2626
}
2727

28+
/**
29+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
30+
*/
2831
public function getValue(ProductAttributeValueInterface $productAttribute)
2932
{
3033
if (null === $productAttribute->getAttribute()) {

src/AutoMapper/ProductMapperConfiguration.php

+101-80
Original file line numberDiff line numberDiff line change
@@ -118,104 +118,125 @@ public function process(MapperGeneratorMetadataInterface $metadata): void
118118
}, $product->getChannels()->toArray());
119119
});
120120

121-
$metadata->forMember('attributes', function (ProductInterface $product): array {
122-
$attributes = [];
123-
$currentLocale = $product->getTranslation()->getLocale();
124-
if (null === $currentLocale) {
125-
return $attributes;
126-
}
127-
$productAttributeDTOClass = $this->configuration->getTargetClass('product_attribute');
128-
foreach ($product->getAttributesByLocale($currentLocale, $currentLocale) as $attributeValue) {
129-
if (null === $attributeValue->getName() || null === $attributeValue->getValue()) {
130-
continue;
131-
}
132-
$attribute = $attributeValue->getAttribute();
133-
if (!$attribute instanceof SearchableInterface || (!$attribute->isSearchable() && !$attribute->isFilterable())) {
134-
continue;
135-
}
136-
$attributes[$attributeValue->getCode()] = $this->autoMapper->map($attributeValue, $productAttributeDTOClass);
137-
}
121+
$metadata->forMember('attributes', [$this, 'getAttributes']);
138122

139-
return $attributes;
140-
});
123+
$metadata->forMember('options', [$this, 'getOptions']);
141124

142-
$metadata->forMember('options', function (ProductInterface $product): array {
143-
$options = [];
144-
$currentLocale = $product->getTranslation()->getLocale();
145-
foreach ($product->getVariants() as $variant) {
146-
foreach ($variant->getOptionValues() as $optionValue) {
147-
if (null === $optionValue->getOption()) {
148-
continue;
149-
}
150-
if (!isset($options[$optionValue->getOptionCode()])) {
151-
$options[$optionValue->getOptionCode()] = [
152-
'name' => $optionValue->getOption()->getTranslation($currentLocale)->getName(),
153-
'values' => [],
154-
];
155-
}
156-
$isEnabled = ($options[$optionValue->getOptionCode()]['values'][$optionValue->getCode()]['enabled'] ?? false)
157-
|| $variant->isEnabled();
158-
// A variant option is considered to be in stock if the current option is enabled and is in stock
159-
$isInStock = ($options[$optionValue->getOptionCode()]['values'][$optionValue->getCode()]['is_in_stock'] ?? false)
160-
|| ($variant->isEnabled() && $this->isProductVariantInStock($variant));
161-
$options[$optionValue->getOptionCode()]['values'][$optionValue->getCode()] = [
162-
'value' => $optionValue->getTranslation($currentLocale)->getValue(),
163-
'enabled' => $isEnabled,
164-
'is_in_stock' => $isInStock,
165-
];
166-
}
167-
}
125+
$metadata->forMember('variants', [$this, 'getVariants']);
168126

169-
foreach ($options as $optionCode => $optionValues) {
170-
$options[$optionCode]['values'] = array_values($optionValues['values']);
171-
}
127+
$metadata->forMember('prices', [$this, 'getPrices']);
128+
}
172129

173-
return $options;
174-
});
130+
public function getSource(): string
131+
{
132+
return $this->configuration->getSourceClass('product');
133+
}
175134

176-
$metadata->forMember('variants', function (ProductInterface $product): array {
177-
$variants = [];
178-
$productVariantDTOClass = $this->configuration->getTargetClass('product_variant');
179-
foreach ($product->getEnabledVariants() as $variant) {
180-
$variants[] = $this->autoMapper->map($variant, $productVariantDTOClass);
181-
}
135+
public function getTarget(): string
136+
{
137+
return $this->configuration->getTargetClass('product');
138+
}
182139

183-
return $variants;
184-
});
140+
/**
141+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
142+
*/
143+
public function getAttributes(ProductInterface $product): array
144+
{
145+
$attributes = [];
146+
$currentLocale = $product->getTranslation()->getLocale();
147+
if (null === $currentLocale) {
148+
return $attributes;
149+
}
150+
$productAttributeDTOClass = $this->configuration->getTargetClass('product_attribute');
151+
foreach ($product->getAttributesByLocale($currentLocale, $currentLocale) as $attributeValue) {
152+
if (null === $attributeValue->getName() || null === $attributeValue->getValue()) {
153+
continue;
154+
}
155+
$attribute = $attributeValue->getAttribute();
156+
if (!$attribute instanceof SearchableInterface || (!$attribute->isSearchable() && !$attribute->isFilterable())) {
157+
continue;
158+
}
159+
$attributes[$attributeValue->getCode()] = $this->autoMapper->map($attributeValue, $productAttributeDTOClass);
160+
}
185161

186-
$metadata->forMember('prices', function (ProductInterface $product): array {
187-
$prices = [];
188-
foreach ($product->getChannels() as $channel) {
189-
/** @var ChannelInterface $channel */
190-
$this->channelSimulationContext->setChannel($channel);
191-
if (
192-
null === ($variant = $this->productVariantResolver->getVariant($product))
193-
|| !$variant instanceof ModelProductVariantInterface
194-
|| null === ($channelPricing = $variant->getChannelPricingForChannel($channel))
195-
) {
196-
$this->channelSimulationContext->setChannel(null);
162+
return $attributes;
163+
}
197164

165+
/**
166+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
167+
*/
168+
public function getOptions(ProductInterface $product): array
169+
{
170+
$options = [];
171+
$currentLocale = $product->getTranslation()->getLocale();
172+
foreach ($product->getVariants() as $variant) {
173+
foreach ($variant->getOptionValues() as $optionValue) {
174+
if (null === $optionValue->getOption()) {
198175
continue;
199176
}
200-
$this->channelSimulationContext->setChannel(null);
201-
$prices[] = $this->autoMapper->map(
202-
$channelPricing,
203-
$this->configuration->getTargetClass('pricing')
204-
);
177+
if (!isset($options[$optionValue->getOptionCode()])) {
178+
$options[$optionValue->getOptionCode()] = [
179+
'name' => $optionValue->getOption()->getTranslation($currentLocale)->getName(),
180+
'values' => [],
181+
];
182+
}
183+
$isEnabled = ($options[$optionValue->getOptionCode()]['values'][$optionValue->getCode()]['enabled'] ?? false)
184+
|| $variant->isEnabled();
185+
// A variant option is considered to be in stock if the current option is enabled and is in stock
186+
$isInStock = ($options[$optionValue->getOptionCode()]['values'][$optionValue->getCode()]['is_in_stock'] ?? false)
187+
|| ($variant->isEnabled() && $this->isProductVariantInStock($variant));
188+
$options[$optionValue->getOptionCode()]['values'][$optionValue->getCode()] = [
189+
'value' => $optionValue->getTranslation($currentLocale)->getValue(),
190+
'enabled' => $isEnabled,
191+
'is_in_stock' => $isInStock,
192+
];
205193
}
194+
}
206195

207-
return $prices;
208-
});
196+
foreach ($options as $optionCode => $optionValues) {
197+
$options[$optionCode]['values'] = array_values($optionValues['values']);
198+
}
199+
200+
return $options;
209201
}
210202

211-
public function getSource(): string
203+
public function getVariants(ProductInterface $product): array
212204
{
213-
return $this->configuration->getSourceClass('product');
205+
$variants = [];
206+
$productVariantDTOClass = $this->configuration->getTargetClass('product_variant');
207+
foreach ($product->getEnabledVariants() as $variant) {
208+
$variants[] = $this->autoMapper->map($variant, $productVariantDTOClass);
209+
}
210+
211+
return $variants;
214212
}
215213

216-
public function getTarget(): string
214+
/**
215+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
216+
*/
217+
public function getPrices(ProductInterface $product): array
217218
{
218-
return $this->configuration->getTargetClass('product');
219+
$prices = [];
220+
foreach ($product->getChannels() as $channel) {
221+
/** @var ChannelInterface $channel */
222+
$this->channelSimulationContext->setChannel($channel);
223+
if (
224+
null === ($variant = $this->productVariantResolver->getVariant($product))
225+
|| !$variant instanceof ModelProductVariantInterface
226+
|| null === ($channelPricing = $variant->getChannelPricingForChannel($channel))
227+
) {
228+
$this->channelSimulationContext->setChannel(null);
229+
230+
continue;
231+
}
232+
$this->channelSimulationContext->setChannel(null);
233+
$prices[] = $this->autoMapper->map(
234+
$channelPricing,
235+
$this->configuration->getTargetClass('pricing')
236+
);
237+
}
238+
239+
return $prices;
219240
}
220241

221242
private function isProductVariantInStock(ProductVariantInterface $productVariant): bool

src/Command/PopulateCommand.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Command\Command;
1818
use Symfony\Component\Console\Input\InputInterface;
1919
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Style\SymfonyStyle;
2021

2122
class PopulateCommand extends Command
2223
{
@@ -37,8 +38,9 @@ protected function configure(): void
3738

3839
protected function execute(InputInterface $input, OutputInterface $output)
3940
{
40-
$this->indexer->indexAll();
41-
$output->writeln('ok');
41+
$io = new SymfonyStyle($input, $output);
42+
$this->indexer->indexAll($io);
43+
$io->success('Done');
4244

4345
return Command::SUCCESS;
4446
}

0 commit comments

Comments
 (0)