1 | <?php |
||
20 | class CartFinisher extends BaseObject |
||
21 | { |
||
22 | /** |
||
23 | * @var ShoppingCart |
||
24 | */ |
||
25 | public $cart; |
||
26 | |||
27 | /** |
||
28 | * @var string|null |
||
29 | */ |
||
30 | public $exchangeFromCurrency; |
||
31 | |||
32 | /** |
||
33 | * @var User |
||
34 | */ |
||
35 | public $user; |
||
36 | |||
37 | /** |
||
38 | * @var PurchaseStrategyInterface[] |
||
39 | */ |
||
40 | protected $purchasers = []; |
||
41 | |||
42 | /** |
||
43 | * @var AbstractPurchase[] array of successful purchases |
||
44 | */ |
||
45 | protected $_success = []; |
||
46 | |||
47 | /** |
||
48 | * @var ErrorPurchaseException[] array of failed purchases |
||
49 | */ |
||
50 | protected $_error = []; |
||
51 | |||
52 | /** |
||
53 | * @var PendingPurchaseException[] array of purchases that are pending |
||
54 | */ |
||
55 | protected $_pending = []; |
||
56 | |||
57 | /** |
||
58 | * Getter for array of successful purchases. |
||
59 | * @return AbstractPurchase[] |
||
60 | */ |
||
61 | public function getSuccess() |
||
65 | |||
66 | /** |
||
67 | * Getter for array of failed purchases. |
||
68 | * @return ErrorPurchaseException[] |
||
69 | */ |
||
70 | public function getError() |
||
74 | |||
75 | /** |
||
76 | * Getter for array of failed purchases. |
||
77 | * @return PendingPurchaseException[] |
||
78 | */ |
||
79 | public function getPending() |
||
83 | |||
84 | /** |
||
85 | * Runs the purchase. |
||
86 | * Purchases the positions in the [[cart]]. |
||
87 | */ |
||
88 | public function run() |
||
114 | |||
115 | protected function ensureCanBeFinished() |
||
138 | |||
139 | protected function createPurchasers() |
||
151 | |||
152 | /** |
||
153 | * @param string $positionClass |
||
154 | * @param string $purchaserClass |
||
155 | * @return PurchaseStrategyInterface |
||
156 | */ |
||
157 | protected function getPurchaser($positionClass, $purchaserClass) |
||
165 | |||
166 | private function exchangeMoney(): void |
||
178 | } |
||
179 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: