Completed
Push — master ( dead21...224450 )
by Bram
02:17
created

TaxModifier::getTableTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * PriceModifier.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 31/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
/**
12
 * Class TaxModifier
13
 *
14
 * Adds a configurable tax rate on the receipt
15
 *
16
 * @package Broarm\EventTickets
17
 */
18
class TaxModifier extends PriceModifier
19
{
20
    /**
21
     * Set the tax rate as a percentage
22
     *
23
     * @var string
24
     */
25
    private static $tax_rate = '21';
0 ignored issues
show
Unused Code introduced by
The property $tax_rate is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
27
    /**
28
     * Set if the tax rate is inclusive or exclusive
29
     *
30
     * @var string
31
     */
32
    private static $inclusive = false;
0 ignored issues
show
Unused Code introduced by
The property $inclusive is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
33
34
    /**
35
     * Set the default sort value to a large int so it always shows and calculates as last
36
     *
37
     * @var array
38
     */
39
    private static $defaults = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
40
        'Title' => 'Tax modifier',
41
        'Sort' => 9999
42
    );
43
44
    public function updateTotal(&$total) {
45
        $rate = (float)self::config()->get('tax_rate') / 100;
46
        $tax = $total * $rate;
47
        $this->setPriceModification($tax);
48
        if (!(bool)self::config()->get('inclusive')) {
49
            $total += $tax;
50
        }
51
    }
52
53
    /**
54
     * Show the used tax rate in the table title
55
     *
56
     * @return string
57
     */
58
    public function getTableTitle()
59
    {
60
        return _t(
61
            'TaxModifier.TABLE_TITLE',
62
            '{rate}% BTW',
63
            null,
64
            array(
0 ignored issues
show
Documentation introduced by
array('rate' => (double)...fig()->get('tax_rate')) is of type array<string,double,{"rate":"double"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
                'rate' => (float)self::config()->get('tax_rate')
66
            )
67
        );
68
    }
69
70
    /**
71
     * Show the calculated tax value as a positive value
72
     *
73
     * @return float
74
     */
75
    public function getTableValue()
76
    {
77
        return $this->PriceModification;
78
    }
79
80
    /**
81
     * Create a tax modifier if it does not already exists
82
     *
83
     * @param Reservation $reservation
84
     *
85
     * @return TaxModifier|\DataObject|null|static
86
     */
87
    public static function findOrMake(Reservation $reservation)
88
    {
89
        if (!$modifier = $reservation->PriceModifiers()->find('ClassName', self::class)) {
0 ignored issues
show
Documentation Bug introduced by
The method PriceModifiers does not exist on object<Broarm\EventTickets\Reservation>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
90
            $modifier = self::create();
91
            $modifier->write();
92
        }
93
94
        return $modifier;
95
    }
96
}
97