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

Type   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80.95%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 63
ccs 17
cts 21
cp 0.8095
rs 10
c 1
b 1
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A getId() 0 4 1
A getUniqueId() 0 4 2
A equals() 0 5 2
A matches() 0 6 3
A jsonSerialize() 0 7 1
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