Completed
Push — master ( fd1dbe...d544ae )
by Andrii
03:02
created

Type::checkMatches()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
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 6
    public function getId()
40
    {
41 6
        return $this->id;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 10
    public function getName()
48
    {
49 10
        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 === self::ANY || $other->id === self::ANY
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
            ? $this->checkMatches($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...
70 6
            : $this->checkMatches($this->id, $other->getId());
71
    }
72
73 6 View Code Duplication
    protected function checkMatches($lhs, $rhs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75 6
        if ($lhs === self::NONE || $rhs === self::NONE) {
76 1
            return false;
77
        }
78
79 6
        return (string) $lhs === (string) $rhs;
80
    }
81
82
    public function jsonSerialize()
83
    {
84
        return array_filter([
85
            'name' => $this->name,
86
            'id' => $this->id,
87
        ]);
88
    }
89
}
90