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 |
||
| 17 | abstract class AbstractRenderer |
||
| 18 | { |
||
| 19 | protected $form; |
||
| 20 | |||
| 21 | protected $elements; |
||
| 22 | protected $elementsRenderer; |
||
| 23 | |||
| 24 | protected $buttonsRenderer = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * AbstractRenderer constructor. |
||
| 28 | */ |
||
| 29 | 7 | public function __construct() |
|
| 32 | |||
| 33 | /** |
||
| 34 | * @return array |
||
| 35 | */ |
||
| 36 | public function getElements() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param $elements |
||
| 47 | */ |
||
| 48 | public function setElements($elements) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return AbstractForm |
||
| 55 | */ |
||
| 56 | public function getForm() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param AbstractForm $form |
||
| 63 | * @return $this |
||
| 64 | */ |
||
| 65 | 7 | public function setForm(AbstractForm $form) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function render() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function openTag() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function renderHidden() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param AbstractElement $element |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | public function renderElement(AbstractElement $element) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The errors are rendered using the Errors View Helper |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function renderMessages() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function renderGroups() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function renderElements() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function renderButtons() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function closeTag() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string|AbstractElement $label |
||
| 205 | * @param bool $required |
||
| 206 | * @param bool $error |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function renderLabel($label, $required = false, $error = false) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param AbstractElement $element |
||
| 232 | * @return mixed |
||
| 233 | */ |
||
| 234 | 4 | public function getElementRenderer(AbstractElement $element) |
|
| 235 | { |
||
| 236 | 4 | $name = $element->getUniqueId(); |
|
| 237 | 4 | if (!isset($this->elementsRenderer[$name])) { |
|
| 238 | 4 | $this->elementsRenderer[$name] = $this->getNewElementRenderer($element); |
|
| 239 | } |
||
| 240 | |||
| 241 | 4 | return $this->elementsRenderer[$name]; |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param AbstractElement $element |
||
| 246 | * @return mixed |
||
| 247 | */ |
||
| 248 | 4 | View Code Duplication | protected function getNewElementRenderer(AbstractElement $element) |
| 249 | { |
||
| 250 | 4 | $type = $element->getType(); |
|
| 251 | 4 | $name = 'Nip_Form_Renderer_Elements_' . ucfirst($type); |
|
| 252 | /** @var AbstractElementRenderer $renderer */ |
||
| 253 | 4 | $renderer = new $name(); |
|
| 254 | 4 | $renderer->setRenderer($this); |
|
| 255 | 4 | $renderer->setElement($element); |
|
| 256 | |||
| 257 | 4 | return $renderer; |
|
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param Nip_Form_Button_Abstract $button |
||
| 262 | * @return mixed |
||
| 263 | */ |
||
| 264 | public function getButtonRenderer(Nip_Form_Button_Abstract $button) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param Nip_Form_Button_Abstract $button |
||
| 276 | * @return mixed |
||
| 277 | */ |
||
| 278 | View Code Duplication | protected function getNewButtonRenderer(Nip_Form_Button_Abstract $button) |
|
| 289 | } |
||
| 290 |
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: