Tax::__get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace FeiMx\Tax\Taxes;
4
5
class Tax
6
{
7
    protected $retention = false;
8
9
    /**
10
     * Create new instance of taxes.
11
     *
12
     * @param bool|string $retention
13
     */
14
    public function __construct($retention = false)
15
    {
16
        $this->retention = $retention;
0 ignored issues
show
Documentation Bug introduced by
It seems like $retention can also be of type string. However, the property $retention is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
17
    }
18
19
    /**
20
     * Get amount percentage for defined tax.
21
     *
22
     * @param string $type Type of the tax amount
0 ignored issues
show
Bug introduced by
There is no parameter named $type. 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...
23
     *
24
     * @return int $percentage Amount of the tax
25
     */
26
    public function percentage(): float
27
    {
28
        $taxName = $this->getTaxName();
29
30
        $type = $this->parseTypePercentage();
31
32
        return $percentage = config(
0 ignored issues
show
Unused Code introduced by
$percentage is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
            "tax.taxes.{$taxName}.{$type}",
34
            config(
35
                "tax.taxes.{$taxName}.".config('tax.fallback')
36
            )
37
        );
38
    }
39
40
    /**
41
     * Convert class to qualified Tax name.
42
     *
43
     * @return string Tax name in lower case
44
     */
45
    protected function getTaxName(): string
46
    {
47
        $className = explode('\\', get_called_class());
48
49
        return strtolower(array_pop($className));
50
    }
51
52
    /**
53
     * Return retention instead of a given type if retention flag is activated.
54
     *
55
     * @param string $type Type of the tax amount
0 ignored issues
show
Bug introduced by
There is no parameter named $type. 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...
56
     *
57
     * @return string Type of the tax amount
58
     */
59
    protected function parseTypePercentage()
60
    {
61
        return is_bool($this->retention) && $this->retention ? 'retention' : $this->retention;
62
    }
63
64
    public function __get($property)
65
    {
66
        if ('name' == $property) {
67
            return $this->getTaxName();
68
        }
69
70
        if (property_exists($this, $property)) {
71
            return $this->{$property};
72
        }
73
74
        throw new \Exception('Property not exists');
75
    }
76
77
    public function __toString()
78
    {
79
        return $this->getTaxName();
80
    }
81
}
82