1 | <?php |
||
17 | class CartFinisher extends BaseObject |
||
18 | { |
||
19 | /** |
||
20 | * @var ShoppingCart |
||
21 | */ |
||
22 | public $cart; |
||
23 | |||
24 | /** |
||
25 | * @var PurchaseStrategyInterface[] |
||
26 | */ |
||
27 | protected $purchasers = []; |
||
28 | |||
29 | /** |
||
30 | * @var AbstractPurchase[] array of successful purchases |
||
31 | */ |
||
32 | protected $_success = []; |
||
33 | |||
34 | /** |
||
35 | * @var ErrorPurchaseException[] array of failed purchases |
||
36 | */ |
||
37 | protected $_error = []; |
||
38 | |||
39 | /** |
||
40 | * @var PendingPurchaseException[] array of purchases that are pending |
||
41 | */ |
||
42 | protected $_pending = []; |
||
43 | |||
44 | /** |
||
45 | * Getter for array of successful purchases. |
||
46 | * @return AbstractPurchase[] |
||
47 | */ |
||
48 | public function getSuccess() |
||
52 | |||
53 | /** |
||
54 | * Getter for array of failed purchases. |
||
55 | * @return ErrorPurchaseException[] |
||
56 | */ |
||
57 | public function getError() |
||
61 | |||
62 | /** |
||
63 | * Getter for array of failed purchases. |
||
64 | * @return PendingPurchaseException[] |
||
65 | */ |
||
66 | public function getPending() |
||
70 | |||
71 | /** |
||
72 | * Runs the purchase. |
||
73 | * Purchases the positions in the [[cart]]. |
||
74 | */ |
||
75 | public function run() |
||
76 | { |
||
77 | if ($this->cart->isEmpty) { |
||
78 | return; |
||
79 | } |
||
80 | |||
81 | $this->ensureCanBeFinished(); |
||
82 | $this->createPurchasers(); |
||
83 | |||
84 | foreach ($this->purchasers as $purchaser) { |
||
85 | $purchaser->run(); |
||
86 | |||
87 | $this->_success = array_merge($this->_success, $purchaser->getSuccessPurchases()); |
||
88 | foreach ($purchaser->getSuccessPurchases() as $purchase) { |
||
89 | $this->cart->remove($purchase->position); |
||
90 | } |
||
91 | $this->_pending = array_merge($this->_pending, $purchaser->getPendingPurchaseExceptions()); |
||
92 | foreach ($purchaser->getPendingPurchaseExceptions() as $exception) { |
||
93 | $this->cart->remove($exception->position); |
||
94 | } |
||
95 | $this->_error = array_merge($this->_error, $purchaser->getErrorPurchaseExceptions()); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | protected function ensureCanBeFinished() |
||
122 | |||
123 | protected function createPurchasers() |
||
124 | { |
||
125 | foreach ($this->cart->positions as $position) { |
||
126 | if ($position instanceof BatchPurchasablePositionInterface) { |
||
127 | $purchaser = $this->getPurchaser(get_class($position), $position->getBatchPurchaseStrategyClass()); |
||
128 | } else { |
||
129 | $purchaser = $this->getPurchaser(get_class($position), OneByOnePurchaseStrategy::class); |
||
130 | } |
||
131 | |||
132 | $purchaser->addPosition($position); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param string $positionClass |
||
138 | * @param string $purchaserClass |
||
139 | * @return PurchaseStrategyInterface |
||
140 | */ |
||
141 | protected function getPurchaser($positionClass, $purchaserClass) |
||
149 | } |
||
150 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: