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 |
||
| 13 | class Builder |
||
| 14 | { |
||
| 15 | use Macroable; |
||
| 16 | use HasOptions; |
||
| 17 | use HasTable; |
||
| 18 | use HasEditor; |
||
| 19 | use Columns\Index; |
||
| 20 | use Columns\Action; |
||
| 21 | use Columns\Checkbox; |
||
| 22 | |||
| 23 | // Select plugin constants. |
||
| 24 | const SELECT_STYLE_API = 'api'; |
||
| 25 | const SELECT_STYLE_SINGLE = 'single'; |
||
| 26 | const SELECT_STYLE_MULTI = 'multi'; |
||
| 27 | const SELECT_STYLE_OS = 'os'; |
||
| 28 | const SELECT_STYLE_MULTI_SHIFT = 'multi+shift'; |
||
| 29 | const SELECT_ITEMS_ROW = 'row'; |
||
| 30 | const SELECT_ITEMS_COLUMN = 'column'; |
||
| 31 | const SELECT_ITEMS_CELL = 'cell'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Collection |
||
| 35 | */ |
||
| 36 | public $collection; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Repository |
||
| 40 | */ |
||
| 41 | public $config; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Factory |
||
| 45 | */ |
||
| 46 | public $view; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var HtmlBuilder |
||
| 50 | */ |
||
| 51 | public $html; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $tableAttributes = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $template = ''; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $attributes = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param Repository $config |
||
| 70 | * @param Factory $view |
||
| 71 | * @param HtmlBuilder $html |
||
| 72 | */ |
||
| 73 | public function __construct(Repository $config, Factory $view, HtmlBuilder $html) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Generate DataTable javascript. |
||
| 84 | * |
||
| 85 | * @param null $script |
||
| 86 | * @param array $attributes |
||
| 87 | * @return \Illuminate\Support\HtmlString |
||
| 88 | * @throws \Exception |
||
| 89 | */ |
||
| 90 | public function scripts($script = null, array $attributes = ['type' => 'text/javascript']) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get generated raw scripts. |
||
| 100 | * |
||
| 101 | * @return \Illuminate\Support\HtmlString |
||
| 102 | * @throws \Exception |
||
| 103 | */ |
||
| 104 | public function generateScripts() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get generated json configuration. |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function generateJson() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Generate DataTables js parameters. |
||
| 137 | * |
||
| 138 | * @param array $attributes |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function parameterize($attributes = []) |
||
| 142 | { |
||
| 143 | $parameters = (new Parameters($attributes))->toArray(); |
||
| 144 | |||
| 145 | $values = []; |
||
| 146 | $replacements = []; |
||
| 147 | |||
| 148 | foreach (array_dot($parameters) as $key => $value) { |
||
| 149 | View Code Duplication | if ($this->isCallbackFunction($value, $key)) { |
|
| 150 | $values[] = trim($value); |
||
| 151 | array_set($parameters, $key, '%' . $key . '%'); |
||
| 152 | $replacements[] = '"%' . $key . '%"'; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $new = []; |
||
| 157 | foreach ($parameters as $key => $value) { |
||
| 158 | array_set($new, $key, $value); |
||
| 159 | } |
||
| 160 | |||
| 161 | $json = json_encode($new); |
||
| 162 | |||
| 163 | $json = str_replace($replacements, $values, $json); |
||
| 164 | |||
| 165 | return $json; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Check if given key & value is a valid callback js function. |
||
| 170 | * |
||
| 171 | * @param string $value |
||
| 172 | * @param string $key |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | protected function isCallbackFunction($value, $key) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get javascript template to use. |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | protected function template() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Generate DataTable's table html. |
||
| 200 | * |
||
| 201 | * @param array $attributes |
||
| 202 | * @param bool $drawFooter |
||
| 203 | * @param bool $drawSearch |
||
| 204 | * @return \Illuminate\Support\HtmlString |
||
| 205 | */ |
||
| 206 | public function table(array $attributes = [], $drawFooter = false, $drawSearch = false) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Configure DataTable's parameters. |
||
| 228 | * |
||
| 229 | * @param array $attributes |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | public function parameters(array $attributes = []) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Set custom javascript template. |
||
| 241 | * |
||
| 242 | * @param string $template |
||
| 243 | * @return $this |
||
| 244 | */ |
||
| 245 | public function setTemplate($template) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Make a data script to be appended on ajax request of dataTables. |
||
| 254 | * |
||
| 255 | * @param array $data |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | protected function makeDataScript(array $data) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Generate scripts that sets the dataTables options into a variable. |
||
| 270 | * |
||
| 271 | * @return $this |
||
| 272 | */ |
||
| 273 | public function asOptions() |
||
| 279 | } |
||
| 280 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..