CalculatesTotals::content()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Treestoneit\ShoppingCart\Concerns;
4
5
use Closure;
6
use Illuminate\Support\Facades\Config;
7
use Treestoneit\ShoppingCart\Models\CartItem;
8
use Treestoneit\ShoppingCart\Models\CartItemCollection;
9
use Treestoneit\ShoppingCart\Taxable;
10
11
trait CalculatesTotals
12
{
13
    /**
14
     * @var float
15
     */
16
    protected $subtotal = 0.0;
17
18
    /**
19
     * @var float
20
     */
21
    protected $tax = 0.0;
22
23
    /**
24
     * Get the cart contents.
25
     *
26
     * @return \Treestoneit\ShoppingCart\Models\CartItemCollection
27
     */
28
    public function content(): CartItemCollection
29
    {
30
        return $this->items();
0 ignored issues
show
Bug introduced by
It seems like items() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
31
    }
32
33
    /**
34
     * Get the number of items in the cart.
35
     *
36
     * @return int
37
     */
38
    public function count(): int
39
    {
40
        return $this->items()->sum('quantity');
0 ignored issues
show
Bug introduced by
It seems like items() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
    }
42
43
    /**
44
     * Get the subtotal of items in the cart.
45
     *
46
     * @return int|float
47
     */
48
    public function subtotal(): float
49
    {
50
        if (! $this->subtotal) {
51
            $this->subtotal = $this->items()->sumRounded(function (CartItem $item) {
0 ignored issues
show
Bug introduced by
It seems like items() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
52
                return $item->subtotal;
53
            });
54
        }
55
56
        return $this->subtotal;
57
    }
58
59
    /**
60
     * Get the tax for items in the cart.
61
     *
62
     * @param  int|float|null  $rate
63
     * @return float
64
     */
65
    public function tax($rate = null): float
66
    {
67
        if (! $this->tax) {
68
            $this->tax = $this->items()->sumRounded($this->getTaxAmountForItem($rate));
0 ignored issues
show
Bug introduced by
It seems like items() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
        }
70
71
        return $this->tax;
72
    }
73
74
    /**
75
     * Clear the cached totals.
76
     *
77
     * @return void
78
     */
79
    public function clearCached(): void
80
    {
81
        $this->subtotal = 0.0;
82
        $this->tax = 0.0;
83
    }
84
85
    /**
86
     * Figure out how to calculate tax for the cart items.
87
     *
88
     * @param  int|float|null  $rate
89
     * @return \Closure
90
     */
91
    protected function getTaxAmountForItem($rate = null): Closure
92
    {
93
        if (! $rate && Config::get('shopping-cart.tax.mode') == 'flat') {
94
            $rate = Config::get('shopping-cart.tax.rate');
95
        }
96
97
        return function (CartItem $item) use ($rate) {
98
            if (! $rate) {
99
                $rate = $item->buyable instanceof Taxable
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $rate, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
100
                    ? $item->buyable->getTaxRate()
101
                    : 0;
102
            }
103
104
            return round($item->price * $item->quantity * ($rate / 100), 2);
105
        };
106
    }
107
}
108