Completed
Push — master ( fd7c36...49f0d1 )
by Luke
02:28
created

CartOptionsMagicMethodsTrait::__set()   C

Complexity

Conditions 12
Paths 14

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 30
rs 5.1612
cc 12
eloc 21
nc 14
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use LukePOLO\LaraCart\LaraCart;
10
11
/**
12
 * Class CartOptionsMagicMethodsTrait
13
 *
14
 * @package LukePOLO\LaraCart\Traits
15
 */
16
trait CartOptionsMagicMethodsTrait
17
{
18
    public $options = [];
19
20
    /**
21
     * Magic Method allows for user input as an object
22
     *
23
     * @param $option
24
     *
25
     * @return mixed | null
26
     */
27
    public function __get($option)
28
    {
29
        try {
30
            return $this->$option;
31
        } 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...
32
            return array_get($this->options, $option);
33
        }
34
    }
35
36
    /**
37
     * Magic Method allows for user input to set a value inside the options array
38
     *
39
     * @param $option
40
     * @param $value
41
     *
42
     * @throws InvalidPrice
43
     * @throws InvalidQuantity
44
     * @throws InvalidTaxableValue
45
     */
46
    public function __set($option, $value)
47
    {
48
        switch ($option) {
49
            case CartItem::ITEM_QTY:
50
                if (!is_numeric($value) || $value < 0) {
51
                    throw new InvalidQuantity('The quantity must be a valid number');
52
                }
53
                break;
54
            case CartItem::ITEM_PRICE:
55
                if (!is_numeric($value)) {
56
                    throw new InvalidPrice('The price must be a valid number');
57
                }
58
                break;
59
            case CartItem::ITEM_TAX:
60
                if (!is_numeric($value) || $value > 1) {
61
                    throw new InvalidTaxableValue('The tax must be a float less than 1');
62
                }
63
                break;
64
            case CartItem::ITEM_TAXABLE:
65
                if(!is_bool($value)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
66
                    throw new InvalidTaxableValue('The taxable option must be a boolean');
67
                }
68
                break;
69
        }
70
        array_set($this->options, $option, $value);
71
72
        if (is_callable(array($this, 'generateHash'))) {
73
            $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...
74
        }
75
    }
76
77
    /**
78
     * Magic Method allows for user to check if an option isset
79
     *
80
     * @param $option
81
     *
82
     * @return bool
83
     */
84
    public function __isset($option)
85
    {
86
        if (!empty($this->options[$option])) {
87
            return true;
88
        } else {
89
            return false;
90
        }
91
    }
92
}
93