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 |
||
16 | class ColumnLink extends Column |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $title; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $class; |
||
28 | |||
29 | /** |
||
30 | * @var DataGrid |
||
31 | */ |
||
32 | protected $grid; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $params; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $href; |
||
43 | |||
44 | |||
45 | /** |
||
46 | * @param DataGrid $grid |
||
47 | * @param string $column |
||
48 | * @param string $name |
||
49 | * @param string $href |
||
50 | * @param array $params |
||
51 | */ |
||
52 | public function __construct(DataGrid $grid, $column, $name, $href, $params) |
||
53 | { |
||
54 | parent::__construct($column, $name); |
||
55 | |||
56 | $this->href = $href; |
||
57 | $this->grid = $grid; |
||
58 | $this->params = $params; |
||
59 | } |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Render row item into template |
||
64 | * @param Row $row |
||
65 | * @return mixed |
||
66 | */ |
||
67 | public function render(Row $row) |
||
68 | { |
||
69 | /** |
||
70 | * Renderer function may be used |
||
71 | */ |
||
72 | View Code Duplication | if ($renderer = $this->getRenderer()) { |
|
|
|||
73 | if (!$renderer->getConditionCallback()) { |
||
74 | return call_user_func_array($renderer->getCallback(), [$row->getItem()]); |
||
75 | } |
||
76 | |||
77 | if (call_user_func_array($renderer->getConditionCallback(), [$row->getItem()])) { |
||
78 | return call_user_func_array($renderer->getCallback(), [$row->getItem()]); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | $value = parent::render($row); |
||
83 | |||
84 | $a = Html::el('a') |
||
85 | ->href($this->createLink($this->href, $this->getItemParams($row))) |
||
86 | ->setText($value); |
||
87 | |||
88 | if ($this->title) { $a->title($this->title); } |
||
89 | if ($this->class) { $a->class($this->class); } |
||
90 | |||
91 | return $a; |
||
92 | } |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Set attribute title |
||
97 | * @param string $title |
||
98 | */ |
||
99 | public function setTitle($title) |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Get attribute title |
||
109 | */ |
||
110 | public function getTitle() |
||
114 | |||
115 | |||
116 | /** |
||
117 | * Set attribute class |
||
118 | * @param string $class |
||
119 | */ |
||
120 | public function setClass($class) |
||
126 | |||
127 | |||
128 | /** |
||
129 | * Get attribute class |
||
130 | */ |
||
131 | public function getClass() |
||
135 | |||
136 | /** |
||
137 | * Get item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
138 | * @param Row $row |
||
139 | * @return array |
||
140 | */ |
||
141 | View Code Duplication | protected function getItemParams(Row $row) |
|
151 | |||
152 | } |
||
153 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.