Conditions | 2 |
Paths | 2 |
Total Lines | 19 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
27 | public function create(ProductVariantInterface $variant, ChannelInterface $channel, $locale) |
||
28 | { |
||
29 | $variantView = new ProductVariantView(); |
||
30 | |||
31 | $variantView->code = $variant->getCode(); |
||
32 | $variantView->name = $variant->getTranslation($locale)->getName(); |
||
|
|||
33 | $variantView->price = $this->priceViewFactory->create($variant->getChannelPricingForChannel($channel)->getPrice()); |
||
34 | |||
35 | foreach ($variant->getOptionValues() as $optionValue) { |
||
36 | $variantView->axis[] = $optionValue->getCode(); |
||
37 | $variantView->nameAxis[$optionValue->getCode()] = sprintf( |
||
38 | '%s %s', |
||
39 | $optionValue->getOption()->getTranslation($locale)->getName(), |
||
40 | $optionValue->getTranslation($locale)->getValue() |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | return $variantView; |
||
45 | } |
||
46 | } |
||
47 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: