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 |
||
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 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $name; |
||
34 | |||
35 | 10 | public function __construct($id, $type, $name = null) |
|
36 | { |
||
37 | 10 | $this->id = $id; |
|
38 | 10 | $this->type = $type; |
|
39 | 10 | $this->name = $name; |
|
40 | 10 | } |
|
41 | |||
42 | /** |
||
43 | * @return int |
||
44 | */ |
||
45 | 9 | public function getId() |
|
46 | { |
||
47 | 9 | return $this->id; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 9 | public function getType() |
|
54 | { |
||
55 | 9 | return $this->type; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function getName() |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | 6 | public function getUniqueId() |
|
70 | { |
||
71 | 6 | $id = $this->getId(); |
|
72 | 6 | $type = $this->getType(); |
|
73 | |||
74 | 6 | return ($type === self::ANY ? '' : $type) . ':' . ($id === self::ANY ? '' : $id); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * @return bool |
||
79 | */ |
||
80 | 2 | public function equals(TargetInterface $other): bool |
|
81 | { |
||
82 | 2 | return $this->getUniqueId() === $other->getUniqueId(); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return bool |
||
87 | */ |
||
88 | 7 | public function matches(TargetInterface $other): bool |
|
89 | { |
||
90 | 7 | return $this->checkMatches($other) || $other->checkMatches($this); |
|
91 | } |
||
92 | |||
93 | 7 | public function checkMatches(TargetInterface $other): bool |
|
94 | { |
||
95 | 7 | if ($this->id === self::ANY) { |
|
96 | 1 | if ($this->type === self::ANY) { |
|
97 | 1 | return true; |
|
98 | } |
||
99 | |||
100 | 1 | return $this->matchTypes($other); |
|
101 | } |
||
102 | |||
103 | 7 | if ($this->type === self::ANY) { |
|
104 | return $this->matchIds($other); |
||
105 | } |
||
106 | |||
107 | 7 | return $this->matchIds($other) && $this->matchTypes($other); |
|
108 | } |
||
109 | |||
110 | 7 | protected function matchIds(TargetInterface $other) |
|
111 | { |
||
112 | 7 | return $this->matchStrings($this->id, $other->getId()); |
|
113 | } |
||
114 | |||
115 | 7 | protected function matchTypes(TargetInterface $other) |
|
116 | { |
||
117 | 7 | return $this->matchStrings($this->type, $other->getType()); |
|
118 | } |
||
119 | |||
120 | 7 | View Code Duplication | protected function matchStrings($lhs, $rhs) |
|
|||
121 | { |
||
122 | 7 | if ($lhs === self::NONE || $rhs === self::NONE) { |
|
123 | 1 | return false; |
|
124 | } |
||
125 | |||
126 | 7 | return (string) $lhs === (string) $rhs; |
|
127 | } |
||
128 | |||
129 | public function jsonSerialize() |
||
133 | } |
||
134 |
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.