Completed
Push — develop ( e9cc4a...e79554 )
by Jens
09:02
created

LineItem::fieldDefinitions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 18
cts 18
cp 1
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Cart;
7
8
use Commercetools\Core\Model\Channel\ChannelReference;
9
use Commercetools\Core\Model\Common\JsonObject;
10
use Commercetools\Core\Model\Common\LocalizedString;
11
use Commercetools\Core\Model\Common\Money;
12
use Commercetools\Core\Model\Common\Price;
13
use Commercetools\Core\Model\Order\ItemStateCollection;
14
use Commercetools\Core\Model\Product\ProductVariant;
15
use Commercetools\Core\Model\TaxCategory\TaxRate;
16
use Commercetools\Core\Model\CustomField\CustomFieldObject;
17
use Commercetools\Core\Model\Common\TaxedItemPrice;
18
use Commercetools\Core\Model\ProductType\ProductTypeReference;
19
20
/**
21
 * @package Commercetools\Core\Model\Cart
22
 * @link https://dev.commercetools.com/http-api-projects-carts.html#lineitem
23
 * @method string getId()
24
 * @method LineItem setId(string $id = null)
25
 * @method string getProductId()
26
 * @method LineItem setProductId(string $productId = null)
27
 * @method LocalizedString getName()
28
 * @method LineItem setName(LocalizedString $name = null)
29
 * @method ProductVariant getVariant()
30
 * @method LineItem setVariant(ProductVariant $variant = null)
31
 * @method Price getPrice()
32
 * @method LineItem setPrice(Price $price = null)
33
 * @method int getQuantity()
34
 * @method LineItem setQuantity(int $quantity = null)
35
 * @method ItemStateCollection getState()
36
 * @method LineItem setState(ItemStateCollection $state = null)
37
 * @method TaxRate getTaxRate()
38
 * @method LineItem setTaxRate(TaxRate $taxRate = null)
39
 * @method ChannelReference getSupplyChannel()
40
 * @method LineItem setSupplyChannel(ChannelReference $supplyChannel = null)
41
 * @method LocalizedString getProductSlug()
42
 * @method LineItem setProductSlug(LocalizedString $productSlug = null)
43
 * @method ChannelReference getDistributionChannel()
44
 * @method LineItem setDistributionChannel(ChannelReference $distributionChannel = null)
45
 * @method CustomFieldObject getCustom()
46
 * @method LineItem setCustom(CustomFieldObject $custom = null)
47
 * @method Money getTotalPrice()
48
 * @method LineItem setTotalPrice(Money $totalPrice = null)
49
 * @method DiscountedPricePerQuantityCollection getDiscountedPricePerQuantity()
50
 * @method TaxedItemPrice getTaxedPrice()
51
 * @method LineItem setTaxedPrice(TaxedItemPrice $taxedPrice = null)
52
 * @method string getPriceMode()
53
 * @method LineItem setPriceMode(string $priceMode = null)
54
 * @method ProductTypeReference getProductType()
55
 * @method LineItem setProductType(ProductTypeReference $productType = null)
56
 */
57
class LineItem extends JsonObject
58
{
59
    const PRICE_MODE_PLATFORM = 'Platform';
60
    const PRICE_MODE_EXTERNAL_TOTAL = 'ExternalTotal';
61
62 27
    public function fieldDefinitions()
63
    {
64
        return [
65 27
            'id' => [static::TYPE => 'string'],
66 27
            'productId' => [static::TYPE => 'string'],
67 27
            'name' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
68 27
            'productSlug' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
69 27
            'variant' => [static::TYPE => '\Commercetools\Core\Model\Product\ProductVariant'],
70 27
            'price' => [static::TYPE => '\Commercetools\Core\Model\Common\Price'],
71 27
            'taxedPrice' => [static::TYPE => '\Commercetools\Core\Model\Common\TaxedItemPrice'],
72 27
            'quantity' => [static::TYPE => 'int'],
73 27
            'state' => [static::TYPE => '\Commercetools\Core\Model\Order\ItemStateCollection'],
74 27
            'taxRate' => [static::TYPE => '\Commercetools\Core\Model\TaxCategory\TaxRate'],
75 27
            'supplyChannel' => [static::TYPE => '\Commercetools\Core\Model\Channel\ChannelReference'],
76 27
            'distributionChannel' => [static::TYPE => '\Commercetools\Core\Model\Channel\ChannelReference'],
77 27
            'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObject'],
78 27
            'totalPrice' => [static::TYPE => '\Commercetools\Core\Model\Common\Money'],
79
            'discountedPricePerQuantity' => [
80 27
                static::TYPE => '\Commercetools\Core\Model\Cart\DiscountedPricePerQuantityCollection'
81
            ],
82 27
            'priceMode' => [static::TYPE => 'string'],
83 27
            'productType' => [static::TYPE => '\Commercetools\Core\Model\ProductType\ProductTypeReference']
84
        ];
85
    }
86
87
    /**
88
     * @param DiscountedPricePerQuantityCollection $discountedPricePerQuantity
89
     * @return static
90
     */
91
    public function setDiscountedPricePerQuantity(
92
        DiscountedPricePerQuantityCollection $discountedPricePerQuantity = null
93
    ) {
94
        return parent::setDiscountedPricePerQuantity($discountedPricePerQuantity);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Commercetools\Core\Model\Common\JsonObject as the method setDiscountedPricePerQuantity() does only exist in the following sub-classes of Commercetools\Core\Model\Common\JsonObject: Commercetools\Core\Model\Cart\CustomLineItem, Commercetools\Core\Model\Cart\LineItem. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
95
    }
96
97
    /**
98
     * @return Money
99
     */
100
    public function getDiscountedPrice()
101
    {
102
        $centAmount = 0;
103
        $currencyCode = $this->getPrice()->getValue()->getCurrencyCode();
104
        foreach ($this->getDiscountedPricePerQuantity() as $discountedPricePerQuantity) {
105
            $centAmount += $discountedPricePerQuantity->getDiscountedTotal()->getCentAmount();
106
        }
107
        return Money::ofCurrencyAndAmount($currencyCode, $centAmount);
108
    }
109
}
110