Completed
Push — master ( 791b1e...fde4e1 )
by Andrii
02:28
created

AbstractTarget   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 103
Duplicated Lines 7.77 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.18%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 20
c 1
b 1
f 0
lcom 1
cbo 1
dl 8
loc 103
ccs 31
cts 34
cp 0.9118
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getId() 0 4 1
A getType() 0 4 1
A getUniqueId() 0 7 3
A equals() 0 4 1
A matches() 0 4 2
B checkMatches() 0 16 5
A matchIds() 0 4 1
A matchTypes() 0 4 1
A matchStrings() 8 8 3
A jsonSerialize() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\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 10
    public function __construct($id, $type)
31
    {
32 10
        $this->id = $id;
33 10
        $this->type = $type;
34 10
    }
35
36
    /**
37
     * @return int
38
     */
39 8
    public function getId()
40
    {
41 8
        return $this->id;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 8
    public function getType()
48
    {
49 8
        return $this->type;
50
    }
51
52
    /**
53
     * @return string
54
     */
55 5
    public function getUniqueId()
56
    {
57 5
        $id = $this->getId();
58 5
        $type = $this->getType();
59
60 5
        return ($type === self::ANY ? '' : $type) . ':' . ($id === self::ANY ? '' : $id);
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $type (string) and self::ANY (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
61
    }
62
63
    /**
64
     * @return bool
65
     */
66 2
    public function equals(TargetInterface $other): bool
67
    {
68 2
        return $this->getUniqueId() === $other->getUniqueId();
69
    }
70
71
    /**
72
     * @return bool
73
     */
74 6
    public function matches(TargetInterface $other): bool
75
    {
76 6
        return $this->checkMatches($other) || $other->checkMatches($this);
77
    }
78
79 6
    public function checkMatches(TargetInterface $other): bool
80
    {
81 6
        if ($this->id === self::ANY) {
82 1
            if ($this->type === self::ANY) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $this->type (string) and self::ANY (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
83 1
                return true;
84
            }
85
86 1
            return $this->matchTypes($other);
87
        }
88
89 6
        if ($this->type === self::ANY) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $this->type (string) and self::ANY (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
90
            return $this->matchIds($other);
91
        }
92
93 6
        return $this->matchIds($other) && $this->matchTypes($other);
94
    }
95
96 6
    protected function matchIds(TargetInterface $other)
97
    {
98 6
        return $this->matchStrings($this->id, $other->getId());
99
    }
100
101 6
    protected function matchTypes(TargetInterface $other)
102
    {
103 6
        return $this->matchStrings($this->type, $other->getType());
104
    }
105
106 6 View Code Duplication
    protected function matchStrings($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...
107
    {
108 6
        if ($lhs === self::NONE || $rhs === self::NONE) {
109 1
            return false;
110
        }
111
112 6
        return (string) $lhs === (string) $rhs;
113
    }
114
115
116
    public function jsonSerialize()
117
    {
118
        return get_object_vars($this);
119
    }
120
}
121