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 |
||
15 | class BasePlugin extends \PEIP\ABS\Context\ContextPlugin implements \PEIP\INF\Context\ContextPlugin |
||
16 | { |
||
17 | protected $builders = [ |
||
18 | 'include' => 'createContext', |
||
19 | 'plugin' => 'createPlugin', |
||
20 | 'channel' => 'createChannel', |
||
21 | 'publish_subscribe_channel' => 'createSubscribableChannel', |
||
22 | 'service_activator' => 'createServiceActivator', |
||
23 | 'gateway' => 'createGateway', |
||
24 | 'splitter' => 'createSplitter', |
||
25 | 'transformer' => 'createTransformer', |
||
26 | 'router' => 'createRouter', |
||
27 | 'aggregator' => 'createAggregator', |
||
28 | 'wiretap' => 'createWiretap', |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * Creates a pollable channel from a configuration object. |
||
33 | * |
||
34 | * @see XMLContext::doCreateChannel |
||
35 | * |
||
36 | * @param object $config configuration object for the pollable channel. |
||
37 | * |
||
38 | * @return \PEIP\INF\Channel\Channel the created pollable channel instance |
||
39 | */ |
||
40 | public function createChannel($config) |
||
44 | |||
45 | /** |
||
46 | * Creates a subscribable channel from a configuration object. |
||
47 | * |
||
48 | * @see XMLContext::doCreateChannel |
||
49 | * |
||
50 | * @param object $config configuration object for the subscribable channel. |
||
51 | * |
||
52 | * @return \PEIP\INF\Channel\Channel the created subscribable channel instance |
||
53 | */ |
||
54 | public function createSubscribableChannel($config) |
||
58 | |||
59 | /** |
||
60 | * Creates and registers arbitrary channel from a configuration object and additional information. |
||
61 | * |
||
62 | * @param object $config configuration object for the channel. |
||
63 | * @param string $defaultChannelClass the channel class to use if none is set in config |
||
64 | * @param $additionalArguments additional arguments for the channel constructor (without first arg = id) |
||
65 | * |
||
66 | * @return \PEIP\INF\Channel\Channel the created channel instance |
||
67 | */ |
||
68 | View Code Duplication | public function doCreateChannel($config, $defaultChannelClass, array $additionalArguments = []) |
|
79 | |||
80 | /** |
||
81 | * Creates and registers gateway from a configuration object. |
||
82 | * |
||
83 | * @see XMLContext::initNodeBuilders |
||
84 | * |
||
85 | * @param object $config configuration object for the gateway. |
||
86 | * @param string $defaultClass the class to use if none is set in config. |
||
87 | * |
||
88 | * @return object the gateway instance |
||
89 | */ |
||
90 | View Code Duplication | public function createGateway($config, $defaultClass = false) |
|
103 | |||
104 | /** |
||
105 | * Creates and registers router from a configuration object. |
||
106 | * Adds this context instance as channel-resolver to the router if |
||
107 | * none is set in config. |
||
108 | * |
||
109 | * @see XMLContext::resolveChannelName |
||
110 | * @see XMLContext::initNodeBuilders |
||
111 | * |
||
112 | * @param object $config configuration object for the gateway. |
||
113 | * @param string $defaultClass the class to use if none is set in config. |
||
114 | * |
||
115 | * @return object the router instance |
||
116 | */ |
||
117 | View Code Duplication | public function createRouter($config, $defaultClass = false) |
|
126 | |||
127 | /** |
||
128 | * Creates and registers splitter from a configuration object. |
||
129 | * |
||
130 | * @see XMLContext::initNodeBuilders |
||
131 | * @see XMLContext::createReplyMessageHandler |
||
132 | * |
||
133 | * @param object $config configuration object for the splitter. |
||
134 | * |
||
135 | * @return object the splitter instance |
||
136 | */ |
||
137 | public function createSplitter($config) |
||
141 | |||
142 | /** |
||
143 | * Creates and registers transformer from a configuration object. |
||
144 | * |
||
145 | * @see XMLContext::initNodeBuilders |
||
146 | * @see XMLContext::createReplyMessageHandler |
||
147 | * |
||
148 | * @param object $config configuration object for the transformer. |
||
149 | * |
||
150 | * @return object the transformer instance |
||
151 | */ |
||
152 | public function createTransformer($config) |
||
156 | |||
157 | /** |
||
158 | * Creates aggregator from a configuration object. |
||
159 | * |
||
160 | * @see XMLContext::initNodeBuilders |
||
161 | * @see XMLContext::createReplyMessageHandler |
||
162 | * |
||
163 | * @param object $config configuration object for the aggregator. |
||
164 | * |
||
165 | * @return object the aggregator instance |
||
166 | */ |
||
167 | public function createAggregator($config) |
||
171 | |||
172 | /** |
||
173 | * Creates wiretap from a configuration object. |
||
174 | * |
||
175 | * @see XMLContext::initNodeBuilders |
||
176 | * @see XMLContext::createReplyMessageHandler |
||
177 | * |
||
178 | * @param object $config configuration object for the wiretap. |
||
179 | * |
||
180 | * @return object the wiretap instance |
||
181 | */ |
||
182 | public function createWiretap($config) |
||
186 | |||
187 | /** |
||
188 | * Creates a reply-message-handler from a configuration object. |
||
189 | * |
||
190 | * @see XMLContext::initNodeBuilders |
||
191 | * |
||
192 | * @param object $config configuration object for the reply-message-handler. |
||
193 | * @param string $defaultClass the class to use if none is set in config. |
||
194 | * |
||
195 | * @return object the reply-message-handler instance |
||
196 | */ |
||
197 | public function createReplyMessageHandler($config, $defaultClass = false) |
||
201 | |||
202 | /** |
||
203 | * Creates and registers service-activator from a configuration object. |
||
204 | * |
||
205 | * @see XMLContext::initNodeBuilders |
||
206 | * |
||
207 | * @param object $config configuration object for the service-activator. |
||
208 | * @param string $defaultClass the class to use if none is set in config. |
||
209 | * |
||
210 | * @return object the service-activator instance |
||
211 | */ |
||
212 | View Code Duplication | public function createServiceActivator($config, $defaultClass = false) |
|
227 | |||
228 | /** |
||
229 | * Utility method to create arguments for a reply-handler constructor from a config-obect. |
||
230 | * |
||
231 | * @param object $config configuration object to create arguments from. |
||
232 | * |
||
233 | * @return mixed build arguments |
||
234 | */ |
||
235 | View Code Duplication | protected function getReplyHandlerArguments($config) |
|
247 | |||
248 | /** |
||
249 | * Utility method to return a request-channel from a config-obect. |
||
250 | * |
||
251 | * @see XMLContext::doGetChannel |
||
252 | * |
||
253 | * @param object $config configuration object to return request-channel from. |
||
254 | * |
||
255 | * @return \PEIP\INF\Channel\Channel request-channel |
||
256 | */ |
||
257 | protected function getRequestChannel($config) |
||
261 | |||
262 | /** |
||
263 | * Utility method to return a reply-channel from a config-obect. |
||
264 | * |
||
265 | * @see XMLContext::doGetChannel |
||
266 | * |
||
267 | * @param object $config configuration object to return reply-channel from. |
||
268 | * |
||
269 | * @return \PEIP\INF\Channel\Channel reply-channel |
||
270 | */ |
||
271 | protected function getReplyChannel($config) |
||
275 | |||
276 | /** |
||
277 | * Utility method to return a certainn channel from a config-obect. |
||
278 | * |
||
279 | * @param string the configuration type ofthe channel (e.g.: 'reply', 'request') |
||
280 | * @param object $config configuration object to return channel from. |
||
281 | * |
||
282 | * @return \PEIP\INF\Channel\Channel reply-channel |
||
283 | */ |
||
284 | public function doGetChannel($type, $config) |
||
292 | } |
||
293 |
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.