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 |
||
10 | class Direction |
||
11 | { |
||
12 | const FORWARD = 'forward'; |
||
13 | const BACKWARD = 'backward'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $direction; |
||
19 | |||
20 | /** |
||
21 | * Direction constructor. |
||
22 | * |
||
23 | * @param string $direction |
||
24 | */ |
||
25 | 17 | public function __construct($direction) |
|
29 | |||
30 | /** |
||
31 | * @param $direction |
||
32 | * @return string |
||
33 | */ |
||
34 | 17 | View Code Duplication | protected static function validate($direction) |
42 | |||
43 | /** |
||
44 | * @return string |
||
45 | */ |
||
46 | 10 | public function __toString() |
|
50 | |||
51 | /** |
||
52 | * @return bool |
||
53 | */ |
||
54 | 2 | public function forward() |
|
58 | |||
59 | /** |
||
60 | * @return bool |
||
61 | */ |
||
62 | 17 | public function backward() |
|
66 | |||
67 | /** |
||
68 | * @return static |
||
69 | */ |
||
70 | public function inverse() |
||
74 | } |
||
75 |
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.