| Conditions | 10 |
| Paths | 15 |
| Total Lines | 44 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 28 | public function convert(int $amount, string $sourceCurrency, string $targetCurrency, array $conversionContext = []): Money |
||
| 29 | { |
||
| 30 | if(!$this->supports($amount, $sourceCurrency, $targetCurrency, $conversionContext)) { |
||
| 31 | throw new CurrencyConversionException($amount, $sourceCurrency, $targetCurrency, $conversionContext); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** @var ProductVariantInterface $productVariant */ |
||
| 35 | $productVariant = $conversionContext['productVariant']; |
||
| 36 | |||
| 37 | $productVariantSourceAmount = $productVariantTargetAmount = null; |
||
| 38 | |||
| 39 | foreach ($productVariant->getChannelPricings() as $channelPricing) { |
||
| 40 | /** @var ChannelInterface|null $channel */ |
||
| 41 | $channel = $this->channelRepository->findOneByCode($channelPricing->getChannelCode()); |
||
| 42 | if (null === $channel) { |
||
| 43 | continue; |
||
| 44 | } |
||
| 45 | |||
| 46 | $baseCurrency = $channel->getBaseCurrency(); |
||
| 47 | if (null === $baseCurrency) { |
||
| 48 | continue; |
||
| 49 | } |
||
| 50 | |||
| 51 | $baseCurrencyCode = $baseCurrency->getCode(); |
||
| 52 | if (null === $baseCurrencyCode) { |
||
| 53 | continue; |
||
| 54 | } |
||
| 55 | |||
| 56 | if ($sourceCurrency === $baseCurrencyCode) { |
||
| 57 | $productVariantSourceAmount = $channelPricing->getPrice(); |
||
| 58 | } elseif ($targetCurrency === $baseCurrencyCode) { |
||
| 59 | $productVariantTargetAmount = $channelPricing->getPrice(); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | if (null === $productVariantSourceAmount || null === $productVariantTargetAmount) { |
||
|
|
|||
| 64 | throw new CurrencyConversionException($amount, $sourceCurrency, $targetCurrency, $conversionContext); |
||
| 65 | } |
||
| 66 | |||
| 67 | $exchangeRate = $productVariantSourceAmount / $productVariantTargetAmount; |
||
| 68 | |||
| 69 | $convertedAmount = round($amount / $exchangeRate); |
||
| 70 | |||
| 71 | return new Money((int) $convertedAmount, new Currency($targetCurrency)); |
||
| 72 | } |
||
| 80 |