Completed
Push — develop ( 295319...961a49 )
by
unknown
08:29
created

LineItem::fieldDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 21
ccs 17
cts 17
cp 1
rs 9.3142
cc 1
eloc 17
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\ItemState;
14
use Commercetools\Core\Model\Order\ItemStateCollection;
15
use Commercetools\Core\Model\Product\ProductVariant;
16
use Commercetools\Core\Model\TaxCategory\TaxRate;
17
use Commercetools\Core\Model\CustomField\CustomFieldObject;
18
19
/**
20
 * @package Commercetools\Core\Model\Cart
21
 * @apidoc http://dev.sphere.io/http-api-projects-carts.html#line-item
22
 * @method string getId()
23
 * @method LineItem setId(string $id = null)
24
 * @method string getProductId()
25
 * @method LineItem setProductId(string $productId = null)
26
 * @method LocalizedString getName()
27
 * @method LineItem setName(LocalizedString $name = null)
28
 * @method ProductVariant getVariant()
29
 * @method LineItem setVariant(ProductVariant $variant = null)
30
 * @method Price getPrice()
31
 * @method LineItem setPrice(Price $price = null)
32
 * @method int getQuantity()
33
 * @method LineItem setQuantity(int $quantity = null)
34
 * @method ItemStateCollection getState()
35
 * @method LineItem setState(ItemStateCollection $state = null)
36
 * @method TaxRate getTaxRate()
37
 * @method LineItem setTaxRate(TaxRate $taxRate = null)
38
 * @method ChannelReference getSupplyChannel()
39
 * @method LineItem setSupplyChannel(ChannelReference $supplyChannel = null)
40
 * @method LocalizedString getProductSlug()
41
 * @method LineItem setProductSlug(LocalizedString $productSlug = null)
42
 * @method ChannelReference getDistributionChannel()
43
 * @method LineItem setDistributionChannel(ChannelReference $distributionChannel = null)
44
 * @method CustomFieldObject getCustom()
45
 * @method LineItem setCustom(CustomFieldObject $custom = null)
46
 * @method Money getTotalPrice()
47
 * @method LineItem setTotalPrice(Money $totalPrice = null)
48
 * @method DiscountedPricePerQuantityCollection getDiscountedPricePerQuantity()
49
 */
50
class LineItem extends JsonObject
51
{
52 2
    public function fieldDefinitions()
53
    {
54
        return [
55 2
            'id' => [static::TYPE => 'string'],
56 2
            'productId' => [static::TYPE => 'string'],
57 2
            'name' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
58 2
            'productSlug' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
59 2
            'variant' => [static::TYPE => '\Commercetools\Core\Model\Product\ProductVariant'],
60 2
            'price' => [static::TYPE => '\Commercetools\Core\Model\Common\Price'],
61 2
            'quantity' => [static::TYPE => 'int'],
62 2
            'state' => [static::TYPE => '\Commercetools\Core\Model\Order\ItemStateCollection'],
63 2
            'taxRate' => [static::TYPE => '\Commercetools\Core\Model\TaxCategory\TaxRate'],
64 2
            'supplyChannel' => [static::TYPE => '\Commercetools\Core\Model\Channel\ChannelReference'],
65 2
            'distributionChannel' => [static::TYPE => '\Commercetools\Core\Model\Channel\ChannelReference'],
66 2
            'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObject'],
67 2
            'totalPrice' => [static::TYPE => '\Commercetools\Core\Model\Common\Money'],
68
            'discountedPricePerQuantity' => [
69 2
                static::TYPE => '\Commercetools\Core\Model\Cart\DiscountedPricePerQuantityCollection'
70 2
            ],
71 2
        ];
72
    }
73
74
    /**
75
     * @param DiscountedPricePerQuantityCollection $discountedPricePerQuantity
76
     * @return static
77
     */
78
    public function setDiscountedPricePerQuantity(
79
        DiscountedPricePerQuantityCollection $discountedPricePerQuantity = null
80
    ) {
81
        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...
82
    }
83
84
    /**
85
     * @return Money
86
     */
87
    public function getDiscountedPrice()
88
    {
89
        $centAmount = 0;
90
        $currencyCode = $this->getPrice()->getValue()->getCurrencyCode();
91
        foreach ($this->getDiscountedPricePerQuantity() as $discountedPricePerQuantity) {
92
            $centAmount += $discountedPricePerQuantity->getDiscountedTotal()->getCentAmount();
93
        }
94
        return Money::ofCurrencyAndAmount($currencyCode, $centAmount);
95
    }
96
}
97