| Total Complexity | 47 |
| Total Lines | 529 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DataGridActionBase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataGridActionBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | abstract class DataGridActionBase implements DataGridActionInterface |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * The runtime function that determines if the action should be displayed |
||
| 40 | * |
||
| 41 | * @var callable |
||
| 42 | */ |
||
| 43 | protected $runtimeFilter; |
||
| 44 | /** |
||
| 45 | * The runtime function to pass in the row dato to the action |
||
| 46 | * |
||
| 47 | * @var callable |
||
| 48 | */ |
||
| 49 | protected $runtimeData; |
||
| 50 | /** |
||
| 51 | * Action's name |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $name = ''; |
||
| 56 | /** |
||
| 57 | * Action's title |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $title = ''; |
||
| 62 | /** |
||
| 63 | * Action's title ID |
||
| 64 | * |
||
| 65 | * @var int |
||
| 66 | */ |
||
| 67 | protected $id = 0; |
||
| 68 | /** |
||
| 69 | * The JavaScript function to be triggered on OnClick event |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $onClickFunction = ''; |
||
| 74 | /** |
||
| 75 | * The OnClick event arguments |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $onClickArgs; |
||
| 80 | /** |
||
| 81 | * Action's icon |
||
| 82 | * |
||
| 83 | * @var IconInterface |
||
| 84 | */ |
||
| 85 | protected $icon; |
||
| 86 | /** |
||
| 87 | * Sets whether this action should be skipped from listing in rows |
||
| 88 | * |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | protected $isSkip = false; |
||
| 92 | /** |
||
| 93 | * The row name which determines whether the action is displayed |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $filterRowSource; |
||
| 98 | /** |
||
| 99 | * Sets as a help action |
||
| 100 | * |
||
| 101 | * @var bool |
||
| 102 | */ |
||
| 103 | protected $isHelper; |
||
| 104 | /** |
||
| 105 | * Action's type |
||
| 106 | * |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | protected $type = 0; |
||
| 110 | /** |
||
| 111 | * Data attributes (ie. data-*) |
||
| 112 | * |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | protected $data; |
||
| 116 | /** |
||
| 117 | * Additional attributes (ie. name=*) |
||
| 118 | * |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | protected $attributes; |
||
| 122 | /** |
||
| 123 | * CSS classes |
||
| 124 | * |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | protected $classes; |
||
| 128 | /** |
||
| 129 | * Sets as a selection action, that is, to be displayed on a selection menu |
||
| 130 | * |
||
| 131 | * @var bool |
||
| 132 | */ |
||
| 133 | protected $isSelection = false; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * DataGridActionBase constructor. |
||
| 137 | * |
||
| 138 | * @param int $id EL id de la acción |
||
| 139 | */ |
||
| 140 | public function __construct($id = null) |
||
| 141 | { |
||
| 142 | $this->id = $id; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Devolver el método reflexivo que determina si se muestra la acción |
||
| 147 | * |
||
| 148 | * @return callable |
||
| 149 | */ |
||
| 150 | public function getRuntimeFilter() |
||
| 151 | { |
||
| 152 | return $this->runtimeFilter; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Establecer el método reflexivo que determina si se muestra la acción |
||
| 157 | * |
||
| 158 | * @param string $class |
||
| 159 | * @param string $method |
||
| 160 | * |
||
| 161 | * @throws \RuntimeException |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | public function setRuntimeFilter($class, $method) |
||
| 165 | { |
||
| 166 | if (method_exists($class, $method)) { |
||
| 167 | $this->runtimeFilter = function ($filter) use ($method) { |
||
| 168 | // new \ReflectionMethod($class, $method); |
||
| 169 | return $filter->{$method}(); |
||
| 170 | }; |
||
| 171 | } else { |
||
| 172 | throw new \RuntimeException('Method does not exist'); |
||
| 173 | } |
||
| 174 | |||
| 175 | return $this; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function getName() |
||
| 182 | { |
||
| 183 | return $this->name; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param $name string |
||
| 188 | * |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | public function setName($name) |
||
| 192 | { |
||
| 193 | $this->name = $name; |
||
| 194 | |||
| 195 | return $this; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getId() |
||
| 202 | { |
||
| 203 | return $this->id; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param int $id |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function setId($id) |
||
| 212 | { |
||
| 213 | $this->id = $id; |
||
| 214 | |||
| 215 | return $this; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getTitle() |
||
| 222 | { |
||
| 223 | return $this->title; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param $title string |
||
| 228 | * |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | public function setTitle($title) |
||
| 232 | { |
||
| 233 | $this->title = $title; |
||
| 234 | |||
| 235 | return $this; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param $function string |
||
| 240 | * |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | public function setOnClickFunction($function) |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param $args string |
||
| 252 | * |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | public function setOnClickArgs($args) |
||
| 256 | { |
||
| 257 | if ($this->onClickArgs === null) { |
||
| 258 | $this->onClickArgs = []; |
||
| 259 | } |
||
| 260 | |||
| 261 | $this->onClickArgs[] = $args; |
||
| 262 | |||
| 263 | return $this; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function getOnClick() |
||
| 270 | { |
||
| 271 | if ($this->onClickArgs !== null) { |
||
| 272 | |||
| 273 | $args = array_map(function ($value) { |
||
| 274 | return (!is_numeric($value) && $value !== 'this') ? '\'' . $value . '\'' : $value; |
||
| 275 | }, $this->onClickArgs); |
||
| 276 | |||
| 277 | return count($args) > 0 ? $this->onClickFunction . '(' . implode(',', $args) . ')' : $this->onClickFunction; |
||
| 278 | } |
||
| 279 | |||
| 280 | return $this->onClickFunction; |
||
| 281 | |||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return IconInterface |
||
| 286 | */ |
||
| 287 | public function getIcon() |
||
| 288 | { |
||
| 289 | return $this->icon; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param $icon IconInterface |
||
| 294 | * |
||
| 295 | * @return $this |
||
| 296 | */ |
||
| 297 | public function setIcon($icon) |
||
| 298 | { |
||
| 299 | $this->icon = $icon; |
||
| 300 | |||
| 301 | return $this; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param $skip bool |
||
| 306 | * |
||
| 307 | * @return $this |
||
| 308 | */ |
||
| 309 | public function setSkip($skip) |
||
| 310 | { |
||
| 311 | $this->isSkip = $skip; |
||
| 312 | |||
| 313 | return $this; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function isSkip() |
||
| 320 | { |
||
| 321 | return $this->isSkip; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function isHelper() |
||
| 328 | { |
||
| 329 | return $this->isHelper; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param bool $helper |
||
| 334 | * |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function setIsHelper($helper) |
||
| 338 | { |
||
| 339 | $this->isHelper = $helper; |
||
| 340 | |||
| 341 | return $this; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | public function getFilterRowSource() |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Filtro para mostrar la acción |
||
| 354 | * |
||
| 355 | * @param $rowSource string |
||
| 356 | * @param mixed $value Valor a filtrar |
||
| 357 | * |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | public function setFilterRowSource($rowSource, $value = 1) |
||
| 361 | { |
||
| 362 | if ($this->filterRowSource === null) { |
||
| 363 | $this->filterRowSource = []; |
||
| 364 | } |
||
| 365 | |||
| 366 | $this->filterRowSource[] = ['field' => $rowSource, 'value' => $value]; |
||
| 367 | |||
| 368 | return $this; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @return int El tipo de acción |
||
| 373 | */ |
||
| 374 | public function getType() |
||
| 375 | { |
||
| 376 | return $this->type; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param int $type El tipo de acción definido en DataGridActionType |
||
| 381 | * |
||
| 382 | * @return $this |
||
| 383 | */ |
||
| 384 | public function setType($type) |
||
| 385 | { |
||
| 386 | $this->type = $type; |
||
| 387 | |||
| 388 | return $this; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | public function getData() |
||
| 395 | { |
||
| 396 | return (array)$this->data; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param array $data Los datos de los atributos |
||
| 401 | * |
||
| 402 | * @return $this |
||
| 403 | */ |
||
| 404 | public function setData(array $data) |
||
| 405 | { |
||
| 406 | $this->data = $data; |
||
| 407 | |||
| 408 | return $this; |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Añadir nuevo atributo de datos |
||
| 413 | * |
||
| 414 | * @param string $name El nombe del atributo |
||
| 415 | * @param mixed $data Los datos del atributo |
||
| 416 | * |
||
| 417 | * @return $this |
||
| 418 | */ |
||
| 419 | public function addData($name, $data) |
||
| 420 | { |
||
| 421 | if ($this->data === null) { |
||
| 422 | $this->data = []; |
||
| 423 | } |
||
| 424 | |||
| 425 | $this->data[$name] = $data; |
||
| 426 | |||
| 427 | return $this; |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | public function getAttributes() |
||
| 434 | { |
||
| 435 | return (array)$this->attributes; |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Establecer atributos |
||
| 440 | * |
||
| 441 | * @param array $attributes Los datos de los atributos |
||
| 442 | * |
||
| 443 | * @return $this |
||
| 444 | */ |
||
| 445 | public function setAttributes(array $attributes) |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Añadir nuevo atributo |
||
| 454 | * |
||
| 455 | * @param string $name El nombe del atributo |
||
| 456 | * @param mixed $value |
||
| 457 | * |
||
| 458 | * @return $this |
||
| 459 | */ |
||
| 460 | public function addAttribute($name, $value) |
||
| 461 | { |
||
| 462 | if ($this->attributes === null) { |
||
| 463 | $this->attributes = []; |
||
| 464 | } |
||
| 465 | |||
| 466 | $this->attributes[$name] = $value; |
||
| 467 | |||
| 468 | return $this; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Returns classes as a string |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public function getClassesAsString() |
||
| 477 | { |
||
| 478 | if ($this->classes === null) { |
||
| 479 | return ''; |
||
| 480 | } |
||
| 481 | |||
| 482 | return implode(' ', $this->classes); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Returns classes |
||
| 487 | * |
||
| 488 | * @return array |
||
| 489 | */ |
||
| 490 | public function getClasses() |
||
| 491 | { |
||
| 492 | return (array)$this->classes; |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Set classes |
||
| 497 | * |
||
| 498 | * @param array $classes |
||
| 499 | */ |
||
| 500 | public function setClasses(array $classes) |
||
| 501 | { |
||
| 502 | $this->classes = $classes; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Adds a new class |
||
| 507 | * |
||
| 508 | * @param mixed $value |
||
| 509 | * |
||
| 510 | * @return $this |
||
| 511 | */ |
||
| 512 | public function addClass($value) |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Returns if the action is used for selecting multiple items |
||
| 525 | * |
||
| 526 | * @return bool |
||
| 527 | */ |
||
| 528 | public function isSelection(): bool |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @param bool $isSelection |
||
| 535 | * |
||
| 536 | * @return DataGridActionBase |
||
| 537 | */ |
||
| 538 | public function setIsSelection(bool $isSelection) |
||
| 539 | { |
||
| 540 | $this->isSelection = $isSelection; |
||
| 541 | |||
| 542 | return $this; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @return callable |
||
| 547 | */ |
||
| 548 | public function getRuntimeData() |
||
| 549 | { |
||
| 550 | return $this->runtimeData; |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Sets the runtime data function |
||
| 555 | * |
||
| 556 | * @param callable $function |
||
| 557 | * |
||
| 558 | * @return $this |
||
| 559 | */ |
||
| 560 | public function setRuntimeData(callable $function) |
||
| 565 | } |
||
| 566 | } |
||
| 567 |