TypeSemantics::groupName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace hiqdev\billing\hiapi\type;
4
5
use hiqdev\php\billing\type\Type;
6
use hiqdev\php\billing\type\TypeInterface;
7
8
final class TypeSemantics
9
{
10
    private const MONTHLY  = 'monthly';
11
    private const OVERUSE  = 'overuse';
12
    private const DISCOUNT = 'discount';
13
    private const DEPOSIT  = 'deposit';
14
    private const HARDWARE = 'hardware';
15
16
    /**
17
     * // TODO: Probably not the best place for this method
18
     *
19
     * @return TypeInterface
20
     */
21
    public function createMonthlyType(): TypeInterface
22
    {
23
        return new Type(null, self::MONTHLY . ',' . self::MONTHLY);
24
    }
25
26
    /**
27
     * @param TypeInterface $type
28
     * @return bool
29
     */
30
    public function isMonthly(TypeInterface $type): bool
31
    {
32
        return $this->groupName($type) === self::MONTHLY;
33
    }
34
35
    /**
36
     * @param TypeInterface $type
37 6
     * @return bool
38
     */
39 6
    public function isHardware(TypeInterface $type): bool
40
    {
41
        return $this->localName($type) === self::HARDWARE;
42
    }
43
44
    /**
45
     * @param TypeInterface $type
46
     * @return bool
47
     */
48
    public function isDiscount(TypeInterface $type): bool
49
    {
50
        return $this->groupName($type) === self::DISCOUNT;
51
    }
52
53
    /**
54
     * @param TypeInterface $type
55
     * @return bool
56
     */
57
    public function isOveruse(TypeInterface $type): bool
58
    {
59
        return $this->groupName($type) === self::OVERUSE;
60
    }
61
62
    /**
63
     * @param TypeInterface $type
64 1
     * @return bool
65
     */
66 1
    public function isDeposit(TypeInterface $type): bool
67
    {
68
        return $this->groupName($type) === self::DEPOSIT;
69
    }
70
71
    /**
72
     * @param TypeInterface $type
73 6
     * @return string
74
     */
75 6
    public function groupName(TypeInterface $type): string
76 6
    {
77 3
        $name = $type->getName();
78
        if (strpos($name, ',') !== false) {
79
            [$name,] = explode(',', $name, 2);
80 6
        }
81
82
        return $name;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $name could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
83
    }
84
85
    public function localName(TypeInterface $type): string
86
    {
87
        $name = $type->getName();
88
        if (strpos($name, ',') !== false) {
89
            [,$name] = explode(',', $name, 2);
90
        }
91
92
        return $name;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $name could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
93
    }
94
}
95