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 |
||
16 | View Code Duplication | class ProgressBarStatusHasBeenUpdated implements ShouldBroadcast |
|
|
|||
17 | { |
||
18 | use Dispatchable, InteractsWithSockets, SerializesModels; |
||
19 | |||
20 | /** |
||
21 | * Id of progress bar to update. |
||
22 | * |
||
23 | * @var |
||
24 | */ |
||
25 | public $id; |
||
26 | |||
27 | /** |
||
28 | * Current progress var value. |
||
29 | * |
||
30 | * @var |
||
31 | */ |
||
32 | public $progress; |
||
33 | |||
34 | /** |
||
35 | * Message to show in progress bar. |
||
36 | * |
||
37 | * @var |
||
38 | */ |
||
39 | public $message; |
||
40 | |||
41 | /** |
||
42 | * Broadcast channel name. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $channelName = 'progress-bar'; |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | public function getChannelName() |
||
55 | |||
56 | /** |
||
57 | * @param string $channelName |
||
58 | */ |
||
59 | public function setChannelName($channelName) |
||
63 | |||
64 | /** |
||
65 | * ProgressBarStatusHasBeenUpdated constructor. |
||
66 | * |
||
67 | * @param $progress |
||
68 | * @param $message |
||
69 | */ |
||
70 | public function __construct($id, $progress, $message) |
||
76 | |||
77 | /** |
||
78 | * Get the channels the event should broadcast on. |
||
79 | * |
||
80 | * @return Channel|array |
||
81 | */ |
||
82 | public function broadcastOn() |
||
86 | } |
||
87 |
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.