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 Payone_Settings_Service_XmlGenerate 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 Payone_Settings_Service_XmlGenerate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Payone_Settings_Service_XmlGenerate |
||
| 34 | { |
||
| 35 | const TAG_CONFIG_ROOT = 'config'; |
||
| 36 | const CLASS_PREFIX = 'Payone_Settings_Data_ConfigFile_'; |
||
| 37 | |||
| 38 | /** @var DOMDocument */ |
||
| 39 | private $dom; |
||
| 40 | |||
| 41 | // @todo neue Methode generate mit gleichen Parametern |
||
| 42 | // @todo wandelt anhand Shop "gruppiert" um und fügt mehrere Shops umwandlungen zusammen in ein config-root |
||
| 43 | // @todo innerhalb von mapShop wird dann ein mapSystem, mapGlobal, mapClearingtypes und mapProtect gerufen |
||
| 44 | // @todo innerhalb dieser methoden je nach bedarf weitere Methoden |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Generates an XML string from a Root config object, including all settings (global, shop, payment, protect) |
||
| 48 | * |
||
| 49 | * @api |
||
| 50 | * |
||
| 51 | * @param Payone_Settings_Data_ConfigFile_Root $config |
||
| 52 | * @return mixed @see SimpleXMLElement::asXml() |
||
| 53 | */ |
||
| 54 | public function generate(Payone_Settings_Data_ConfigFile_Root $config) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Generates an XML string from a Root config object, including all settings (global, shop, payment, protect) |
||
| 73 | * |
||
| 74 | * @api |
||
| 75 | * |
||
| 76 | * @param Payone_Settings_Data_ConfigFile_Root $config |
||
| 77 | * @return mixed @see SimpleXMLElement::asXml() |
||
| 78 | */ |
||
| 79 | public function execute(Payone_Settings_Data_ConfigFile_Root $config) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $name tag name |
||
| 90 | * @param array $array data |
||
| 91 | * @param null|SimpleXMLElement $root IF not set, $name will form the root element |
||
| 92 | * @return SimpleXMLElement |
||
| 93 | */ |
||
| 94 | public function simpleXmlFromNestedArray($name, $array, SimpleXMLElement $root = null) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param Payone_Settings_Data_ConfigFile_Shop $shopConfig |
||
| 126 | * @param DOMElement $configXml |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | protected function mapShop(Payone_Settings_Data_ConfigFile_Shop $shopConfig, DOMElement $configXml) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param Payone_Settings_Data_ConfigFile_Shop_System $systemConfig |
||
| 148 | * @param DOMElement $shopXml |
||
| 149 | * @return DOMElement |
||
| 150 | */ |
||
| 151 | protected function mapSystem(Payone_Settings_Data_ConfigFile_Shop_System $systemConfig, DOMElement $shopXml) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig |
||
| 164 | * @param DOMElement $shopXml |
||
| 165 | * @return DOMElement |
||
| 166 | */ |
||
| 167 | protected function mapGlobal(Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig, DOMElement $shopXml) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param Payone_Settings_Data_ConfigFile_Shop_ClearingTypes $clearingTypes |
||
| 183 | * @param DOMElement $shopXml |
||
| 184 | * @return DOMElement |
||
| 185 | */ |
||
| 186 | protected function mapClearingTypes(Payone_Settings_Data_ConfigFile_Shop_ClearingTypes $clearingTypes, DOMElement $shopXml) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param Payone_Settings_Data_ConfigFile_Shop_Protect $protectConfig |
||
| 210 | * @param DOMElement $shopXml |
||
| 211 | * @return DOMElement |
||
| 212 | */ |
||
| 213 | protected function mapProtect(Payone_Settings_Data_ConfigFile_Shop_Protect $protectConfig, DOMElement $shopXml) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param Payone_Settings_Data_ConfigFile_Shop_Misc $miscConfig |
||
| 225 | * @param DOMElement $shopXml |
||
| 226 | * @return DOMElement |
||
| 227 | */ |
||
| 228 | protected function mapMisc(Payone_Settings_Data_ConfigFile_Shop_Misc $miscConfig, DOMElement $shopXml) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param Payone_Settings_Data_ConfigFile_Protect_Consumerscore $consumerscoreConfig |
||
| 240 | * @param DOMElement $protectXml |
||
| 241 | * @return DOMElement |
||
| 242 | */ |
||
| 243 | View Code Duplication | protected function mapConsumerscore(Payone_Settings_Data_ConfigFile_Protect_Consumerscore $consumerscoreConfig, DOMElement $protectXml) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param Payone_Settings_Data_ConfigFile_Protect_Addresscheck $addresscheckConfig |
||
| 261 | * @param DOMElement $protectXml |
||
| 262 | * @return DOMElement |
||
| 263 | */ |
||
| 264 | View Code Duplication | protected function mapAddresscheck(Payone_Settings_Data_ConfigFile_Protect_Addresscheck $addresscheckConfig, DOMElement $protectXml) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig |
||
| 281 | * @param DOMElement $globalXml |
||
| 282 | * @return DOMElement |
||
| 283 | */ |
||
| 284 | protected function mapParameterInvoice(Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig, DOMElement $globalXml) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig |
||
| 299 | * @param DOMElement $globalXml |
||
| 300 | * @return DOMElement |
||
| 301 | */ |
||
| 302 | protected function mapPaymentCreditcard(Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig, DOMElement $globalXml) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param Payone_Settings_Data_ConfigFile_Shop_Misc $miscConfig |
||
| 314 | * @param DOMElement $miscXml |
||
| 315 | */ |
||
| 316 | public function addTransactionstatusForwarding(Payone_Settings_Data_ConfigFile_Shop_Misc $miscConfig, DOMElement $miscXml) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig |
||
| 332 | * @param DOMElement $globalXml |
||
| 333 | */ |
||
| 334 | public function addStatusMapping(Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig, DOMElement $globalXml) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param DOMElement $cleatringTypeNode |
||
| 352 | * @param Payone_Settings_Data_ConfigFile_PaymentMethod_Abstract $valueClearingType |
||
| 353 | */ |
||
| 354 | public function addTypesOrGlobalInfo(DOMElement $cleatringTypeNode, Payone_Settings_Data_ConfigFile_PaymentMethod_Abstract $valueClearingType) |
||
| 367 | |||
| 368 | public function addGlobal($parent, $type) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param DOMElement $cleatringTypeNode |
||
| 381 | * @param Payone_Settings_Data_ConfigFile_PaymentMethod_Abstract $valueClearingType |
||
| 382 | */ |
||
| 383 | public function addFeeConfig(DOMElement $cleatringTypeNode, Payone_Settings_Data_ConfigFile_PaymentMethod_Abstract $valueClearingType) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param DOMElement $parent |
||
| 402 | * @param $object |
||
| 403 | * @param $property |
||
| 404 | * @param bool $withCdata |
||
| 405 | * @return DOMElement |
||
| 406 | */ |
||
| 407 | protected function addChild(DOMElement $parent, $object, $property, $withCdata = false) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param DOMElement $mapNode |
||
| 429 | * @param $name |
||
| 430 | * @param $value |
||
| 431 | * @return DOMElement |
||
| 432 | */ |
||
| 433 | protected function addAttribute(DOMElement $mapNode, $name, $value) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param DOMElement|DOMDocument $parent |
||
| 451 | * @param $key |
||
| 452 | * @param $value |
||
| 453 | * @param bool $asCdata |
||
| 454 | * @return DOMElement |
||
| 455 | */ |
||
| 456 | protected function appendElement( $parent, $key, $value = null, $asCdata = false) |
||
| 477 | } |
||
| 478 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.