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 |
||
| 12 | class EndpointType |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * The binding for this endpoint. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | public $Binding; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The URI to this endpoint. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | public $Location; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The URI where responses can be delivered. |
||
| 30 | * |
||
| 31 | * @var string|null |
||
| 32 | */ |
||
| 33 | public $ResponseLocation = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Extra (namespace qualified) attributes. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $attributes = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Initialize an EndpointType. |
||
| 44 | * |
||
| 45 | * @param \DOMElement|null $xml The XML element we should load. |
||
| 46 | * @throws \Exception |
||
| 47 | */ |
||
| 48 | public function __construct(\DOMElement $xml = null) |
||
| 49 | { |
||
| 50 | if ($xml === null) { |
||
| 51 | return; |
||
| 52 | } |
||
| 53 | |||
| 54 | if (!$xml->hasAttribute('Binding')) { |
||
| 55 | throw new \Exception('Missing Binding on ' . $xml->tagName); |
||
| 56 | } |
||
| 57 | $this->Binding = $xml->getAttribute('Binding'); |
||
| 58 | |||
| 59 | if (!$xml->hasAttribute('Location')) { |
||
| 60 | throw new \Exception('Missing Location on ' . $xml->tagName); |
||
| 61 | } |
||
| 62 | $this->Location = $xml->getAttribute('Location'); |
||
| 63 | |||
| 64 | if ($xml->hasAttribute('ResponseLocation')) { |
||
| 65 | $this->ResponseLocation = $xml->getAttribute('ResponseLocation'); |
||
| 66 | } |
||
| 67 | |||
| 68 | foreach ($xml->attributes as $a) { |
||
| 69 | if ($a->namespaceURI === null) { |
||
| 70 | continue; /* Not namespace-qualified -- skip. */ |
||
| 71 | } |
||
| 72 | $fullName = '{' . $a->namespaceURI . '}' . $a->localName; |
||
| 73 | $this->attributes[$fullName] = array( |
||
| 74 | 'qualifiedName' => $a->nodeName, |
||
| 75 | 'namespaceURI' => $a->namespaceURI, |
||
| 76 | 'value' => $a->value, |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Check if a namespace-qualified attribute exists. |
||
| 83 | * |
||
| 84 | * @param string $namespaceURI The namespace URI. |
||
| 85 | * @param string $localName The local name. |
||
| 86 | * @return boolean true if the attribute exists, false if not. |
||
| 87 | */ |
||
| 88 | View Code Duplication | public function hasAttributeNS($namespaceURI, $localName) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Get a namespace-qualified attribute. |
||
| 100 | * |
||
| 101 | * @param string $namespaceURI The namespace URI. |
||
| 102 | * @param string $localName The local name. |
||
| 103 | * @return string The value of the attribute, or an empty string if the attribute does not exist. |
||
| 104 | */ |
||
| 105 | public function getAttributeNS($namespaceURI, $localName) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get a namespace-qualified attribute. |
||
| 120 | * |
||
| 121 | * @param string $namespaceURI The namespace URI. |
||
| 122 | * @param string $qualifiedName The local name. |
||
| 123 | * @param string $value The attribute value. |
||
| 124 | * @throws \Exception |
||
| 125 | */ |
||
| 126 | public function setAttributeNS($namespaceURI, $qualifiedName, $value) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Remove a namespace-qualified attribute. |
||
| 147 | * |
||
| 148 | * @param string $namespaceURI The namespace URI. |
||
| 149 | * @param string $localName The local name. |
||
| 150 | */ |
||
| 151 | View Code Duplication | public function removeAttributeNS($namespaceURI, $localName) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Add this endpoint to an XML element. |
||
| 162 | * |
||
| 163 | * @param \DOMElement $parent The element we should append this endpoint to. |
||
| 164 | * @param string $name The name of the element we should create. |
||
| 165 | * @return \DOMElement |
||
| 166 | */ |
||
| 167 | public function toXML(\DOMElement $parent, $name) |
||
| 190 | } |
||
| 191 |
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.