Completed
Push — master ( 113a8a...459c7d )
by Luke
02:16
created

CartSubItem::getPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace LukePOLO\LaraCart;
4
5
use LukePOLO\LaraCart\Traits\CartOptionsMagicMethodsTrait;
6
7
/**
8
 * Class CartItemOption
9
 *
10
 * @package LukePOLO\LaraCart
11
 */
12
class CartSubItem
13
{
14
    use CartOptionsMagicMethodsTrait;
15
16
    const ITEMS = 'items';
17
18
    public $locale;
19
    public $internationalFormat;
20
21
    private $itemHash;
22
23
    /**
24
     * @param $options
25
     */
26
    public function __construct($options)
27
    {
28
        $this->options['items'] = [];
29
30
        foreach ($options as $option => $value) {
31
            array_set($this->options, $option, $value);
32
        }
33
34
        $this->itemHash = app(LaraCart::HASH, $this->options);
35
    }
36
37
    /**
38
     * Gets the hash for the item
39
     *
40
     * @return mixed
41
     */
42
    public function getHash()
43
    {
44
        return $this->itemHash;
45
    }
46
47
    /**
48
     * Gets the formatted price
49
     *
50
     * @param bool|true $format
51
     *
52
     * @return string
53
     */
54
    public function price($format = true)
55
    {
56
        $price = $this->price;
0 ignored issues
show
Documentation introduced by
The property price does not exist on object<LukePOLO\LaraCart\CartSubItem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
57
58
        if (isset($this->items)) {
59
            foreach ($this->items as $item) {
0 ignored issues
show
Documentation introduced by
The property items does not exist on object<LukePOLO\LaraCart\CartSubItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
                $price += $item->price(false);
61
            }
62
        }
63
64
        return LaraCart::formatMoney($price, $this->locale, $this->internationalFormat, $format);
65
    }
66
}
67