Completed
Pull Request — master (#124)
by Łukasz
02:52
created

ProductViewFactory::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 3
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Factory;
4
5
use Sylius\Component\Core\Model\ChannelInterface;
6
use Sylius\Component\Core\Model\ProductImageInterface;
7
use Sylius\Component\Core\Model\ProductInterface;
8
use Sylius\Component\Core\Model\TaxonInterface;
9
use Sylius\ShopApiPlugin\View\ProductView;
10
use Sylius\ShopApiPlugin\View\TaxonView;
11
12
final class ProductViewFactory implements ProductViewFactoryInterface
13
{
14
    /**
15
     * @var ImageViewFactoryInterface
16
     */
17
    private $imageViewFactory;
18
19
    /**
20
     * @var ProductAttributeValuesViewFactoryInterface
21
     */
22
    private $attributeValuesViewFactory;
23
24
    /**
25
     * @var TaxonViewFactoryInterface
26
     */
27
    private $taxonViewFactory;
28
29
    /**
30
     * @var string
31
     */
32
    private $fallback;
33
34
    /**
35
     * @param ImageViewFactoryInterface $imageViewFactory
36
     * @param ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory
37
     * @param TaxonViewFactoryInterface $taxonViewFactory
38
     * @param string $fallback
39
     */
40
    public function __construct(
41
        ImageViewFactoryInterface $imageViewFactory,
42
        ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory,
43
        TaxonViewFactoryInterface $taxonViewFactory,
44
        $fallback
45
    ) {
46
        $this->imageViewFactory = $imageViewFactory;
47
        $this->attributeValuesViewFactory = $attributeValuesViewFactory;
48
        $this->taxonViewFactory = $taxonViewFactory;
49
        $this->fallback = $fallback;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function create(ProductInterface $product, ChannelInterface $channel, $locale)
56
    {
57
        $productView = new ProductView();
58
        $productView->name = $product->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...
59
        $productView->code = $product->getCode();
60
        $productView->slug = $product->getTranslation($locale)->getSlug();
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 getSlug() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Product\Model\ProductTranslation, 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...
61
62
        /** @var ProductImageInterface $image */
63
        foreach ($product->getImages() as $image) {
64
            $imageView = $this->imageViewFactory->create($image);
65
            $productView->images[] = $imageView;
66
        }
67
68
        /** @var TaxonInterface $taxon */
69
        foreach ($product->getTaxons() as $taxon) {
70
            $productView->taxons[] = $this->getTaxonWithAncestors($taxon, $locale);
71
        }
72
73
        $productView->attributes = $this->attributeValuesViewFactory->create($product->getAttributesByLocale($locale, $this->fallback));
0 ignored issues
show
Bug introduced by
It seems like $product->getAttributesB...ocale, $this->fallback) targeting Sylius\Component\Attribu...getAttributesByLocale() can also be of type object<Doctrine\Common\Collections\Collection>; however, Sylius\ShopApiPlugin\Fac...toryInterface::create() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
74
75
        return $productView;
76
    }
77
78
    /**
79
     * @param TaxonInterface $taxon
80
     * @param string $locale
81
     *
82
     * @return TaxonView
83
     */
84
    private function getTaxonWithAncestors(TaxonInterface $taxon, $locale)
85
    {
86
        $currentTaxonView = $this->taxonViewFactory->create($taxon, $locale);
87
88
        if (null === $taxon->getParent()) {
89
            return $currentTaxonView;
90
        }
91
92
        $taxonView = $this->getTaxonWithAncestors($taxon->getParent(), $locale);
0 ignored issues
show
Compatibility introduced by
$taxon->getParent() of type object<Sylius\Component\...y\Model\TaxonInterface> is not a sub-type of object<Sylius\Component\...e\Model\TaxonInterface>. It seems like you assume a child interface of the interface Sylius\Component\Taxonomy\Model\TaxonInterface 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...
93
        $taxonView->children[] = $currentTaxonView;
94
95
        return $taxonView;
96
    }
97
}
98