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 |
||
| 3 | class Nip_Form_Renderer_DisplayGroup |
||
|
|
|||
| 4 | { |
||
| 5 | |||
| 6 | /** |
||
| 7 | * @var Nip_Form_DisplayGroup |
||
| 8 | */ |
||
| 9 | protected $_group; |
||
| 10 | |||
| 11 | |||
| 12 | /** |
||
| 13 | * @return Nip_Form_Renderer_DisplayGroup |
||
| 14 | */ |
||
| 15 | public function setGroup(Nip_Form_DisplayGroup $group) |
||
| 16 | { |
||
| 17 | $this->_group = $group; |
||
| 18 | return $this; |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @return Nip_Form_Renderer_DisplayGroup|null |
||
| 23 | */ |
||
| 24 | public function getGroup() |
||
| 25 | { |
||
| 26 | return $this->_group; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function render() |
||
| 30 | { |
||
| 31 | $return = '<fieldset' . $this->renderAttributes() . '>'; |
||
| 32 | $return .= '<legend>' . $this->getGroup()->getLegend() . '</legend>'; |
||
| 33 | |||
| 34 | $renderer = clone $this->getGroup()->getForm()->getRenderer(); |
||
| 35 | $renderer->setElements($this->getGroup()->toArray()); |
||
| 36 | $return .= $renderer->renderElements(); |
||
| 37 | $return .= '</fieldset>'; |
||
| 38 | return $return; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function renderAttributes($overrides = array()) |
||
| 42 | { |
||
| 43 | $attribs = $this->getGroup()->getAttribs(); |
||
| 44 | $elementAttribs = $this->getElementAttribs(); |
||
| 45 | $return = ''; |
||
| 46 | foreach ($attribs as $name => $value) { |
||
| 47 | if (in_array($name, $elementAttribs)) { |
||
| 48 | if (in_array($name, array_keys($overrides))) { |
||
| 49 | $value = $overrides[$name]; |
||
| 50 | } |
||
| 51 | $return .= ' ' . $name . '="' . $value . '"'; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | return $return; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function getElementAttribs() |
||
| 60 | } |
||
| 61 | } |
||
| 62 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.