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 |
||
| 18 | class UnorderedList |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @deprecated |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public static $defaultTag = 'ul'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @deprecated |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $tag; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $options = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $items = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param array $options |
||
| 45 | */ |
||
| 46 | 1 | public function __construct($options = []) |
|
| 47 | { |
||
| 48 | Html::addCssClass($options, FA::$cssPrefix . '-ul'); |
||
| 49 | |||
| 50 | $options['item'] = function ($item, $index) { |
||
| 51 | return call_user_func($item, $index); |
||
| 52 | 1 | }; |
|
| 53 | |||
| 54 | 1 | $this->options = $options; |
|
| 55 | 1 | } |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function __toString() |
||
| 61 | { |
||
| 62 | return Html::ul($this->items, $this->options); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $label |
||
| 67 | * @param array $options |
||
| 68 | * @return static |
||
| 69 | */ |
||
| 70 | public function item($label, $options = []) |
||
| 71 | { |
||
| 72 | 1 | $this->items[] = function ($index) use ($label, $options) { |
|
|
|
|||
| 73 | $tag = ArrayHelper::remove($options, 'tag', 'li'); |
||
| 74 | |||
| 75 | $icon = ArrayHelper::remove($options, 'icon'); |
||
| 76 | 1 | $icon = empty($icon) |
|
| 77 | ? null |
||
| 78 | : (is_string($icon) ? (string)(new Icon($icon))->li() : $icon); |
||
| 79 | |||
| 80 | $content = trim($icon . $label); |
||
| 81 | |||
| 82 | return Html::tag($tag, $content, $options); |
||
| 83 | 1 | }; |
|
| 84 | |||
| 85 | 1 | return $this; |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @deprecated |
||
| 90 | * Change html tag. |
||
| 91 | * @param string $tag |
||
| 92 | * @return static |
||
| 93 | * @throws \yii\base\InvalidParamException |
||
| 94 | */ |
||
| 95 | 1 | public function tag($tag) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @deprecated |
||
| 106 | * @param string|null $tag |
||
| 107 | * @param array $options |
||
| 108 | * @return string |
||
| 109 | * @throws \yii\base\InvalidConfigException |
||
| 110 | */ |
||
| 111 | View Code Duplication | public function render($tag = null, $options = []) |
|
| 123 | } |
||
| 124 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.