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 |
||
| 20 | class AttributeQuery extends SubjectQuery |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * The attributes, as an associative array. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $attributes; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The NameFormat used on all attributes. |
||
| 31 | * |
||
| 32 | * If more than one NameFormat is used, this will contain |
||
| 33 | * the unspecified nameformat. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $nameFormat; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor for SAML 2 attribute query messages. |
||
| 41 | * |
||
| 42 | * @param \DOMElement|null $xml The input message. |
||
| 43 | * @throws \Exception |
||
| 44 | */ |
||
| 45 | public function __construct(\DOMElement $xml = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Retrieve all requested attributes. |
||
| 92 | * |
||
| 93 | * @return array All requested attributes, as an associative array. |
||
| 94 | */ |
||
| 95 | public function getAttributes() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Set all requested attributes. |
||
| 102 | * |
||
| 103 | * @param array $attributes All requested attributes, as an associative array. |
||
| 104 | */ |
||
| 105 | public function setAttributes(array $attributes) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Retrieve the NameFormat used on all attributes. |
||
| 112 | * |
||
| 113 | * If more than one NameFormat is used in the received attributes, this |
||
| 114 | * returns the unspecified NameFormat. |
||
| 115 | * |
||
| 116 | * @return string The NameFormat used on all attributes. |
||
| 117 | */ |
||
| 118 | public function getAttributeNameFormat() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Set the NameFormat used on all attributes. |
||
| 125 | * |
||
| 126 | * @param string $nameFormat The NameFormat used on all attributes. |
||
| 127 | */ |
||
| 128 | public function setAttributeNameFormat($nameFormat) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Convert the attribute query message to an XML element. |
||
| 137 | * |
||
| 138 | * @return \DOMElement This attribute query. |
||
| 139 | */ |
||
| 140 | public function toUnsignedXML() |
||
| 171 | } |
||
| 172 |
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.