OrderFixedDiscountAction   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 77
loc 77
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A execute() 19 19 3
A calculateAdjustmentAmount() 4 4 1
A calculate() 12 12 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 ibrand/discount.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Component\Discount\Actions;
13
14
use iBrand\Component\Discount\Contracts\DiscountActionContract;
15
use iBrand\Component\Discount\Contracts\DiscountContract;
16
use iBrand\Component\Discount\Contracts\DiscountSubjectContract;
17
use iBrand\Component\Discount\Distributors\PercentageIntegerDistributor;
18
use Illuminate\Support\Collection;
19
20
/**
21
 * Class OrderFixedDiscountAction.
22
 */
23 View Code Duplication
class OrderFixedDiscountAction extends DiscountAction implements DiscountActionContract
0 ignored issues
show
Duplication introduced by
This class 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...
24
{
25
    const TYPE = 'order_fixed_discount';
26
27
    /**
28
     * @var IntegerDistributor
29
     */
30
    private $distributor;
31
32
    /**
33
     * OrderFixedDiscountAction constructor.
34
     *
35
     * @param IntegerDistributor $distributor
36
     */
37 2
    public function __construct(PercentageIntegerDistributor $distributor)
38
    {
39 2
        $this->distributor = $distributor;
0 ignored issues
show
Documentation Bug introduced by
It seems like $distributor of type object<iBrand\Component\...tageIntegerDistributor> is incompatible with the declared type object<iBrand\Component\...ons\IntegerDistributor> of property $distributor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40 2
    }
41
42
    /**
43
     * @param DiscountSubjectContract $subject
44
     * @param array                   $configuration
45
     * @param DiscountContract        $discount
46
     *
47
     * @return mixed|void
48
     */
49 1
    public function execute(DiscountSubjectContract $subject, array $configuration, DiscountContract $discount)
50
    {
51 1
        $discountAmount = $this->calculateAdjustmentAmount($subject->getCurrentTotal(), $configuration['amount']);
52
53 1
        if (0 === $discountAmount) {
54
            return;
55
        }
56
57 1
        $adjustment = $this->createAdjustment($discount, $discountAmount);
58 1
        $subject->addAdjustment($adjustment);
59
60 1
        $splitDiscountAmount = $this->distributor->distribute($subject->getItems()->pluck('total')->toArray(), $discountAmount);
61
62 1
        $i = 0;
63 1
        foreach ($subject->getItems() as $item) {
64 1
            $item->divide_order_discount += $splitDiscountAmount[$i++];
65 1
            $item->recalculateAdjustmentsTotal();
66
        }
67 1
    }
68
69
    /**
70
     * @param $discountSubjectTotal
71
     * @param $targetDiscountAmount
72
     *
73
     * @return float|int
74
     */
75 2
    private function calculateAdjustmentAmount($discountSubjectTotal, $targetDiscountAmount)
76
    {
77 2
        return -1 * min($discountSubjectTotal, $targetDiscountAmount);
78
    }
79
80
    /**
81
     * @param DiscountSubjectContract $subject
82
     * @param array                   $configuration
83
     * @param DiscountContract        $discount
84
     *
85
     * @return float|int|mixed
86
     */
87 1
    public function calculate(DiscountSubjectContract $subject, array $configuration, DiscountContract $discount)
88
    {
89 1
        $discount->adjustments = new Collection();
0 ignored issues
show
Bug introduced by
Accessing adjustments on the interface iBrand\Component\Discoun...tracts\DiscountContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
90
91 1
        $discountAmount = $this->calculateAdjustmentAmount($subject->getCurrentTotal(), $configuration['amount']);
92
93 1
        $discount->adjustments->push(['order_id' => $subject->id, 'amount' => $discountAmount]);
0 ignored issues
show
Bug introduced by
Accessing adjustments on the interface iBrand\Component\Discoun...tracts\DiscountContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing id on the interface iBrand\Component\Discoun...DiscountSubjectContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
94
95 1
        $discount->adjustmentTotal = $discountAmount;
0 ignored issues
show
Bug introduced by
Accessing adjustmentTotal on the interface iBrand\Component\Discoun...tracts\DiscountContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
96
97 1
        return $discountAmount;
98
    }
99
}
100