1 | <?php |
||
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 |
||
32 | |||
33 | /** |
||
34 | * Get the number of items in the cart. |
||
35 | * |
||
36 | * @return int |
||
37 | */ |
||
38 | public function count(): int |
||
42 | |||
43 | /** |
||
44 | * Get the subtotal of items in the cart. |
||
45 | * |
||
46 | * @return int|float |
||
47 | */ |
||
48 | public function subtotal(): float |
||
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 |
||
73 | |||
74 | /** |
||
75 | * Clear the cached totals. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function clearCached(): void |
||
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 |
||
107 | } |
||
108 |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.