Completed
Push — master ( 41948a...5bb6bb )
by Łukasz
17:08 queued 13:33
created

ShippingMethodViewFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Factory;
4
5
use Sylius\Component\Core\Model\ShipmentInterface;
6
use Sylius\Component\Core\Model\ShippingMethodInterface;
7
use Sylius\Component\Registry\ServiceRegistry;
8
use Sylius\Component\Shipping\Calculator\CalculatorInterface;
9
use Sylius\ShopApiPlugin\View\ShippingMethodView;
10
11
final class ShippingMethodViewFactory implements ShippingMethodViewFactoryInterface
12
{
13
    /**
14
     * @var ServiceRegistry
15
     */
16
    private $calculators;
17
18
    /**
19
     * @var PriceViewFactoryInterface
20
     */
21
    private $priceViewFactory;
22
23
    /**
24
     * @param ServiceRegistry $calculators
25
     * @param PriceViewFactoryInterface $priceViewFactory
26
     */
27
    public function __construct(ServiceRegistry $calculators, PriceViewFactoryInterface $priceViewFactory)
28
    {
29
        $this->calculators = $calculators;
30
        $this->priceViewFactory = $priceViewFactory;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function create(ShipmentInterface $shipment, $locale)
37
    {
38
        return $this->createWithShippingMethod($shipment, $shipment->getMethod(), $locale);
0 ignored issues
show
Compatibility introduced by
$shipment->getMethod() of type object<Sylius\Component\...hippingMethodInterface> is not a sub-type of object<Sylius\Component\...hippingMethodInterface>. It seems like you assume a child interface of the interface Sylius\Component\Shippin...ShippingMethodInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function createWithShippingMethod(ShipmentInterface $shipment, ShippingMethodInterface $shippingMethod, $locale)
45
    {
46
        /** @var CalculatorInterface $calculator */
47
        $calculator = $this->calculators->get($shippingMethod->getCalculator());
48
49
        $shippingMethodView = new ShippingMethodView();
50
51
        $shippingMethodView->code = $shippingMethod->getCode();
52
        $shippingMethodView->name = $shippingMethod->getTranslation($locale)->getName();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method getName() does only exist in the following implementations of said interface: Sylius\Component\Attribu...el\AttributeTranslation, Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Product...ociationTypeTranslation, Sylius\Component\Product...uctAttributeTranslation, Sylius\Component\Product...roductOptionTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Product...oductVariantTranslation, Sylius\Component\Shippin...ippingMethodTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
53
        $shippingMethodView->description = $shippingMethod->getTranslation($locale)->getDescription();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method getDescription() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Shippin...ippingMethodTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
54
        $shippingMethodView->price = $this->priceViewFactory->create(
55
            $calculator->calculate($shipment, $shippingMethod->getConfiguration())
56
        );
57
58
        return $shippingMethodView;
59
    }
60
}
61