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 |
||
7 | abstract class Dataset { |
||
8 | |||
9 | protected $definition = null, $handlers = [], $data = []; |
||
10 | |||
11 | # Add handler |
||
12 | |||
13 | protected function addHandler(string $name, callable $handler) { |
||
19 | |||
20 | # Constructor |
||
21 | |||
22 | public function __construct() { |
||
28 | |||
29 | # Reset data |
||
30 | |||
31 | public function reset() { |
||
49 | |||
50 | # Update data |
||
51 | |||
52 | public function update(array $data) { |
||
78 | |||
79 | # Cast data |
||
80 | |||
81 | public function cast(array $data) { |
||
96 | |||
97 | # Return data |
||
98 | |||
99 | public function data() { |
||
103 | |||
104 | # Return param value |
||
105 | |||
106 | public function get(string $name) { |
||
110 | |||
111 | # Getter |
||
112 | |||
113 | public function __get(string $name) { |
||
117 | } |
||
118 | } |
||
119 |
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.