| Conditions | 9 |
| Paths | 96 |
| Total Lines | 52 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 38 | public function create(ProductVariantInterface $variant, string $localeCode): ProductVariantView |
||
| 39 | { |
||
| 40 | $translation = $variant->getTranslation($localeCode); |
||
| 41 | |||
| 42 | $weight = $variant->getShippingWeight(); |
||
| 43 | if (null !== $weight) { |
||
| 44 | $weight = (int) ceil(1000 * $variant->getShippingWeight()); |
||
| 45 | } |
||
| 46 | |||
| 47 | if ($variant->isTracked()) { |
||
| 48 | $onHand = $variant->getOnHand(); |
||
| 49 | } else { |
||
| 50 | $onHand = 1; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** @var ProductVariantView $variantView */ |
||
| 54 | $variantView = new $this->productVariantViewClass(); |
||
| 55 | $variantView->id = $variant->getId(); |
||
| 56 | $variantView->code = $variant->getCode(); |
||
| 57 | $variantView->name = $translation->getName(); |
||
|
|
|||
| 58 | $variantView->weight = $weight; |
||
| 59 | $variantView->onHand = $onHand; |
||
| 60 | |||
| 61 | /** @var ProductInterface|null $product */ |
||
| 62 | $product = $variant->getProduct(); |
||
| 63 | Assert::notNull($product); |
||
| 64 | |||
| 65 | if ($product instanceof BrandAwareInterface) { |
||
| 66 | $brand = $product->getBrand(); |
||
| 67 | if (null !== $brand) { |
||
| 68 | $variantView->brand = $this->brandViewFactory->create($brand); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | if ($variant instanceof BarcodeAwareInterface) { |
||
| 73 | $variantView->barcode = $variant->getBarcode(); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** @var ProductImageInterface $image */ |
||
| 77 | foreach ($product->getImages() as $image) { |
||
| 78 | $imageView = $this->imageViewFactory->create($image); |
||
| 79 | |||
| 80 | foreach ($image->getProductVariants() as $imagesVariant) { |
||
| 81 | if ($imagesVariant !== $variant) { |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | |||
| 85 | $variantView->images[] = $imageView; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return $variantView; |
||
| 90 | } |
||
| 92 |