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 |
||
| 10 | class TableRenderer implements WebRenderer { |
||
| 11 | |||
| 12 | /** @var RendererRegistry */ |
||
| 13 | private $renderers; |
||
| 14 | |||
| 15 | /** @var LinkPrinter */ |
||
| 16 | private $printer; |
||
| 17 | |||
| 18 | public function __construct(RendererRegistry $renderers, LinkPrinter $printer) { |
||
| 19 | $this->renderers = $renderers; |
||
| 20 | $this->printer = $printer; |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param mixed $value |
||
| 25 | * @return bool |
||
| 26 | */ |
||
| 27 | public function handles($value) { |
||
| 28 | return $value instanceof Table; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param Table $value |
||
| 33 | * @return mixed |
||
| 34 | */ |
||
| 35 | public function render($value) { |
||
| 36 | $rows = $this->renderRows($value); |
||
| 37 | |||
| 38 | if (!$rows) { |
||
|
|
|||
| 39 | return null; |
||
| 40 | }; |
||
| 41 | |||
| 42 | return (string)new Element('table', ['class' => 'table table-striped'], array_merge([ |
||
| 43 | new Element('thead', [], [new Element('tr', [], $this->renderHeaders($value))]) |
||
| 44 | ], $rows)); |
||
| 45 | } |
||
| 46 | |||
| 47 | private function renderHeaders($table) { |
||
| 48 | $headers = [new Element('th', ['width' => '1'])]; |
||
| 49 | foreach ($this->getHeaders($table) as $caption) { |
||
| 50 | $headers[] = new Element('th', [], [$caption]); |
||
| 51 | } |
||
| 52 | return $headers; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param Table $table |
||
| 57 | * @return array |
||
| 58 | * @throws \Exception |
||
| 59 | */ |
||
| 60 | private function renderRows($table) { |
||
| 61 | $rows = []; |
||
| 62 | foreach ($table->getItems() as $item) { |
||
| 63 | $row = [new Element('td', [], $this->printer->createDropDown($item))]; |
||
| 64 | View Code Duplication | foreach ($table->getCells($item) as $cell) { |
|
| 65 | $row[] = new Element('td', [], [$this->renderers->getRenderer($cell)->render($cell)]); |
||
| 66 | } |
||
| 67 | |||
| 68 | $rows[] = new Element('tr', [], $row); |
||
| 69 | } |
||
| 70 | return $rows; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param Table $value |
||
| 75 | * @return array|Element[] |
||
| 76 | */ |
||
| 77 | public function headElements($value) { |
||
| 78 | $elements = []; |
||
| 79 | foreach ($value->getItems() as $item) { |
||
| 80 | View Code Duplication | foreach ($value->getCells($item) as $cell) { |
|
| 81 | $renderer = $this->renderers->getRenderer($cell); |
||
| 82 | if ($renderer instanceof WebRenderer) { |
||
| 83 | $elements = array_merge($elements, $renderer->headElements($cell)); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | return $elements; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param Table $table |
||
| 92 | * @return mixed |
||
| 93 | */ |
||
| 94 | protected function getHeaders($table) { |
||
| 97 | } |
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.