Passed
Push — master ( 4bb1d0...373e60 )
by Dmitry
11:42
created

Type::anyId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 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
0 ignored issues
show
Bug introduced by
The type hiqdev\php\billing\type\TypeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
{
20
    /**
21
     * @var int|string|null|float The unique identifier of the type. Can be an integer or string.
22
     *                            Special values:
23
     *                            - `Type::ANY` indicates that the type can match any ID.
24
     *                            - `Type::NONE` indicates that there is no valid ID.
25
     */
26
    protected $id;
27
28
    /**
29
     * @var string|null|float The name of the type. Can be a specific name or one of the special values:
30 47
     *                        - `Type::ANY` indicates that the type can match any name.
31
     *                        - `Type::NONE` indicates that there is no valid name.
32 47
     */
33 47
    protected $name;
34 47
35
    public function __construct($id, $name = self::ANY)
36
    {
37
        $this->id = $id;
38
        $this->name = $name;
39 7
    }
40
41 7
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getId()
45
    {
46
        return $this->id;
47 29
    }
48
49 29
    /**
50
     * {@inheritdoc}
51
     */
52 2
    public function getName(): ?string
53
    {
54 2
        return $this->name;
55
    }
56
57 2
    public function hasName(): bool
58
    {
59 2
        return !empty($this->name) && $this->name !== self::ANY && $this->name !== self::NONE;
60
    }
61
62 2
    public function getUniqueId()
63
    {
64 2
        return $this->hasName() ? $this->name : $this->id;
65 2
    }
66
67
    public function equals(TypeInterface $other): bool
68 22
    {
69
        return $this->id === $other->getId() &&
70 22
            $this->name === $other->getName();
71 18
    }
72 22
73
    public function matches(TypeInterface $other): bool
74
    {
75 22
        return $this->id === self::ANY || $other->getId() === self::ANY
76
            ? $this->checkMatches($this->name, $other->getName())
77 22
            : $this->checkMatches($this->id, $other->getId());
78 1
    }
79
80
    protected function checkMatches($lhs, $rhs)
81 22
    {
82
        if ($lhs === self::NONE || $rhs === self::NONE) {
83
            return false;
84 1
        }
85
86 1
        return (string) $lhs === (string) $rhs;
87
    }
88
89
    public function jsonSerialize(): array
90
    {
91
        return array_filter(get_object_vars($this));
92
    }
93
94
    public function isDefined(): bool
95
    {
96
        return $this->id !== null || $this->name !== null;
97
    }
98
99
    public static function anyId($name): TypeInterface
100
    {
101
        return new static(self::ANY, $name);
102
    }
103
}
104