Failed Conditions
Pull Request — experimental/sf (#29)
by Kentaro
50:12 queued 39:05
created

UsePointProcessor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 7.96 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 9
loc 113
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 16 3
A addPointDiscountItem() 0 33 2
A removePointDiscountItem() 9 9 4
A usePointToPrice() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Service\PurchaseFlow\Processor;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Eccube\Entity\BaseInfo;
18
use Eccube\Entity\ItemHolderInterface;
19
use Eccube\Entity\Master\OrderItemType;
20
use Eccube\Entity\Master\TaxDisplayType;
21
use Eccube\Entity\Master\TaxType;
22
use Eccube\Entity\Order;
23
use Eccube\Entity\OrderItem;
24
use Eccube\Service\PurchaseFlow\ItemHolderProcessor;
25
use Eccube\Service\PurchaseFlow\ProcessResult;
26
use Eccube\Service\PurchaseFlow\PurchaseContext;
27
28
/**
29
 * 使用ポイント値引明細追加.
30
 */
31
class UsePointProcessor implements ItemHolderProcessor
32
{
33
    /**
34
     * @var EntityManagerInterface
35
     */
36
    protected $entityManager;
37
38
    /**
39
     * @var BaseInfo
40
     */
41
    protected $BaseInfo;
42
43
    /**
44
     * UsePointProcessor constructor.
45
     *
46
     * @param EntityManagerInterface $entityManager
47
     * @param BaseInfo $BaseInfo
48
     */
49
    public function __construct(EntityManagerInterface $entityManager, BaseInfo $BaseInfo)
50
    {
51
        $this->entityManager = $entityManager;
52
        $this->BaseInfo = $BaseInfo;
53
    }
54
55
    /**
56
     * @param ItemHolderInterface $itemHolder
57
     * @param PurchaseContext     $context
58
     *
59
     * @return ProcessResult
60
     */
61
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
62
    {
63
        if ($itemHolder->getUsePoint() > 0) {
64
            if ($itemHolder->getUsePoint() > $context->getUser()->getPoint()) {
65
                // TODO カートに戻さないように修正する
66
                return ProcessResult::error('利用ポイントが所有ポイントを上回っています.');
67
            }
68
69
            // XXX delete/insert ではなく, update/insert の方がいいかも
70
            $this->removePointDiscountItem($itemHolder);
71
72
            return $this->addPointDiscountItem($itemHolder);
73
        }
74
75
        return ProcessResult::success();
76
    }
77
78
    /**
79
     * 明細追加処理
80
     *
81
     * @param ItemHolderInterface $itemHolder
82
     */
83
    protected function addPointDiscountItem(ItemHolderInterface $itemHolder)
84
    {
85
        $DiscountType = $this->entityManager
86
            ->find(OrderItemType::class, OrderItemType::DISCOUNT);
87
        // TODO
88
        $TaxInclude = $this->entityManager
89
            ->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
90
        $Taxion = $this->entityManager
91
            ->find(TaxType::class, TaxType::TAXATION);
92
93
        /** @var Order $Order */
94
        $Order = $itemHolder;
95
96
        $priceOfUsePoint = $this->usePointToPrice($Order->getUsePoint());
97
        if (($itemHolder->getTotal() + $priceOfUsePoint) < 0) {
98
            // TODO カートに戻さないように修正する
99
            // TODO 送料・手数料も考慮する
100
            return ProcessResult::error('利用ポイントがお支払い金額を上回っています.');
101
        }
102
        $OrderItem = new OrderItem();
103
        $OrderItem->setProductName('ポイント値引')
104
            ->setPrice($priceOfUsePoint)
105
            ->setPriceIncTax($priceOfUsePoint)
106
            ->setTaxRate(8)
107
            ->setQuantity(1)
108
            ->setOrderItemType($DiscountType)
109
            ->setTaxDisplayType($TaxInclude)
110
            ->setTaxType($Taxion)
111
            ->setOrder($itemHolder);
0 ignored issues
show
Documentation introduced by
$itemHolder is of type object<Eccube\Entity\ItemHolderInterface>, but the function expects a null|object<Eccube\Entity\Order>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
        $itemHolder->addItem($OrderItem);
113
114
        return ProcessResult::success();
115
    }
116
117
    /**
118
     * 既存のポイント明細を削除する.
119
     *
120
     * @param ItemHolderInterface $itemHolder
121
     */
122 View Code Duplication
    protected function removePointDiscountItem(ItemHolderInterface $itemHolder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        foreach ($itemHolder->getItems() as $item) {
125
            if ($item->isDiscount() && $item->getProductName() == 'ポイント値引') {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Eccube\Entity\ItemInterface as the method getProductName() does only exist in the following implementations of said interface: Eccube\Entity\OrderItem.

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...
126
                $itemHolder->removeOrderItem($item);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Eccube\Entity\ItemHolderInterface as the method removeOrderItem() does only exist in the following implementations of said interface: Eccube\Entity\Order.

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...
127
                $this->entityManager->remove($item);
128
            }
129
        }
130
    }
131
132
    /**
133
     * 利用ポイントを単価に換算する.
134
     *
135
     * @param integer $usePoint 利用ポイント
136
     *
137
     * @return integer
138
     */
139
    protected function usePointToPrice($usePoint)
140
    {
141
        return ($usePoint * $this->BaseInfo->getPointConversionRate()) * -1;
142
    }
143
}
144