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 |
||
| 9 | class MapRenderer implements WebRenderer { |
||
| 10 | |||
| 11 | /** @var RendererRegistry */ |
||
| 12 | protected $renderers; |
||
| 13 | |||
| 14 | /** @var LinkPrinter */ |
||
| 15 | private $links; |
||
| 16 | |||
| 17 | public function __construct(RendererRegistry $renderers, LinkPrinter $links) { |
||
| 18 | $this->renderers = $renderers; |
||
| 19 | $this->links = $links; |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param mixed $value |
||
| 24 | * @return bool |
||
| 25 | */ |
||
| 26 | public function handles($value) { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param array $array |
||
| 32 | * @return mixed |
||
| 33 | */ |
||
| 34 | public function render($array) { |
||
| 35 | $content = [ |
||
| 36 | new Element('div', ['class' => 'panel-body'], [ |
||
| 37 | $this->renderArray($array) |
||
| 38 | ]) |
||
| 39 | ]; |
||
| 40 | |||
| 41 | $links = $this->links->createLinkElements($array); |
||
| 42 | if ($links) { |
||
|
|
|||
| 43 | array_unshift($content, new Element('div', ['class' => 'panel-heading clearfix'], [ |
||
| 44 | new Element('small', ['class' => 'pull-right'], $links) |
||
| 45 | ])); |
||
| 46 | } |
||
| 47 | return new Element('div', ['class' => 'panel panel-default'], $content); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function renderArray(array $array) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param array $array |
||
| 70 | * @return array|Element[] |
||
| 71 | */ |
||
| 72 | public function headElements($array) { |
||
| 82 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.