Type   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 2
Metric Value
eloc 18
dl 0
loc 69
ccs 25
cts 25
cp 1
rs 10
c 3
b 1
f 2
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A __construct() 0 4 1
A getName() 0 3 1
A hasName() 0 3 3
A getUniqueId() 0 3 2
A checkMatches() 0 7 3
A matches() 0 5 3
A jsonSerialize() 0 3 1
A equals() 0 4 2
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\type;
12
13
/**
14
 * General Type.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class Type implements TypeInterface
19
{
20
    /**
21
     * @var int|string
22
     */
23
    protected $id;
24
25
    /**
26
     * @var string
27
     */
28
    protected $name;
29
30 47
    public function __construct($id, $name = null)
31
    {
32 47
        $this->id = $id;
33 47
        $this->name = $name;
34 47
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 7
    public function getId()
40
    {
41 7
        return $this->id;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 29
    public function getName(): ?string
48
    {
49 29
        return $this->name;
50
    }
51
52 2
    public function hasName(): bool
53
    {
54 2
        return !empty($this->name) && $this->name !== self::ANY && $this->name !== self::NONE;
55
    }
56
57 2
    public function getUniqueId()
58
    {
59 2
        return $this->hasName() ? $this->name : $this->id;
60
    }
61
62 2
    public function equals(TypeInterface $other): bool
63
    {
64 2
        return $this->id === $other->getId() &&
65 2
            $this->name === $other->getName();
66
    }
67
68 22
    public function matches(TypeInterface $other): bool
69
    {
70 22
        return $this->id === self::ANY || $other->getId() === self::ANY
71 18
            ? $this->checkMatches($this->name, $other->getName())
72 22
            : $this->checkMatches($this->id, $other->getId());
73
    }
74
75 22
    protected function checkMatches($lhs, $rhs)
76
    {
77 22
        if ($lhs === self::NONE || $rhs === self::NONE) {
78 1
            return false;
79
        }
80
81 22
        return (string) $lhs === (string) $rhs;
82
    }
83
84 1
    public function jsonSerialize(): array
85
    {
86 1
        return array_filter(get_object_vars($this));
87
    }
88
}
89