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 |
||
24 | class ReasonUserCommand extends AbstractConnectionCommand |
||
25 | { |
||
26 | /** |
||
27 | * Description of the login parameter |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $parameterLoginDescription; |
||
32 | |||
33 | /** |
||
34 | * Description of the reason parameter. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $parameterReasonDescription; |
||
39 | |||
40 | /** |
||
41 | * Description of the command. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $description; |
||
46 | |||
47 | /** |
||
48 | * Message to display in chat. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $chatMessage; |
||
53 | |||
54 | /** |
||
55 | * Name of the dedicated function to call. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $functionName; |
||
60 | |||
61 | /** |
||
62 | * ReasonUserCommand constructor. |
||
63 | * |
||
64 | * @param $command |
||
65 | * @param $permission |
||
66 | * @param array $aliases |
||
67 | * @param $functionName |
||
68 | * @param $parameterLoginDescription |
||
69 | * @param $parameterReasonDescription |
||
70 | * @param AdminGroups $adminGroupsHelper |
||
71 | * @param Factory $factory |
||
72 | * @param ChatNotification $chatNotification |
||
73 | * @param PlayerStorage $playerStorage |
||
74 | * @param LoggerInterface $logger |
||
75 | * @param Time $timeHelper |
||
76 | */ |
||
77 | 1 | View Code Duplication | public function __construct( |
|
|||
78 | $command, |
||
79 | $permission, |
||
80 | array $aliases = [], |
||
81 | $functionName, |
||
82 | $parameterLoginDescription, |
||
83 | $parameterReasonDescription, |
||
84 | AdminGroups $adminGroupsHelper, |
||
85 | Factory $factory, |
||
86 | ChatNotification $chatNotification, |
||
87 | PlayerStorage $playerStorage, |
||
88 | LoggerInterface $logger, |
||
89 | Time $timeHelper |
||
90 | ) { |
||
91 | 1 | parent::__construct( |
|
92 | 1 | $command, |
|
93 | $permission, |
||
94 | $aliases, |
||
95 | $adminGroupsHelper, |
||
96 | $factory, |
||
97 | $chatNotification, |
||
98 | $playerStorage, |
||
99 | $logger, |
||
100 | $timeHelper |
||
101 | ); |
||
102 | |||
103 | 1 | $this->description = 'expansion_admin_chat.'.strtolower($functionName).'.description'; |
|
104 | 1 | $this->chatMessage = 'expansion_admin_chat.'.strtolower($functionName).'.msg'; |
|
105 | 1 | $this->functionName = $functionName; |
|
106 | 1 | $this->parameterLoginDescription = $parameterLoginDescription; |
|
107 | 1 | $this->parameterReasonDescription = $parameterReasonDescription; |
|
108 | 1 | } |
|
109 | |||
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | 1 | protected function configure() |
|
125 | |||
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | 1 | public function execute($login, InputInterface $input) |
|
162 | } |
||
163 |
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.