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:
Complex classes like XMLContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XMLContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class XMLContext |
||
|
|||
40 | implements |
||
41 | \PEIP\INF\Context\Context, |
||
42 | \PEIP\INF\Channel\ChannelResolver { |
||
43 | |||
44 | protected |
||
45 | $services = array(), |
||
46 | $configs = array(), |
||
47 | $gateways = array(), |
||
48 | $nodeBuilders = array(), |
||
49 | $channelRegistry, |
||
50 | $serviceProvider; |
||
51 | |||
52 | /** |
||
53 | * constructor |
||
54 | * |
||
55 | * @access public |
||
56 | * @param string $string the configuration string |
||
57 | * @return |
||
58 | */ |
||
59 | public function __construct($string){ |
||
71 | |||
72 | public function addConfig($config){ |
||
75 | |||
76 | public function handleReadConfig(\PEIP\INF\Event\Event $event){ |
||
79 | |||
80 | /** |
||
81 | * Creates and returns a XMLContext instance from a given config-string. |
||
82 | * |
||
83 | * @access public |
||
84 | * @param string $string the configuration string |
||
85 | * @return XMLContext the context instance |
||
86 | * @throws RuntimeException |
||
87 | */ |
||
88 | public static function createFromString($string){ |
||
91 | |||
92 | /** |
||
93 | * Creates and returns a XMLContext instance from a given config-file. |
||
94 | * |
||
95 | * @access public |
||
96 | * @param string $file the path to the configuration file |
||
97 | * @return XMLContext the context instance |
||
98 | * @throws RuntimeException |
||
99 | */ |
||
100 | public static function createFromFile($file){ |
||
107 | |||
108 | /** |
||
109 | * Initializes the context. |
||
110 | * |
||
111 | * @access protected |
||
112 | * @return void |
||
113 | */ |
||
114 | protected function init(){ |
||
126 | |||
127 | /** |
||
128 | * Registers a callable as builder for given node-name |
||
129 | * |
||
130 | * @implements \PEIP\INF\Context\Context |
||
131 | * @access public |
||
132 | * @param string $nodeName the name of the node |
||
133 | * @param callable $callable a callable which creates instances for node-name |
||
134 | */ |
||
135 | public function registerNodeBuilder($nodeName, $callable){ |
||
138 | |||
139 | /** |
||
140 | * Registers a context-plugin instance. |
||
141 | * |
||
142 | * @implements \PEIP\INF\Context\Context |
||
143 | * @access public |
||
144 | * @param \PEIP\INF\Context\Context_Plugin $plugin a plugin instance |
||
145 | */ |
||
146 | public function addPlugin(\PEIP\INF\Context\ContextPlugin $plugin){ |
||
149 | |||
150 | /** |
||
151 | * Creates a registers a context-plugin instance from a config object. |
||
152 | * |
||
153 | * @access public |
||
154 | * @param array $config configuration for the plugin |
||
155 | * @return |
||
156 | */ |
||
157 | public function createPlugin(array $config){ |
||
161 | |||
162 | /** |
||
163 | * Adds a context instance to the services stack. |
||
164 | * Note: Object instances registered with the included context will |
||
165 | * overwrite any instance with the same id on the including context. |
||
166 | * If you need a different behavior, please, make use |
||
167 | * of an include-tag in your configuration before your main |
||
168 | * configuration part. |
||
169 | * eg.: |
||
170 | * <config> |
||
171 | * <include file="path/to/include/context/config.xml"/> |
||
172 | * <!-- main configuration --> |
||
173 | * </config> |
||
174 | * |
||
175 | * @access public |
||
176 | * @param XMLContext $config the config to include |
||
177 | */ |
||
178 | public function includeContext(XMLContext $context){ |
||
181 | |||
182 | /** |
||
183 | * Creates and adds a context from file. |
||
184 | * |
||
185 | * @see XMLContext::includeContext |
||
186 | * @access public |
||
187 | * @param XMLContext $context the config to include |
||
188 | */ |
||
189 | public function includeContextFromFile($filePath){ |
||
194 | |||
195 | /** |
||
196 | * Creates and adds a context from string. |
||
197 | * |
||
198 | * @see XMLContext::includeContext |
||
199 | * @access public |
||
200 | * @param string $configString the config to include |
||
201 | */ |
||
202 | public function includeContextFromString($configString){ |
||
206 | |||
207 | /** |
||
208 | * Creates a context instance from a config object and includes it. |
||
209 | * |
||
210 | * @see XMLContext::includeContext |
||
211 | * @access protected |
||
212 | * @param object $config the configuration for the context |
||
213 | */ |
||
214 | protected function createContext($config){ |
||
219 | |||
220 | |||
221 | public function getServiceProvider(){ |
||
226 | |||
227 | /** |
||
228 | * Registers the build-methods for the main-components with this context. |
||
229 | * Note: This method and subsequent registered methods of this class are |
||
230 | * candidates for refactoring. Because this class has grown much to large |
||
231 | * and for better design and flexibility the core builder-methods should be |
||
232 | * put into a core context-plugin. |
||
233 | * |
||
234 | * @see XMLContext::includeContext |
||
235 | * @access protected |
||
236 | */ |
||
237 | protected function initNodeBuilders(){ |
||
259 | |||
260 | /** |
||
261 | * Builds a specific configuration-node. Calls the build-method which |
||
262 | * is registered with the node-name. If none is registered does nothing. |
||
263 | * |
||
264 | * @see XMLContext::doCreateChannel |
||
265 | * @access protected |
||
266 | * @param object $node configuration-node |
||
267 | * @return void |
||
268 | */ |
||
269 | protected function buildNode($node){ |
||
276 | |||
277 | /** |
||
278 | * Resolves a channel-name and returns channel-instace if found. |
||
279 | * Main purpose is to allow the context to act as a channel-resolver for |
||
280 | * mainly routers, hence implement the \PEIP\INF\Channel\ChannelResolver pattern. |
||
281 | * Note: Channels are registerd globally in a registry per thread/process. |
||
282 | * This allows to connect many context instances through channels without |
||
283 | * coupling them by in a include in configuration. |
||
284 | * |
||
285 | * @see \PEIP\INF\Channel\ChannelResolver |
||
286 | * @implements \PEIP\INF\Channel\ChannelResolver |
||
287 | * @access public |
||
288 | * @param string $channelName the name/id of the channel to return |
||
289 | * @return \PEIP\INF\Channel\Channel |
||
290 | */ |
||
291 | public function resolveChannelName($channelName){ |
||
294 | |||
295 | /** |
||
296 | * returns a service for a given id |
||
297 | * |
||
298 | * @implements \PEIP\INF\Context\Context |
||
299 | * @access public |
||
300 | * @param mixed $id the id for the service |
||
301 | * @return object the service instance if found |
||
302 | */ |
||
303 | public function getService($id){ |
||
306 | |||
307 | /** |
||
308 | * returns all registered services |
||
309 | * |
||
310 | * @access public |
||
311 | * @return array registered services |
||
312 | */ |
||
313 | public function getServices(){ |
||
316 | |||
317 | /** |
||
318 | * Checks wether a service with a given id is registered |
||
319 | * |
||
320 | * @access public |
||
321 | * @param mixed $id the id for the service |
||
322 | * @return boolean wether service is registered |
||
323 | */ |
||
324 | public function hasService($id){ |
||
327 | |||
328 | /** |
||
329 | * Tries to receive a service for a given id. |
||
330 | * Throws RuntimeException if no service is found |
||
331 | * |
||
332 | * @access protected |
||
333 | * @throws RuntimeException |
||
334 | * @param mixed $id the id for the service |
||
335 | * @return object the service instance if found |
||
336 | * |
||
337 | */ |
||
338 | protected function requestService($id){ |
||
345 | |||
346 | /** |
||
347 | * Creates and initializes service instance from a given configuration. |
||
348 | * Registers instance if id is set in config. |
||
349 | * |
||
350 | * @access protected |
||
351 | * @param object $config |
||
352 | * @return object the initialized service instance |
||
353 | */ |
||
354 | protected function initService($config){ |
||
360 | |||
361 | /** |
||
362 | * Creates and initializes service instance from a given configuration. |
||
363 | * |
||
364 | * @access public |
||
365 | * @param $config |
||
366 | * @return object the initialized service instance |
||
367 | */ |
||
368 | public function createService(array $config){ |
||
371 | |||
372 | |||
373 | /** |
||
374 | * returns gateway instance forgiven id. |
||
375 | * the use of this method is deprecated. Use getService instead. |
||
376 | * |
||
377 | * @deprecated |
||
378 | * @access public |
||
379 | * @param mixed $id the id ofthe gateway |
||
380 | * @return object the gateway instance |
||
381 | */ |
||
382 | public function getGateway($id){ |
||
385 | |||
386 | /** |
||
387 | * Creates a pollable channel from a configuration object. |
||
388 | * |
||
389 | * @see XMLContext::doCreateChannel |
||
390 | * @access public |
||
391 | * @param object $config configuration object for the pollable channel. |
||
392 | * @return \PEIP\INF\Channel\Channel the created pollable channel instance |
||
393 | */ |
||
394 | public function createChannel($config){ |
||
397 | |||
398 | /** |
||
399 | * Creates a subscribable channel from a configuration object. |
||
400 | * |
||
401 | * @see XMLContext::doCreateChannel |
||
402 | * @access public |
||
403 | * @param object $config configuration object for the subscribable channel. |
||
404 | * @return \PEIP\INF\Channel\Channel the created subscribable channel instance |
||
405 | */ |
||
406 | public function createSubscribableChannel($config){ |
||
409 | |||
410 | /** |
||
411 | * Creates and registers arbitrary channel from a configuration object and additional information. |
||
412 | * |
||
413 | * @access public |
||
414 | * @param object $config configuration object for the channel. |
||
415 | * @param string $defaultChannelClass the channel class to use if none is set in config |
||
416 | * @param $additionalArguments additional arguments for the channel constructor (without first arg = id) |
||
417 | * @return \PEIP\INF\Channel\Channel the created channel instance |
||
418 | */ |
||
419 | public function doCreateChannel($config, $defaultChannelClass, array $additionalArguments = array()){ |
||
428 | |||
429 | /** |
||
430 | * Creates and registers gateway from a configuration object. |
||
431 | * |
||
432 | * @see XMLContext::initNodeBuilders |
||
433 | * @access public |
||
434 | * @param object $config configuration object for the gateway. |
||
435 | * @param string $defaultClass the class to use if none is set in config. |
||
436 | * @return object the gateway instance |
||
437 | */ |
||
438 | View Code Duplication | public function createGateway($config, $defaultClass = false){ |
|
449 | |||
450 | /** |
||
451 | * Creates and registers router from a configuration object. |
||
452 | * Adds this context instance as channel-resolver to the router if |
||
453 | * none is set in config. |
||
454 | * |
||
455 | * @see XMLContext::resolveChannelName |
||
456 | * @see XMLContext::initNodeBuilders |
||
457 | * @access public |
||
458 | * @param object $config configuration object for the gateway. |
||
459 | * @param string $defaultClass the class to use if none is set in config. |
||
460 | * @return object the router instance |
||
461 | */ |
||
462 | View Code Duplication | public function createRouter($config, $defaultClass = false){ |
|
469 | |||
470 | /** |
||
471 | * Creates and registers splitter from a configuration object. |
||
472 | * |
||
473 | * @see XMLContext::initNodeBuilders |
||
474 | * @see XMLContext::createReplyMessageHandler |
||
475 | * @access public |
||
476 | * @param object $config configuration object for the splitter. |
||
477 | * @return object the splitter instance |
||
478 | */ |
||
479 | public function createSplitter($config){ |
||
482 | |||
483 | /** |
||
484 | * Creates and registers transformer from a configuration object. |
||
485 | * |
||
486 | * @see XMLContext::initNodeBuilders |
||
487 | * @see XMLContext::createReplyMessageHandler |
||
488 | * @access public |
||
489 | * @param object $config configuration object for the transformer. |
||
490 | * @return object the transformer instance |
||
491 | */ |
||
492 | public function createTransformer($config){ |
||
495 | |||
496 | /** |
||
497 | * Creates aggregator from a configuration object. |
||
498 | * |
||
499 | * @see XMLContext::initNodeBuilders |
||
500 | * @see XMLContext::createReplyMessageHandler |
||
501 | * @access public |
||
502 | * @param object $config configuration object for the aggregator. |
||
503 | * @return object the aggregator instance |
||
504 | */ |
||
505 | public function createAggregator($config){ |
||
508 | |||
509 | /** |
||
510 | * Creates wiretap from a configuration object. |
||
511 | * |
||
512 | * @see XMLContext::initNodeBuilders |
||
513 | * @see XMLContext::createReplyMessageHandler |
||
514 | * @access public |
||
515 | * @param object $config configuration object for the wiretap. |
||
516 | * @return object the wiretap instance |
||
517 | */ |
||
518 | public function createWiretap($config){ |
||
521 | |||
522 | /** |
||
523 | * Creates a reply-message-handler from a configuration object. |
||
524 | * |
||
525 | * @see XMLContext::initNodeBuilders |
||
526 | * @access public |
||
527 | * @param object $config configuration object for the reply-message-handler. |
||
528 | * @param string $defaultClass the class to use if none is set in config. |
||
529 | * @return object the reply-message-handler instance |
||
530 | */ |
||
531 | public function createReplyMessageHandler($config, $defaultClass = false){ |
||
534 | |||
535 | /** |
||
536 | * Creates and registers service-activator from a configuration object. |
||
537 | * |
||
538 | * @see XMLContext::initNodeBuilders |
||
539 | * @access public |
||
540 | * @param object $config configuration object for the service-activator. |
||
541 | * @param string $defaultClass the class to use if none is set in config. |
||
542 | * @return object the service-activator instance |
||
543 | */ |
||
544 | View Code Duplication | public function createServiceActivator($config, $defaultClass = false){ |
|
557 | |||
558 | |||
559 | /** |
||
560 | * Provides a service for a configuration object. |
||
561 | * Returns reference to a service if configured, otherwise |
||
562 | * creates new service instance. |
||
563 | * |
||
564 | * @see XMLContext::getService |
||
565 | * @see XMLContext::createService |
||
566 | * @access protected |
||
567 | * @param object $config configuration object for the service. |
||
568 | * @return |
||
569 | */ |
||
570 | protected function provideService($config){ |
||
579 | |||
580 | /** |
||
581 | * Utility method to return a (camel-cased) setter method-name for a property of a config-obect. |
||
582 | * Returns setter-name if set in configuration, otherwise if name of property is set, returns |
||
583 | * camel-cased setter-name build from property-name. |
||
584 | * |
||
585 | * @see XMLContext::getService |
||
586 | * @see XMLContext::createService |
||
587 | * @access protected |
||
588 | * @param object $config configuration object for the setter-method. |
||
589 | * @return string camel-cased |
||
590 | */ |
||
591 | protected static function getSetter($config){ |
||
599 | |||
600 | /** |
||
601 | * Builds single argument (to call a method with later) from a config-obect. |
||
602 | * |
||
603 | * @access protected |
||
604 | * @param object $config configuration object to create argument from. |
||
605 | * @return mixed build argument |
||
606 | */ |
||
607 | View Code Duplication | protected function buildArg($config){ |
|
634 | |||
635 | |||
636 | /** |
||
637 | * Utility method to create arguments for a reply-handler constructor from a config-obect. |
||
638 | * |
||
639 | * @access protected |
||
640 | * @param object $config configuration object to create arguments from. |
||
641 | * @return mixed build arguments |
||
642 | */ |
||
643 | View Code Duplication | protected function getReplyHandlerArguments($config){ |
|
653 | |||
654 | |||
655 | /** |
||
656 | * Utility method to return a request-channel from a config-obect. |
||
657 | * |
||
658 | * @see XMLContext::doGetChannel |
||
659 | * @access protected |
||
660 | * @param object $config configuration object to return request-channel from. |
||
661 | * @return \PEIP\INF\Channel\Channel request-channel |
||
662 | */ |
||
663 | protected function getRequestChannel($config){ |
||
666 | |||
667 | |||
668 | /** |
||
669 | * Utility method to return a reply-channel from a config-obect. |
||
670 | * |
||
671 | * @see XMLContext::doGetChannel |
||
672 | * @access protected |
||
673 | * @param object $config configuration object to return reply-channel from. |
||
674 | * @return \PEIP\INF\Channel\Channel reply-channel |
||
675 | */ |
||
676 | protected function getReplyChannel($config){ |
||
679 | |||
680 | |||
681 | /** |
||
682 | * Utility method to return a certainn channel from a config-obect. |
||
683 | * |
||
684 | * @access protected |
||
685 | * @param string the configuration type ofthe channel (e.g.: 'reply', 'request') |
||
686 | * @param object $config configuration object to return channel from. |
||
687 | * @return \PEIP\INF\Channel\Channel reply-channel |
||
688 | */ |
||
689 | View Code Duplication | public function doGetChannel($type, $config){ |
|
701 | |||
702 | |||
703 | |||
704 | /** |
||
705 | * Builds an arbitrary service/object instance from a config-obect. |
||
706 | * |
||
707 | * @static |
||
708 | * @access protected |
||
709 | * @param object $config configuration object to build a service instance from. |
||
710 | * @param array $arguments arguments for the service constructor |
||
711 | * @param string $defaultClass class to create instance for if none is set in config |
||
712 | * @return object build and modified srvice instance |
||
713 | */ |
||
714 | protected static function doBuild($config, $arguments, $defaultClass = false){ |
||
733 | |||
734 | /** |
||
735 | * Utility function to build an object instance for given class with given constructor-arguments. |
||
736 | * |
||
737 | * @see GenericBuilder |
||
738 | * @static |
||
739 | * @access protected |
||
740 | * @param object $className name of class to build instance for. |
||
741 | * @param array $arguments arguments for the constructor |
||
742 | * @return object build and modified srvice instance |
||
743 | */ |
||
744 | protected static function build($className, $arguments){ |
||
747 | |||
748 | protected static function hasPublicProperty($service, $type, $name){ |
||
755 | |||
756 | } |
||
757 |