Completed
Push — master ( 791681...fd1dbe )
by Andrii
02:06
created

Type::matches()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 4
nop 1
crap 3
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-2018, 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 10
    public function __construct($id, $name = null)
31
    {
32 10
        $this->id = $id;
33 10
        $this->name = $name;
34 10
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 4
    public function getId()
40
    {
41 4
        return $this->id;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 8
    public function getName()
48
    {
49 8
        return $this->name;
50
    }
51
52 2
    public function getUniqueId()
53
    {
54 2
        return $this->getId() ?: $this->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
55
    }
56
57
    /**
58
     * @return string
59
     */
60 2
    public function equals(TypeInterface $other)
61
    {
62 2
        return $this->id === $other->getId() &&
63 2
            $this->name === $other->getName();
0 ignored issues
show
Bug introduced by
Consider using $other->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
64
    }
65
66 6
    public function matches(TypeInterface $other)
67
    {
68 6
        return $this->id === null || $other->id === null
0 ignored issues
show
Bug introduced by
Accessing id on the interface hiqdev\php\billing\type\TypeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
69 3
            ? (string) $this->name === (string) $other->name
0 ignored issues
show
Bug introduced by
Accessing name on the interface hiqdev\php\billing\type\TypeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
70 6
            : (string) $this->id === (string) $other->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface hiqdev\php\billing\type\TypeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
71
    }
72
73
    public function jsonSerialize()
74
    {
75
        return array_filter([
76
            'name' => $this->name,
77
            'id' => $this->id,
78
        ]);
79
    }
80
}
81