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

AbstractTarget   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 100
Duplicated Lines 8 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.63%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 8
loc 100
ccs 29
cts 32
cp 0.9063
rs 10
c 1
b 1
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getUniqueId() 0 4 1
A getId() 0 4 1
A getType() 0 4 1
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
        return $this->type . ':' . $this->id;
58
    }
59
60
    /**
61
     * @return bool
62
     */
63 2
    public function equals(TargetInterface $other): bool
64
    {
65 2
        return $this->getUniqueId() === $other->getUniqueId();
66
    }
67
68
    /**
69
     * @return bool
70
     */
71 6
    public function matches(TargetInterface $other): bool
72
    {
73 6
        return $this->checkMatches($other) || $other->checkMatches($this);
74
    }
75
76 6
    public function checkMatches(TargetInterface $other): bool
77
    {
78 6
        if ($this->id === self::ANY) {
79 1
            if ($this->type === self::ANY) {
80 1
                return true;
81
            }
82
83 1
            return $this->matchTypes($other);
84
        }
85
86 6
        if ($this->type === self::ANY) {
87
            return $this->matchIds($other);
88
        }
89
90 6
        return $this->matchIds($other) && $this->matchTypes($other);
91
    }
92
93 6
    protected function matchIds(TargetInterface $other)
94
    {
95 6
        return $this->matchStrings($this->id, $other->getId());
96
    }
97
98 6
    protected function matchTypes(TargetInterface $other)
99
    {
100 6
        return $this->matchStrings($this->type, $other->getType());
101
    }
102
103 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...
104
    {
105 6
        if ($lhs === self::NONE || $rhs === self::NONE) {
106 1
            return false;
107
        }
108
109 6
        return (string) $lhs === (string) $rhs;
110
    }
111
112
113
    public function jsonSerialize()
114
    {
115
        return get_object_vars($this);
116
    }
117
}
118