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 |
||
| 33 | class XMLContext |
||
| 34 | implements |
||
| 35 | \PEIP\INF\Context\Context, |
||
| 36 | \PEIP\INF\Channel\ChannelResolver {
|
||
| 37 | |||
| 38 | protected |
||
| 39 | $services = array(), |
||
| 40 | $configs = array(), |
||
| 41 | $gateways = array(), |
||
| 42 | $nodeBuilders = array(), |
||
| 43 | $channelRegistry, |
||
| 44 | $serviceProvider; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * constructor |
||
| 48 | * |
||
| 49 | * @access public |
||
| 50 | * @param string $string the configuration string |
||
| 51 | * @return |
||
| 52 | */ |
||
| 53 | public function __construct($string){
|
||
| 65 | |||
| 66 | public function addConfig($config){
|
||
| 69 | |||
| 70 | public function handleReadConfig(\PEIP\INF\Event\Event $event){
|
||
| 73 | |||
| 74 | /** |
||
| 75 | * Creates and returns a XMLContext instance from a given config-string. |
||
| 76 | * |
||
| 77 | * @access public |
||
| 78 | * @param string $string the configuration string |
||
| 79 | * @return XMLContext the context instance |
||
| 80 | * @throws RuntimeException |
||
| 81 | */ |
||
| 82 | public static function createFromString($string){
|
||
| 85 | |||
| 86 | /** |
||
| 87 | * Creates and returns a XMLContext instance from a given config-file. |
||
| 88 | * |
||
| 89 | * @access public |
||
| 90 | * @param string $file the path to the configuration file |
||
| 91 | * @return XMLContext the context instance |
||
| 92 | * @throws RuntimeException |
||
| 93 | */ |
||
| 94 | public static function createFromFile($file){
|
||
| 101 | |||
| 102 | /** |
||
| 103 | * Initializes the context. |
||
| 104 | * |
||
| 105 | * @access protected |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | protected function init(){
|
||
| 120 | |||
| 121 | /** |
||
| 122 | * Registers a callable as builder for given node-name |
||
| 123 | * |
||
| 124 | * @implements \PEIP\INF\Context\Context |
||
| 125 | * @access public |
||
| 126 | * @param string $nodeName the name of the node |
||
| 127 | * @param callable $callable a callable which creates instances for node-name |
||
| 128 | */ |
||
| 129 | public function registerNodeBuilder($nodeName, $callable){
|
||
| 132 | |||
| 133 | /** |
||
| 134 | * Registers a context-plugin instance. |
||
| 135 | * |
||
| 136 | * @implements \PEIP\INF\Context\Context |
||
| 137 | * @access public |
||
| 138 | * @param \PEIP\INF\Context\Context_Plugin $plugin a plugin instance |
||
| 139 | */ |
||
| 140 | public function addPlugin(\PEIP\INF\Context\ContextPlugin $plugin){
|
||
| 143 | |||
| 144 | /** |
||
| 145 | * Creates a registers a context-plugin instance from a config object. |
||
| 146 | * |
||
| 147 | * @access public |
||
| 148 | * @param object $config configuration object for the plugin |
||
| 149 | * @return |
||
| 150 | */ |
||
| 151 | public function createPlugin($config){
|
||
| 155 | |||
| 156 | /** |
||
| 157 | * Adds a context instance to the services stack. |
||
| 158 | * Note: Object instances registered with the included context will |
||
| 159 | * overwrite any instance with the same id on the including context. |
||
| 160 | * If you need a different behavior, please, make use |
||
| 161 | * of an include-tag in your configuration before your main |
||
| 162 | * configuration part. |
||
| 163 | * eg.: |
||
| 164 | * <config> |
||
| 165 | * <include file="path/to/include/context/config.xml"/> |
||
| 166 | * <!-- main configuration --> |
||
| 167 | * </config> |
||
| 168 | * |
||
| 169 | * @access public |
||
| 170 | * @param XMLContext $config the config to include |
||
| 171 | */ |
||
| 172 | public function includeContext(XMLContext $context){
|
||
| 175 | |||
| 176 | /** |
||
| 177 | * Creates and adds a context from file. |
||
| 178 | * |
||
| 179 | * @see XMLContext::includeContext |
||
| 180 | * @access public |
||
| 181 | * @param XMLContext $context the config to include |
||
| 182 | */ |
||
| 183 | public function includeContextFromFile($filePath){
|
||
| 188 | |||
| 189 | /** |
||
| 190 | * Creates and adds a context from string. |
||
| 191 | * |
||
| 192 | * @see XMLContext::includeContext |
||
| 193 | * @access public |
||
| 194 | * @param string $configString the config to include |
||
| 195 | */ |
||
| 196 | public function includeContextFromString($configString){
|
||
| 200 | |||
| 201 | /** |
||
| 202 | * Creates a context instance from a config object and includes it. |
||
| 203 | * |
||
| 204 | * @see XMLContext::includeContext |
||
| 205 | * @access protected |
||
| 206 | * @param object $config the configuration for the context |
||
| 207 | */ |
||
| 208 | protected function createContext($config){
|
||
| 213 | |||
| 214 | |||
| 215 | public function getServiceProvider(){
|
||
| 220 | |||
| 221 | /** |
||
| 222 | * Registers the build-methods for the main-components with this context. |
||
| 223 | * Note: This method and subsequent registered methods of this class are |
||
| 224 | * candidates for refactoring. Because this class has grown much to large |
||
| 225 | * and for better design and flexibility the core builder-methods should be |
||
| 226 | * put into a core context-plugin. |
||
| 227 | * |
||
| 228 | * @see XMLContext::includeContext |
||
| 229 | * @access protected |
||
| 230 | */ |
||
| 231 | protected function initNodeBuilders(){
|
||
| 253 | |||
| 254 | /** |
||
| 255 | * Builds a specific configuration-node. Calls the build-method which |
||
| 256 | * is registered with the node-name. If none is registered does nothing. |
||
| 257 | * |
||
| 258 | * @see XMLContext::doCreateChannel |
||
| 259 | * @access protected |
||
| 260 | * @param object $node configuration-node |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | protected function buildNode($node){
|
||
| 270 | |||
| 271 | /** |
||
| 272 | * Resolves a channel-name and returns channel-instace if found. |
||
| 273 | * Main purpose is to allow the context to act as a channel-resolver for |
||
| 274 | * mainly routers, hence implement the \PEIP\INF\Channel\ChannelResolver pattern. |
||
| 275 | * Note: Channels are registerd globally in a registry per thread/process. |
||
| 276 | * This allows to connect many context instances through channels without |
||
| 277 | * coupling them by in a include in configuration. |
||
| 278 | * |
||
| 279 | * @see \PEIP\INF\Channel\ChannelResolver |
||
| 280 | * @implements \PEIP\INF\Channel\ChannelResolver |
||
| 281 | * @access public |
||
| 282 | * @param string $channelName the name/id of the channel to return |
||
| 283 | * @return \PEIP\INF\Channel\Channel |
||
| 284 | */ |
||
| 285 | public function resolveChannelName($channelName){
|
||
| 288 | |||
| 289 | /** |
||
| 290 | * returns a service for a given id |
||
| 291 | * |
||
| 292 | * @implements \PEIP\INF\Context\Context |
||
| 293 | * @access public |
||
| 294 | * @param mixed $id the id for the service |
||
| 295 | * @return object the service instance if found |
||
| 296 | */ |
||
| 297 | public function getService($id){
|
||
| 300 | |||
| 301 | /** |
||
| 302 | * returns all registered services |
||
| 303 | * |
||
| 304 | * @access public |
||
| 305 | * @return array registered services |
||
| 306 | */ |
||
| 307 | public function getServices(){
|
||
| 310 | |||
| 311 | /** |
||
| 312 | * Checks wether a service with a given id is registered |
||
| 313 | * |
||
| 314 | * @access public |
||
| 315 | * @param mixed $id the id for the service |
||
| 316 | * @return boolean wether service is registered |
||
| 317 | */ |
||
| 318 | public function hasService($id){
|
||
| 321 | |||
| 322 | /** |
||
| 323 | * Tries to receive a service for a given id. |
||
| 324 | * Throws RuntimeException if no service is found |
||
| 325 | * |
||
| 326 | * @access protected |
||
| 327 | * @throws RuntimeException |
||
| 328 | * @param mixed $id the id for the service |
||
| 329 | * @return object the service instance if found |
||
| 330 | * |
||
| 331 | */ |
||
| 332 | protected function requestService($id){
|
||
| 339 | |||
| 340 | /** |
||
| 341 | * Creates and initializes service instance from a given configuration. |
||
| 342 | * Registers instance if id is set in config. |
||
| 343 | * |
||
| 344 | * @access protected |
||
| 345 | * @param object $config |
||
| 346 | * @return object the initialized service instance |
||
| 347 | */ |
||
| 348 | protected function initService($config){
|
||
| 354 | |||
| 355 | /** |
||
| 356 | * Creates and initializes service instance from a given configuration. |
||
| 357 | * |
||
| 358 | * @access public |
||
| 359 | * @param $config |
||
| 360 | * @return object the initialized service instance |
||
| 361 | */ |
||
| 362 | public function createService($config){
|
||
| 365 | |||
| 366 | /** |
||
| 367 | * Modifies a service instance from configuration. |
||
| 368 | * - Sets properties on the instance. |
||
| 369 | * -- Calls a public setter method if exists. |
||
| 370 | * -- Else sets a public property if exists. |
||
| 371 | * - Calls methods on the instance. |
||
| 372 | * - Registers listeners to events on the instance |
||
| 373 | * |
||
| 374 | * @access protected |
||
| 375 | * @param object $service the service instance to modify |
||
| 376 | * @param object $config configuration to get the modification instructions from. |
||
| 377 | * @return object the modificated service |
||
| 378 | */ |
||
| 379 | protected function modifyService($service, $config){
|
||
| 420 | |||
| 421 | /** |
||
| 422 | * returns gateway instance forgiven id. |
||
| 423 | * the use of this method is deprecated. Use getService instead. |
||
| 424 | * |
||
| 425 | * @deprecated |
||
| 426 | * @access public |
||
| 427 | * @param mixed $id the id ofthe gateway |
||
| 428 | * @return object the gateway instance |
||
| 429 | */ |
||
| 430 | public function getGateway($id){
|
||
| 433 | |||
| 434 | /** |
||
| 435 | * Creates a pollable channel from a configuration object. |
||
| 436 | * |
||
| 437 | * @see XMLContext::doCreateChannel |
||
| 438 | * @access public |
||
| 439 | * @param object $config configuration object for the pollable channel. |
||
| 440 | * @return \PEIP\INF\Channel\Channel the created pollable channel instance |
||
| 441 | */ |
||
| 442 | public function createChannel($config){
|
||
| 445 | |||
| 446 | /** |
||
| 447 | * Creates a subscribable channel from a configuration object. |
||
| 448 | * |
||
| 449 | * @see XMLContext::doCreateChannel |
||
| 450 | * @access public |
||
| 451 | * @param object $config configuration object for the subscribable channel. |
||
| 452 | * @return \PEIP\INF\Channel\Channel the created subscribable channel instance |
||
| 453 | */ |
||
| 454 | public function createSubscribableChannel($config){
|
||
| 457 | |||
| 458 | /** |
||
| 459 | * Creates and registers arbitrary channel from a configuration object and additional information. |
||
| 460 | * |
||
| 461 | * @access public |
||
| 462 | * @param object $config configuration object for the channel. |
||
| 463 | * @param string $defaultChannelClass the channel class to use if none is set in config |
||
| 464 | * @param $additionalArguments additional arguments for the channel constructor (without first arg = id) |
||
| 465 | * @return \PEIP\INF\Channel\Channel the created channel instance |
||
| 466 | */ |
||
| 467 | View Code Duplication | public function doCreateChannel($config, $defaultChannelClass, array $additionalArguments = array()){
|
|
| 476 | |||
| 477 | /** |
||
| 478 | * Creates and registers gateway from a configuration object. |
||
| 479 | * |
||
| 480 | * @see XMLContext::initNodeBuilders |
||
| 481 | * @access public |
||
| 482 | * @param object $config configuration object for the gateway. |
||
| 483 | * @param string $defaultClass the class to use if none is set in config. |
||
| 484 | * @return object the gateway instance |
||
| 485 | */ |
||
| 486 | View Code Duplication | public function createGateway($config, $defaultClass = false){
|
|
| 497 | |||
| 498 | /** |
||
| 499 | * Creates and registers router from a configuration object. |
||
| 500 | * Adds this context instance as channel-resolver to the router if |
||
| 501 | * none is set in config. |
||
| 502 | * |
||
| 503 | * @see XMLContext::resolveChannelName |
||
| 504 | * @see XMLContext::initNodeBuilders |
||
| 505 | * @access public |
||
| 506 | * @param object $config configuration object for the gateway. |
||
| 507 | * @param string $defaultClass the class to use if none is set in config. |
||
| 508 | * @return object the router instance |
||
| 509 | */ |
||
| 510 | View Code Duplication | public function createRouter($config, $defaultClass = false){
|
|
| 517 | |||
| 518 | /** |
||
| 519 | * Creates and registers splitter from a configuration object. |
||
| 520 | * |
||
| 521 | * @see XMLContext::initNodeBuilders |
||
| 522 | * @see XMLContext::createReplyMessageHandler |
||
| 523 | * @access public |
||
| 524 | * @param object $config configuration object for the splitter. |
||
| 525 | * @return object the splitter instance |
||
| 526 | */ |
||
| 527 | public function createSplitter($config){
|
||
| 530 | |||
| 531 | /** |
||
| 532 | * Creates and registers transformer from a configuration object. |
||
| 533 | * |
||
| 534 | * @see XMLContext::initNodeBuilders |
||
| 535 | * @see XMLContext::createReplyMessageHandler |
||
| 536 | * @access public |
||
| 537 | * @param object $config configuration object for the transformer. |
||
| 538 | * @return object the transformer instance |
||
| 539 | */ |
||
| 540 | public function createTransformer($config){
|
||
| 543 | |||
| 544 | /** |
||
| 545 | * Creates aggregator from a configuration object. |
||
| 546 | * |
||
| 547 | * @see XMLContext::initNodeBuilders |
||
| 548 | * @see XMLContext::createReplyMessageHandler |
||
| 549 | * @access public |
||
| 550 | * @param object $config configuration object for the aggregator. |
||
| 551 | * @return object the aggregator instance |
||
| 552 | */ |
||
| 553 | public function createAggregator($config){
|
||
| 556 | |||
| 557 | /** |
||
| 558 | * Creates wiretap from a configuration object. |
||
| 559 | * |
||
| 560 | * @see XMLContext::initNodeBuilders |
||
| 561 | * @see XMLContext::createReplyMessageHandler |
||
| 562 | * @access public |
||
| 563 | * @param object $config configuration object for the wiretap. |
||
| 564 | * @return object the wiretap instance |
||
| 565 | */ |
||
| 566 | public function createWiretap($config){
|
||
| 569 | |||
| 570 | /** |
||
| 571 | * Creates a reply-message-handler from a configuration object. |
||
| 572 | * |
||
| 573 | * @see XMLContext::initNodeBuilders |
||
| 574 | * @access public |
||
| 575 | * @param object $config configuration object for the reply-message-handler. |
||
| 576 | * @param string $defaultClass the class to use if none is set in config. |
||
| 577 | * @return object the reply-message-handler instance |
||
| 578 | */ |
||
| 579 | public function createReplyMessageHandler($config, $defaultClass = false){
|
||
| 582 | |||
| 583 | /** |
||
| 584 | * Creates and registers service-activator from a configuration object. |
||
| 585 | * |
||
| 586 | * @see XMLContext::initNodeBuilders |
||
| 587 | * @access public |
||
| 588 | * @param object $config configuration object for the service-activator. |
||
| 589 | * @param string $defaultClass the class to use if none is set in config. |
||
| 590 | * @return object the service-activator instance |
||
| 591 | */ |
||
| 592 | View Code Duplication | public function createServiceActivator($config, $defaultClass = false){
|
|
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * Provides a service for a configuration object. |
||
| 609 | * Returns reference to a service if configured, otherwise |
||
| 610 | * creates new service instance. |
||
| 611 | * |
||
| 612 | * @see XMLContext::getService |
||
| 613 | * @see XMLContext::createService |
||
| 614 | * @access protected |
||
| 615 | * @param object $config configuration object for the service. |
||
| 616 | * @return |
||
| 617 | */ |
||
| 618 | protected function provideService($config){
|
||
| 627 | |||
| 628 | /** |
||
| 629 | * Utility method to return a (camel-cased) setter method-name for a property of a config-obect. |
||
| 630 | * Returns setter-name if set in configuration, otherwise if name of property is set, returns |
||
| 631 | * camel-cased setter-name build from property-name. |
||
| 632 | * |
||
| 633 | * @see XMLContext::getService |
||
| 634 | * @see XMLContext::createService |
||
| 635 | * @access protected |
||
| 636 | * @param object $config configuration object for the setter-method. |
||
| 637 | * @return string camel-cased |
||
| 638 | */ |
||
| 639 | protected static function getSetter($config){
|
||
| 647 | |||
| 648 | /** |
||
| 649 | * Builds single argument (to call a method with later) from a config-obect. |
||
| 650 | * |
||
| 651 | * @access protected |
||
| 652 | * @param object $config configuration object to create argument from. |
||
| 653 | * @return mixed build argument |
||
| 654 | */ |
||
| 655 | View Code Duplication | protected function buildArg($config){
|
|
| 682 | |||
| 683 | |||
| 684 | /** |
||
| 685 | * Utility method to create arguments for a reply-handler constructor from a config-obect. |
||
| 686 | * |
||
| 687 | * @access protected |
||
| 688 | * @param object $config configuration object to create arguments from. |
||
| 689 | * @return mixed build arguments |
||
| 690 | */ |
||
| 691 | View Code Duplication | protected function getReplyHandlerArguments($config){
|
|
| 701 | |||
| 702 | |||
| 703 | /** |
||
| 704 | * Utility method to return a request-channel from a config-obect. |
||
| 705 | * |
||
| 706 | * @see XMLContext::doGetChannel |
||
| 707 | * @access protected |
||
| 708 | * @param object $config configuration object to return request-channel from. |
||
| 709 | * @return \PEIP\INF\Channel\Channel request-channel |
||
| 710 | */ |
||
| 711 | protected function getRequestChannel($config){
|
||
| 714 | |||
| 715 | |||
| 716 | /** |
||
| 717 | * Utility method to return a reply-channel from a config-obect. |
||
| 718 | * |
||
| 719 | * @see XMLContext::doGetChannel |
||
| 720 | * @access protected |
||
| 721 | * @param object $config configuration object to return reply-channel from. |
||
| 722 | * @return \PEIP\INF\Channel\Channel reply-channel |
||
| 723 | */ |
||
| 724 | protected function getReplyChannel($config){
|
||
| 727 | |||
| 728 | |||
| 729 | /** |
||
| 730 | * Utility method to return a certainn channel from a config-obect. |
||
| 731 | * |
||
| 732 | * @access protected |
||
| 733 | * @param string the configuration type ofthe channel (e.g.: 'reply', 'request') |
||
| 734 | * @param object $config configuration object to return channel from. |
||
| 735 | * @return \PEIP\INF\Channel\Channel reply-channel |
||
| 736 | */ |
||
| 737 | View Code Duplication | public function doGetChannel($type, $config){
|
|
| 749 | |||
| 750 | |||
| 751 | /** |
||
| 752 | * Builds and modifies an arbitrary service/object instance from a config-obect. |
||
| 753 | * |
||
| 754 | * @see XMLContext::doBuild |
||
| 755 | * @see XMLContext::modifyService |
||
| 756 | * @implements \PEIP\INF\Context\Context |
||
| 757 | * @access public |
||
| 758 | * @param object $config configuration object to build a service instance from. |
||
| 759 | * @param array $arguments arguments for the service constructor |
||
| 760 | * @param string $defaultClass class to create instance for if none is set in config |
||
| 761 | * @return object build and modified srvice instance |
||
| 762 | */ |
||
| 763 | public function buildAndModify($config, $arguments, $defaultClass = false){
|
||
| 766 | |||
| 767 | /** |
||
| 768 | * Builds an arbitrary service/object instance from a config-obect. |
||
| 769 | * |
||
| 770 | * @static |
||
| 771 | * @access protected |
||
| 772 | * @param object $config configuration object to build a service instance from. |
||
| 773 | * @param array $arguments arguments for the service constructor |
||
| 774 | * @param string $defaultClass class to create instance for if none is set in config |
||
| 775 | * @return object build and modified srvice instance |
||
| 776 | */ |
||
| 777 | protected static function doBuild($config, $arguments, $defaultClass = false){
|
||
| 796 | |||
| 797 | /** |
||
| 798 | * Utility function to build an object instance for given class with given constructor-arguments. |
||
| 799 | * |
||
| 800 | * @see GenericBuilder |
||
| 801 | * @static |
||
| 802 | * @access protected |
||
| 803 | * @param object $className name of class to build instance for. |
||
| 804 | * @param array $arguments arguments for the constructor |
||
| 805 | * @return object build and modified srvice instance |
||
| 806 | */ |
||
| 807 | protected static function build($className, $arguments){
|
||
| 810 | |||
| 811 | protected static function hasPublicProperty($service, $type, $name){
|
||
| 818 | |||
| 819 | } |
||
| 820 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: