CartOptionsMagicMethodsTrait   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 6
Bugs 3 Features 1
Metric Value
wmc 19
c 6
b 3
f 1
lcom 0
cbo 3
dl 0
loc 77
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 8 2
C __set() 0 30 15
A __isset() 0 8 2
1
<?php
2
3
namespace LukePOLO\LaraCart\Traits;
4
5
use LukePOLO\LaraCart\CartItem;
6
use LukePOLO\LaraCart\Exceptions\InvalidPrice;
7
use LukePOLO\LaraCart\Exceptions\InvalidQuantity;
8
use LukePOLO\LaraCart\Exceptions\InvalidTaxableValue;
9
10
/**
11
 * Class CartOptionsMagicMethodsTrait.
12
 */
13
trait CartOptionsMagicMethodsTrait
14
{
15
    public $options = [];
16
17
    /**
18
     * Magic Method allows for user input as an object.
19
     *
20
     * @param $option
21
     *
22
     * @return mixed | null
23
     */
24
    public function __get($option)
25
    {
26
        try {
27
            return $this->$option;
28
        } catch (\ErrorException $e) {
0 ignored issues
show
Unused Code introduced by
catch (\ErrorException $...s->options, $option); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
29
            return array_get($this->options, $option);
30
        }
31
    }
32
33
    /**
34
     * Magic Method allows for user input to set a value inside the options array.
35
     *
36
     * @param $option
37
     * @param $value
38
     *
39
     * @throws InvalidPrice
40
     * @throws InvalidQuantity
41
     * @throws InvalidTaxableValue
42
     */
43
    public function __set($option, $value)
44
    {
45
        switch ($option) {
46
            case CartItem::ITEM_QTY:
47
                if (!is_numeric($value) || $value < 0) {
48
                    throw new InvalidQuantity('The quantity must be a valid number');
49
                }
50
                break;
51
            case CartItem::ITEM_PRICE:
52
                if (!is_numeric($value)) {
53
                    throw new InvalidPrice('The price must be a valid number');
54
                }
55
                break;
56
            case CartItem::ITEM_TAX:
57
                if (!empty($value) && (!is_numeric($value) || $value > 1)) {
58
                    throw new InvalidTaxableValue('The tax must be a float less than 1');
59
                }
60
                break;
61
            case CartItem::ITEM_TAXABLE:
62
                if (!is_bool($value) && $value != 0 && $value != 1) {
63
                    throw new InvalidTaxableValue('The taxable option must be a boolean');
64
                }
65
                break;
66
        }
67
        array_set($this->options, $option, $value);
68
69
        if (is_callable([$this, 'generateHash'])) {
70
            $this->generateHash();
0 ignored issues
show
Bug introduced by
It seems like generateHash() 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...
71
        }
72
    }
73
74
    /**
75
     * Magic Method allows for user to check if an option isset.
76
     *
77
     * @param $option
78
     *
79
     * @return bool
80
     */
81
    public function __isset($option)
82
    {
83
        if (!empty($this->options[$option])) {
84
            return true;
85
        } else {
86
            return false;
87
        }
88
    }
89
}
90