Completed
Push — master ( b41e63...77c848 )
by Andrii
02:21
created

AbstractTarget::matches()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\target;
12
13
/**
14
 * @see TargetInterface
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
abstract class AbstractTarget implements TargetInterface
19
{
20
    /**
21
     * @var int|string
22
     */
23
    protected $id;
24
25
    /**
26
     * @var string
27
     */
28
    protected $type;
29
30 9
    public function __construct($id, $type)
31
    {
32 9
        $this->id = $id;
33 9
        $this->type = $type;
34 9
    }
35
36
    /**
37
     * @return int
38
     */
39 3
    public function getId()
40
    {
41 3
        return $this->id;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    public function getType()
48
    {
49 2
        return $this->type;
50
    }
51
52
    /**
53
     * @return string
54
     */
55 7
    public function getUniqueId()
56
    {
57 7
        return $this->type . ':' . $this->id;
58
    }
59
60
    /**
61
     * @return bool
62
     */
63 6
    public function equals(TargetInterface $other): bool
64
    {
65 6
        return $this->getUniqueId() === $other->getUniqueId();
66
    }
67
68
    /**
69
     * @return bool
70
     */
71 1
    public function matches(TargetInterface $other): bool
72
    {
73 1
        return $this->checkMatches($other) || $other->checkMatches($this);
74
    }
75
76 1
    public function checkMatches(TargetInterface $other): bool
77
    {
78 1
        if ($this->id === null) {
79 1
            if ($this->type === null) {
80 1
                return true;
81
            }
82
            return (string) $this->type === (string) $other->getType();
83 1
        }
84
85
        if ($this->type === null) {
86 1
            return (string) $this->id === (string) $other->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface hiqdev\php\billing\target\TargetInterface 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...
87
        }
88
89
        return $this->equals($other);
90 1
    }
91
92
    public function jsonSerialize()
93
    {
94
        return get_object_vars($this);
95
    }
96
}
97