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 |
||
27 | View Code Duplication | abstract class IntegerObject implements JsonSerializable |
|
|
|||
28 | { |
||
29 | /** |
||
30 | * Contains the value |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $value; |
||
35 | |||
36 | /** |
||
37 | * @param int $value |
||
38 | * |
||
39 | * @throws InvalidArgumentException If given data is not an integer |
||
40 | */ |
||
41 | public function __construct($value) |
||
49 | |||
50 | /** |
||
51 | * Sets the value |
||
52 | * |
||
53 | * @param int $value |
||
54 | * |
||
55 | * @return void |
||
56 | */ |
||
57 | protected function setValue($value) |
||
61 | |||
62 | /** |
||
63 | * Getter for value |
||
64 | * |
||
65 | * @return int |
||
66 | */ |
||
67 | public function getValue() |
||
71 | |||
72 | /** |
||
73 | * @inheritDoc |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function jsonSerialize() |
||
81 | |||
82 | /** |
||
83 | * Returns string representation |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function __toString() |
||
91 | } |
||
92 |
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.