Completed
Pull Request — master (#183)
by
unknown
01:41
created

Cart::saveConditions()   A

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 1
1
<?php namespace Darryldecode\Cart;
2
3
use Darryldecode\Cart\Exceptions\InvalidConditionException;
4
use Darryldecode\Cart\Exceptions\InvalidItemException;
5
use Darryldecode\Cart\Helpers\Helpers;
6
use Darryldecode\Cart\Validators\CartItemValidator;
7
use Darryldecode\Cart\Exceptions\UnknownModelException;
8
9
/**
10
 * Class Cart
11
 * @package Darryldecode\Cart
12
 */
13
class Cart
14
{
15
16
    /**
17
     * the item storage
18
     *
19
     * @var
20
     */
21
    protected $session;
22
23
    /**
24
     * the event dispatcher
25
     *
26
     * @var
27
     */
28
    protected $events;
29
30
    /**
31
     * the cart session key
32
     *
33
     * @var
34
     */
35
    protected $instanceName;
36
37
    /**
38
     * the session key use for the cart
39
     *
40
     * @var
41
     */
42
    protected $sessionKey;
43
44
    /**
45
     * the session key use to persist cart items
46
     *
47
     * @var
48
     */
49
    protected $sessionKeyCartItems;
50
51
    /**
52
     * the session key use to persist cart conditions
53
     *
54
     * @var
55
     */
56
    protected $sessionKeyCartConditions;
57
58
    /**
59
     * Configuration to pass to ItemCollection
60
     *
61
     * @var
62
     */
63
    protected $config;
64
65
    /**
66
     * This holds the currently added item id in cart for association
67
     * 
68
     * @var
69
     */
70
    protected $currentItemId;
71
72
    /**
73
     * our object constructor
74
     *
75
     * @param $session
76
     * @param $events
77
     * @param $instanceName
78
     * @param $session_key
79
     * @param $config
80
     */
81
    public function __construct($session, $events, $instanceName, $session_key, $config)
82
    {
83
        $this->events = $events;
84
        $this->session = $session;
85
        $this->instanceName = $instanceName;
86
        $this->sessionKey = $session_key;
87
        $this->sessionKeyCartItems = $this->sessionKey . '_cart_items';
88
        $this->sessionKeyCartConditions = $this->sessionKey . '_cart_conditions';
89
        $this->config = $config;
90
        $this->currentItem = null;
0 ignored issues
show
Bug introduced by
The property currentItem does not seem to exist. Did you mean currentItemId?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
91
        $this->fireEvent('created');
92
    }
93
94
    /**
95
     * sets the session key
96
     *
97
     * @param string $sessionKey the session key or identifier
98
     * @return $this|bool
99
     * @throws \Exception
100
     */
101
    public function session($sessionKey)
102
    {
103
        if (!$sessionKey) throw new \Exception("Session key is required.");
104
105
        $this->sessionKey = $sessionKey;
106
        $this->sessionKeyCartItems = $this->sessionKey . '_cart_items';
107
        $this->sessionKeyCartConditions = $this->sessionKey . '_cart_conditions';
108
109
        return $this;
110
    }
111
112
    /**
113
     * get instance name of the cart
114
     *
115
     * @return string
116
     */
117
    public function getInstanceName()
118
    {
119
        return $this->instanceName;
120
    }
121
122
    /**
123
     * get an item on a cart by item ID
124
     *
125
     * @param $itemId
126
     * @return mixed
127
     */
128
    public function get($itemId)
129
    {
130
        return $this->getContent()->get($itemId);
131
    }
132
133
    /**
134
     * check if an item exists by item ID
135
     *
136
     * @param $itemId
137
     * @return bool
138
     */
139
    public function has($itemId)
140
    {
141
        return $this->getContent()->has($itemId);
142
    }
143
144
    /**
145
     * add item to the cart, it can be an array or multi dimensional array
146
     *
147
     * @param string|array $id
148
     * @param string $name
149
     * @param float $price
150
     * @param int $quantity
151
     * @param array $attributes
152
     * @param CartCondition|array $conditions
153
     * @param string $associatedModel
154
     * @return $this
155
     * @throws InvalidItemException
156
     */
157
    public function add($id, $name = null, $price = null, $quantity = null, $attributes = array(), $conditions = array(), $associatedModel = null)
158
    {
159
        // if the first argument is an array,
160
        // we will need to call add again
161
        if (is_array($id)) {
162
            // the first argument is an array, now we will need to check if it is a multi dimensional
163
            // array, if so, we will iterate through each item and call add again
164
            if (Helpers::isMultiArray($id)) {
165
                foreach ($id as $item) {
166
                    $this->add(
167
                        $item['id'],
168
                        $item['name'],
169
                        $item['price'],
170
                        $item['quantity'],
171
                        Helpers::issetAndHasValueOrAssignDefault($item['attributes'], array()),
172
                        Helpers::issetAndHasValueOrAssignDefault($item['conditions'], array()),
173
                        Helpers::issetAndHasValueOrAssignDefault($item['associatedModel'], null)
174
                    );
175
                }
176
            } else {
177
                $this->add(
178
                    $id['id'],
179
                    $id['name'],
180
                    $id['price'],
181
                    $id['quantity'],
182
                    Helpers::issetAndHasValueOrAssignDefault($id['attributes'], array()),
183
                    Helpers::issetAndHasValueOrAssignDefault($id['conditions'], array()),
184
                    Helpers::issetAndHasValueOrAssignDefault($item['associatedModel'], null)
0 ignored issues
show
Bug introduced by
The variable $item seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
185
                );
186
            }
187
188
            return $this;
189
        }
190
191
        // validate data
192
        $item = $this->validate(array(
193
            'id' => $id,
194
            'name' => $name,
195
            'price' => Helpers::normalizePrice($price),
196
            'quantity' => $quantity,
197
            'attributes' => new ItemAttributeCollection($attributes),
198
            'conditions' => $conditions,
199
            'associatedModel' => $associatedModel,
200
        ));
201
202
        // get the cart
203
        $cart = $this->getContent();
204
205
        // if the item is already in the cart we will just update it
206
        if ($cart->has($id)) {
207
208
            $this->update($id, $item);
209
        } else {
210
211
            $this->addRow($id, $item);
212
        }
213
214
        $this->currentItemId = $id;
215
216
        return $this;
217
    }
218
219
    /**
220
     * update a cart
221
     *
222
     * @param $id
223
     * @param $data
224
     *
225
     * the $data will be an associative array, you don't need to pass all the data, only the key value
226
     * of the item you want to update on it
227
     * @return bool
228
     */
229
    public function update($id, $data)
230
    {
231
        if ($this->fireEvent('updating', $data) === false) {
232
            return false;
233
        }
234
235
        $cart = $this->getContent();
236
237
        $item = $cart->pull($id);
238
239
        foreach ($data as $key => $value) {
240
            // if the key is currently "quantity" we will need to check if an arithmetic
241
            // symbol is present so we can decide if the update of quantity is being added
242
            // or being reduced.
243
            if ($key == 'quantity') {
244
                // we will check if quantity value provided is array,
245
                // if it is, we will need to check if a key "relative" is set
246
                // and we will evaluate its value if true or false,
247
                // this tells us how to treat the quantity value if it should be updated
248
                // relatively to its current quantity value or just totally replace the value
249
                if (is_array($value)) {
250
                    if (isset($value['relative'])) {
251
                        if ((bool)$value['relative']) {
252
                            $item = $this->updateQuantityRelative($item, $key, $value['value']);
253
                        } else {
254
                            $item = $this->updateQuantityNotRelative($item, $key, $value['value']);
255
                        }
256
                    }
257
                } else {
258
                    $item = $this->updateQuantityRelative($item, $key, $value);
259
                }
260
            } elseif ($key == 'attributes') {
261
                $item[$key] = new ItemAttributeCollection($value);
262
            } else {
263
                $item[$key] = $value;
264
            }
265
        }
266
267
        $cart->put($id, $item);
268
269
        $this->save($cart);
270
271
        $this->fireEvent('updated', $item);
272
        return true;
273
    }
274
275
    /**
276
     * add condition on an existing item on the cart
277
     *
278
     * @param int|string $productId
279
     * @param CartCondition $itemCondition
280
     * @return $this
281
     */
282
    public function addItemCondition($productId, $itemCondition)
283
    {
284
        if ($product = $this->get($productId)) {
285
            $conditionInstance = "\\Darryldecode\\Cart\\CartCondition";
286
287
            if ($itemCondition instanceof $conditionInstance) {
288
                // we need to copy first to a temporary variable to hold the conditions
289
                // to avoid hitting this error "Indirect modification of overloaded element of Darryldecode\Cart\ItemCollection has no effect"
290
                // this is due to laravel Collection instance that implements Array Access
291
                // // see link for more info: http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect
292
                $itemConditionTempHolder = $product['conditions'];
293
294
                if (is_array($itemConditionTempHolder)) {
295
                    array_push($itemConditionTempHolder, $itemCondition);
296
                } else {
297
                    $itemConditionTempHolder = $itemCondition;
298
                }
299
300
                $this->update($productId, array(
301
                    'conditions' => $itemConditionTempHolder // the newly updated conditions
302
                ));
303
            }
304
        }
305
306
        return $this;
307
    }
308
309
    /**
310
     * removes an item on cart by item ID
311
     *
312
     * @param $id
313
     * @return bool
314
     */
315
    public function remove($id)
316
    {
317
        $cart = $this->getContent();
318
319
        if ($this->fireEvent('removing', $id) === false) {
320
            return false;
321
        }
322
323
        $cart->forget($id);
324
325
        $this->save($cart);
326
327
        $this->fireEvent('removed', $id);
328
        return true;
329
    }
330
331
    /**
332
     * clear cart
333
     * @return bool
334
     */
335
    public function clear()
336
    {
337
        if ($this->fireEvent('clearing') === false) {
338
            return false;
339
        }
340
341
        $this->session->put(
342
            $this->sessionKeyCartItems,
343
            array()
344
        );
345
346
        $this->fireEvent('cleared');
347
        return true;
348
    }
349
350
    /**
351
     * add a condition on the cart
352
     *
353
     * @param CartCondition|array $condition
354
     * @return $this
355
     * @throws InvalidConditionException
356
     */
357
    public function condition($condition)
358
    {
359
        if (is_array($condition)) {
360
            foreach ($condition as $c) {
361
                $this->condition($c);
362
            }
363
364
            return $this;
365
        }
366
367
        if (!$condition instanceof CartCondition) throw new InvalidConditionException('Argument 1 must be an instance of \'Darryldecode\Cart\CartCondition\'');
368
369
        $conditions = $this->getConditions();
370
371
        // Check if order has been applied
372
        if ($condition->getOrder() == 0) {
373
            $last = $conditions->last();
374
            $condition->setOrder(!is_null($last) ? $last->getOrder() + 1 : 1);
375
        }
376
377
        $conditions->put($condition->getName(), $condition);
378
379
        $conditions = $conditions->sortBy(function ($condition, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
380
            return $condition->getOrder();
381
        });
382
383
        $this->saveConditions($conditions);
384
385
        return $this;
386
    }
387
388
    /**
389
     * get conditions applied on the cart
390
     *
391
     * @return CartConditionCollection
392
     */
393
    public function getConditions()
394
    {
395
        return new CartConditionCollection($this->session->get($this->sessionKeyCartConditions));
396
    }
397
398
    /**
399
     * get condition applied on the cart by its name
400
     *
401
     * @param $conditionName
402
     * @return CartCondition
403
     */
404
    public function getCondition($conditionName)
405
    {
406
        return $this->getConditions()->get($conditionName);
407
    }
408
409
    /**
410
     * Get all the condition filtered by Type
411
     * Please Note that this will only return condition added on cart bases, not those conditions added
412
     * specifically on an per item bases
413
     *
414
     * @param $type
415
     * @return CartConditionCollection
416
     */
417
    public function getConditionsByType($type)
418
    {
419
        return $this->getConditions()->filter(function (CartCondition $condition) use ($type) {
420
            return $condition->getType() == $type;
421
        });
422
    }
423
424
425
    /**
426
     * Remove all the condition with the $type specified
427
     * Please Note that this will only remove condition added on cart bases, not those conditions added
428
     * specifically on an per item bases
429
     *
430
     * @param $type
431
     * @return $this
432
     */
433
    public function removeConditionsByType($type)
434
    {
435
        $this->getConditionsByType($type)->each(function ($condition) {
436
            $this->removeCartCondition($condition->getName());
437
        });
438
    }
439
440
441
    /**
442
     * removes a condition on a cart by condition name,
443
     * this can only remove conditions that are added on cart bases not conditions that are added on an item/product.
444
     * If you wish to remove a condition that has been added for a specific item/product, you may
445
     * use the removeItemCondition(itemId, conditionName) method instead.
446
     *
447
     * @param $conditionName
448
     * @return void
449
     */
450
    public function removeCartCondition($conditionName)
451
    {
452
        $conditions = $this->getConditions();
453
454
        $conditions->pull($conditionName);
455
456
        $this->saveConditions($conditions);
457
    }
458
459
    /**
460
     * remove a condition that has been applied on an item that is already on the cart
461
     *
462
     * @param $itemId
463
     * @param $conditionName
464
     * @return bool
465
     */
466
    public function removeItemCondition($itemId, $conditionName)
467
    {
468
        if (!$item = $this->getContent()->get($itemId)) {
469
            return false;
470
        }
471
472
        if ($this->itemHasConditions($item)) {
473
            // NOTE:
474
            // we do it this way, we get first conditions and store
475
            // it in a temp variable $originalConditions, then we will modify the array there
476
            // and after modification we will store it again on $item['conditions']
477
            // This is because of ArrayAccess implementation
478
            // see link for more info: http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect
479
480
            $tempConditionsHolder = $item['conditions'];
481
482
            // if the item's conditions is in array format
483
            // we will iterate through all of it and check if the name matches
484
            // to the given name the user wants to remove, if so, remove it
485
            if (is_array($tempConditionsHolder)) {
486
                foreach ($tempConditionsHolder as $k => $condition) {
487
                    if ($condition->getName() == $conditionName) {
488
                        unset($tempConditionsHolder[$k]);
489
                    }
490
                }
491
492
                $item['conditions'] = $tempConditionsHolder;
493
            }
494
495
            // if the item condition is not an array, we will check if it is
496
            // an instance of a Condition, if so, we will check if the name matches
497
            // on the given condition name the user wants to remove, if so,
498
            // lets just make $item['conditions'] an empty array as there's just 1 condition on it anyway
499
            else {
500
                $conditionInstance = "Darryldecode\\Cart\\CartCondition";
501
502
                if ($item['conditions'] instanceof $conditionInstance) {
503
                    if ($tempConditionsHolder->getName() == $conditionName) {
504
                        $item['conditions'] = array();
505
                    }
506
                }
507
            }
508
        }
509
510
        $this->update($itemId, array(
511
            'conditions' => $item['conditions']
512
        ));
513
514
        return true;
515
    }
516
517
    /**
518
     * remove all conditions that has been applied on an item that is already on the cart
519
     *
520
     * @param $itemId
521
     * @return bool
522
     */
523
    public function clearItemConditions($itemId)
524
    {
525
        if (!$item = $this->getContent()->get($itemId)) {
526
            return false;
527
        }
528
529
        $this->update($itemId, array(
530
            'conditions' => array()
531
        ));
532
533
        return true;
534
    }
535
536
    /**
537
     * clears all conditions on a cart,
538
     * this does not remove conditions that has been added specifically to an item/product.
539
     * If you wish to remove a specific condition to a product, you may use the method: removeItemCondition($itemId, $conditionName)
540
     *
541
     * @return void
542
     */
543
    public function clearCartConditions()
544
    {
545
        $this->session->put(
546
            $this->sessionKeyCartConditions,
547
            array()
548
        );
549
    }
550
551
    /**
552
     * get cart sub total without conditions
553
     * @param bool $formatted
554
     * @return float
555
     */
556
    public function getSubTotalWithoutConditions($formatted = true)
557
    {
558
        $cart = $this->getContent();
559
560
        $sum = $cart->sum(function ($item) {
561
            return $item->getPriceSum();
562
        });
563
564
        return Helpers::formatValue(floatval($sum), $formatted, $this->config);
565
    }
566
567
    /**
568
     * get cart sub total
569
     * @param bool $formatted
570
     * @return float
571
     */
572
    public function getSubTotal($formatted = true)
573
    {
574
        $cart = $this->getContent();
575
576
        $sum = $cart->sum(function (ItemCollection $item) {
577
            return $item->getPriceSumWithConditions(false);
578
        });
579
580
        // get the conditions that are meant to be applied
581
        // on the subtotal and apply it here before returning the subtotal
582
        $conditions = $this
583
            ->getConditions()
584
            ->filter(function (CartCondition $cond) {
585
                return $cond->getTarget() === 'subtotal';
586
            });
587
588
        // if there is no conditions, lets just return the sum
589
        if (!$conditions->count()) return Helpers::formatValue(floatval($sum), $formatted, $this->config);
590
591
        // there are conditions, lets apply it
592
        $newTotal = 0.00;
593
        $process = 0;
594
595 View Code Duplication
        $conditions->each(function (CartCondition $cond) use ($sum, &$newTotal, &$process) {
596
597
            // if this is the first iteration, the toBeCalculated
598
            // should be the sum as initial point of value.
599
            $toBeCalculated = ($process > 0) ? $newTotal : $sum;
600
601
            $newTotal = $cond->applyCondition($toBeCalculated);
602
603
            $process++;
604
        });
605
606
        return Helpers::formatValue(floatval($newTotal), $formatted, $this->config);
607
    }
608
609
    /**
610
     * the new total in which conditions are already applied
611
     *
612
     * @return float
613
     */
614
    public function getTotal()
615
    {
616
        $subTotal = $this->getSubTotal(false);
617
618
        $newTotal = 0.00;
619
620
        $process = 0;
621
622
        $conditions = $this
623
            ->getConditions()
624
            ->filter(function (CartCondition $cond) {
625
                return $cond->getTarget() === 'total';
626
            });
627
628
        // if no conditions were added, just return the sub total
629
        if (!$conditions->count()) {
630
            return Helpers::formatValue($subTotal, $this->config['format_numbers'], $this->config);
631
        }
632
633
        $conditions
634 View Code Duplication
            ->each(function (CartCondition $cond) use ($subTotal, &$newTotal, &$process) {
635
                $toBeCalculated = ($process > 0) ? $newTotal : $subTotal;
636
637
                $newTotal = $cond->applyCondition($toBeCalculated);
638
639
                $process++;
640
            });
641
642
        return Helpers::formatValue($newTotal, $this->config['format_numbers'], $this->config);
643
    }
644
645
    /**
646
     * get total quantity of items in the cart
647
     *
648
     * @return int
649
     */
650
    public function getTotalQuantity()
651
    {
652
        $items = $this->getContent();
653
654
        if ($items->isEmpty()) return 0;
655
656
        $count = $items->sum(function ($item) {
657
            return $item['quantity'];
658
        });
659
660
        return $count;
661
    }
662
663
    /**
664
     * get the cart
665
     *
666
     * @return CartCollection
667
     */
668
    public function getContent()
669
    {
670
        return (new CartCollection($this->session->get($this->sessionKeyCartItems)));
671
    }
672
673
    /**
674
     * check if cart is empty
675
     *
676
     * @return bool
677
     */
678
    public function isEmpty()
679
    {
680
        $cart = new CartCollection($this->session->get($this->sessionKeyCartItems));
681
682
        return $cart->isEmpty();
683
    }
684
685
    /**
686
     * validate Item data
687
     *
688
     * @param $item
689
     * @return array $item;
690
     * @throws InvalidItemException
691
     */
692 View Code Duplication
    protected function validate($item)
693
    {
694
        $rules = array(
695
            'id' => 'required',
696
            'price' => 'required|numeric',
697
            'quantity' => 'required|numeric|min:1',
698
            'name' => 'required',
699
        );
700
701
        $validator = CartItemValidator::make($item, $rules);
702
703
        if ($validator->fails()) {
704
            throw new InvalidItemException($validator->messages()->first());
705
        }
706
707
        return $item;
708
    }
709
710
    /**
711
     * add row to cart collection
712
     *
713
     * @param $id
714
     * @param $item
715
     * @return bool
716
     */
717
    protected function addRow($id, $item)
718
    {
719
        if ($this->fireEvent('adding', $item) === false) {
720
            return false;
721
        }
722
723
        $cart = $this->getContent();
724
725
        $cart->put($id, new ItemCollection($item, $this->config));
726
727
        $this->save($cart);
728
729
        $this->fireEvent('added', $item);
730
731
        return true;
732
    }
733
734
    /**
735
     * save the cart
736
     *
737
     * @param $cart CartCollection
738
     */
739
    protected function save($cart)
740
    {
741
        $this->session->put($this->sessionKeyCartItems, $cart);
742
    }
743
744
    /**
745
     * save the cart conditions
746
     *
747
     * @param $conditions
748
     */
749
    protected function saveConditions($conditions)
750
    {
751
        $this->session->put($this->sessionKeyCartConditions, $conditions);
752
    }
753
754
    /**
755
     * check if an item has condition
756
     *
757
     * @param $item
758
     * @return bool
759
     */
760
    protected function itemHasConditions($item)
761
    {
762
        if (!isset($item['conditions'])) return false;
763
764
        if (is_array($item['conditions'])) {
765
            return count($item['conditions']) > 0;
766
        }
767
768
        $conditionInstance = "Darryldecode\\Cart\\CartCondition";
769
770
        if ($item['conditions'] instanceof $conditionInstance) return true;
771
772
        return false;
773
    }
774
775
    /**
776
     * update a cart item quantity relative to its current quantity
777
     *
778
     * @param $item
779
     * @param $key
780
     * @param $value
781
     * @return mixed
782
     */
783
    protected function updateQuantityRelative($item, $key, $value)
784
    {
785
        if (preg_match('/\-/', $value) == 1) {
786
            $value = (int)str_replace('-', '', $value);
787
788
            // we will not allowed to reduced quantity to 0, so if the given value
789
            // would result to item quantity of 0, we will not do it.
790
            if (($item[$key] - $value) > 0) {
791
                $item[$key] -= $value;
792
            }
793
        } elseif (preg_match('/\+/', $value) == 1) {
794
            $item[$key] += (int)str_replace('+', '', $value);
795
        } else {
796
            $item[$key] += (int)$value;
797
        }
798
799
        return $item;
800
    }
801
802
    /**
803
     * update cart item quantity not relative to its current quantity value
804
     *
805
     * @param $item
806
     * @param $key
807
     * @param $value
808
     * @return mixed
809
     */
810
    protected function updateQuantityNotRelative($item, $key, $value)
811
    {
812
        $item[$key] = (int)$value;
813
814
        return $item;
815
    }
816
817
    /**
818
     * Setter for decimals. Change value on demand.
819
     * @param $decimals
820
     */
821
    public function setDecimals($decimals)
822
    {
823
        $this->decimals = $decimals;
0 ignored issues
show
Bug introduced by
The property decimals does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
824
    }
825
826
    /**
827
     * Setter for decimals point. Change value on demand.
828
     * @param $dec_point
829
     */
830
    public function setDecPoint($dec_point)
831
    {
832
        $this->dec_point = $dec_point;
0 ignored issues
show
Bug introduced by
The property dec_point does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
833
    }
834
835
    public function setThousandsSep($thousands_sep)
836
    {
837
        $this->thousands_sep = $thousands_sep;
0 ignored issues
show
Bug introduced by
The property thousands_sep does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
838
    }
839
840
    /**
841
     * @param $name
842
     * @param $value
843
     * @return mixed
844
     */
845
    protected function fireEvent($name, $value = [])
846
    {
847
        return $this->events->dispatch($this->getInstanceName() . '.' . $name, array_values([$value, $this]));
848
    }
849
850
    /**
851
     * Associate the cart item with the given id with the given model.
852
     *
853
     * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
854
     * @param mixed  $model
855
     *
856
     * @return void
857
     */
858
    public function associate($model)
859
    {
860
        if (is_string($model) && !class_exists($model)) {
861
            throw new UnknownModelException("The supplied model {$model} does not exist.");
862
        }
863
864
        $cart = $this->getContent();
865
866
        $item = $cart->pull($this->currentItemId);
867
868
        $item['associatedModel'] = $model;
869
870
        $cart->put($this->currentItemId, new ItemCollection($item, $this->config));
871
872
        $this->save($cart);
873
874
        return $this;
875
    }
876
}
877