| Total Complexity | 48 |
| Total Lines | 501 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DataGridBase 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 DataGridBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | abstract class DataGridBase implements DataGridInterface |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * Tiempo de ejecución |
||
| 49 | * |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | protected $time = 0; |
||
| 53 | /** |
||
| 54 | * El id de la matriz |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $id = ''; |
||
| 59 | /** |
||
| 60 | * La cabecera de la matriz |
||
| 61 | * |
||
| 62 | * @var DataGridHeaderInterface |
||
| 63 | */ |
||
| 64 | protected $header; |
||
| 65 | /** |
||
| 66 | * Los datos de la matriz |
||
| 67 | * |
||
| 68 | * @var DataGridData |
||
| 69 | */ |
||
| 70 | protected $data; |
||
| 71 | /** |
||
| 72 | * El paginador |
||
| 73 | * |
||
| 74 | * @var DataGridPagerBase |
||
| 75 | */ |
||
| 76 | protected $pager; |
||
| 77 | /** |
||
| 78 | * Las acciones asociadas a los elementos de la matriz |
||
| 79 | * |
||
| 80 | * @var DataGridActionInterface[] |
||
| 81 | */ |
||
| 82 | protected $actions = []; |
||
| 83 | /** |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | protected $actionsCount = 0; |
||
| 87 | /** |
||
| 88 | * Las acciones asociadas a los elementos de la matriz que se muestran en un menú |
||
| 89 | * |
||
| 90 | * @var DataGridActionInterface[] |
||
| 91 | */ |
||
| 92 | protected $actionsMenu = []; |
||
| 93 | /** |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | protected $actionsMenuCount = 0; |
||
| 97 | /** |
||
| 98 | * La acción a realizar al cerrar la matriz |
||
| 99 | * |
||
| 100 | * @var int |
||
| 101 | */ |
||
| 102 | protected $onCloseAction = 0; |
||
| 103 | /** |
||
| 104 | * La plantilla a utilizar para presentar la cabecera |
||
| 105 | * |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | protected $headerTemplate; |
||
| 109 | /** |
||
| 110 | * La plantilla a utilizar para presentar las acciones |
||
| 111 | * |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected $actionsTemplate; |
||
| 115 | /** |
||
| 116 | * La plantilla a utilizar para presentar el paginador |
||
| 117 | * |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | protected $pagerTemplate; |
||
| 121 | /** |
||
| 122 | * La plantilla a utilizar para presentar los datos |
||
| 123 | * |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | protected $rowsTemplate; |
||
| 127 | /** |
||
| 128 | * La plantilla a utilizar para presentar la tabla |
||
| 129 | * |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | protected $tableTemplate; |
||
| 133 | /** |
||
| 134 | * @var Theme |
||
| 135 | */ |
||
| 136 | protected $theme; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * DataGridBase constructor. |
||
| 140 | * |
||
| 141 | * @param ThemeInterface $theme |
||
| 142 | */ |
||
| 143 | public function __construct(ThemeInterface $theme) |
||
| 144 | { |
||
| 145 | $this->theme = $theme; |
||
|
|
|||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return int |
||
| 150 | */ |
||
| 151 | public function getOnCloseAction() |
||
| 152 | { |
||
| 153 | return $this->onCloseAction; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param \SP\Core\Acl\ActionsInterface $action |
||
| 158 | * |
||
| 159 | * @return $this |
||
| 160 | */ |
||
| 161 | public function setOnCloseAction(ActionsInterface $action) |
||
| 162 | { |
||
| 163 | $this->onCloseAction = $action; |
||
| 164 | |||
| 165 | return $this; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getId() |
||
| 172 | { |
||
| 173 | return $this->id; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param $id string |
||
| 178 | * |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function setId($id) |
||
| 182 | { |
||
| 183 | $this->id = $id; |
||
| 184 | |||
| 185 | return $this; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return DataGridHeader|DataGridHeaderSort|DataGridHeaderInterface |
||
| 190 | */ |
||
| 191 | public function getHeader() |
||
| 192 | { |
||
| 193 | return $this->header; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param DataGridHeaderInterface $header |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setHeader(DataGridHeaderInterface $header) |
||
| 202 | { |
||
| 203 | $this->header = $header; |
||
| 204 | |||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return DataGridDataInterface |
||
| 210 | */ |
||
| 211 | public function getData() |
||
| 212 | { |
||
| 213 | return $this->data; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param DataGridDataInterface $data |
||
| 218 | * |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | public function setData(DataGridDataInterface $data) |
||
| 222 | { |
||
| 223 | $this->data = $data; |
||
| 224 | |||
| 225 | return $this; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param DataGridActionInterface $action |
||
| 230 | * @param bool $isMenu Añadir al menu de acciones |
||
| 231 | * |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | public function addDataAction(DataGridActionInterface $action, $isMenu = false) |
||
| 235 | { |
||
| 236 | if ($isMenu === false) { |
||
| 237 | $this->actions[] = $action; |
||
| 238 | |||
| 239 | if (!$action->isSkip()) { |
||
| 240 | $this->actionsCount++; |
||
| 241 | } |
||
| 242 | } else { |
||
| 243 | $this->actionsMenu[] = $action; |
||
| 244 | |||
| 245 | if (!$action->isSkip()) { |
||
| 246 | $this->actionsMenuCount++; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | return $this; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return DataGridActionInterface[] |
||
| 255 | */ |
||
| 256 | public function getDataActions() |
||
| 257 | { |
||
| 258 | return $this->actions; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | public function getGrid() |
||
| 265 | { |
||
| 266 | return $this; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Establecer la plantilla utilizada para la cabecera |
||
| 271 | * |
||
| 272 | * @param string $template El nombre de la plantilla a utilizar |
||
| 273 | * @param string $base Directorio base para la plantilla |
||
| 274 | * |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | public function setDataHeaderTemplate($template, $base = null) |
||
| 278 | { |
||
| 279 | try { |
||
| 280 | $this->headerTemplate = $this->checkTemplate($template, $base); |
||
| 281 | } catch (FileNotFoundException $e) { |
||
| 282 | processException($e); |
||
| 283 | } |
||
| 284 | |||
| 285 | return $this; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Comprobar si existe una plantilla y devolver la ruta completa |
||
| 290 | * |
||
| 291 | * @param $template |
||
| 292 | * @param null $base |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | * @throws FileNotFoundException |
||
| 296 | */ |
||
| 297 | protected function checkTemplate($template, $base = null) |
||
| 298 | { |
||
| 299 | $template = null === $base ? $template . '.inc' : $base . DIRECTORY_SEPARATOR . $template . '.inc'; |
||
| 300 | $file = $this->theme->getViewsPath() . DIRECTORY_SEPARATOR . $template; |
||
| 301 | |||
| 302 | if (!is_readable($file)) { |
||
| 303 | throw new FileNotFoundException(sprintf(__('No es posible obtener la plantilla "%s" : %s'), $template, $file)); |
||
| 304 | } |
||
| 305 | |||
| 306 | return $file; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Devolver la plantilla utilizada para la cabecera |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function getDataHeaderTemplate() |
||
| 315 | { |
||
| 316 | return $this->headerTemplate; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Establecer la plantilla utilizada para las acciones |
||
| 321 | * |
||
| 322 | * @param string $template El nombre de la plantilla a utilizar |
||
| 323 | * |
||
| 324 | * @return $this |
||
| 325 | */ |
||
| 326 | public function setDataActionsTemplate($template) |
||
| 327 | { |
||
| 328 | try { |
||
| 329 | $this->actionsTemplate = $this->checkTemplate($template); |
||
| 330 | } catch (FileNotFoundException $e) { |
||
| 331 | logger($e->getMessage()); |
||
| 332 | } |
||
| 333 | |||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Devolver la plantilla utilizada para las acciones |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getDataActionsTemplate() |
||
| 343 | { |
||
| 344 | return $this->actionsTemplate; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Establecer la plantilla utilizada para el paginador |
||
| 349 | * |
||
| 350 | * @param string $template El nombre de la plantilla a utilizar |
||
| 351 | * @param string $base Directorio base para la plantilla |
||
| 352 | * |
||
| 353 | * @return $this |
||
| 354 | */ |
||
| 355 | public function setDataPagerTemplate($template, $base = null) |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Devolver la plantilla utilizada para el paginador |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public function getDataPagerTemplate() |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $template El nombre de la plantilla a utilizar |
||
| 378 | * @param string $base Directorio base para la plantilla |
||
| 379 | * |
||
| 380 | * @return mixed |
||
| 381 | */ |
||
| 382 | public function setDataRowTemplate($template, $base = null) |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public function getDataRowTemplate() |
||
| 397 | { |
||
| 398 | return $this->rowsTemplate; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Devolver el paginador |
||
| 403 | * |
||
| 404 | * @return DataGridPagerInterface |
||
| 405 | */ |
||
| 406 | public function getPager() |
||
| 407 | { |
||
| 408 | return $this->pager; |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Establecer el paginador |
||
| 413 | * |
||
| 414 | * @param DataGridPagerInterface $pager |
||
| 415 | * |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | public function setPager(DataGridPagerInterface $pager) |
||
| 419 | { |
||
| 420 | $this->pager = $pager; |
||
| 421 | |||
| 422 | return $this; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Actualizar los datos del paginador |
||
| 427 | */ |
||
| 428 | public function updatePager() |
||
| 429 | { |
||
| 430 | if ($this->pager instanceof DataGridPagerInterface) { |
||
| 431 | $this->pager->setTotalRows($this->data->getDataCount()); |
||
| 432 | } |
||
| 433 | |||
| 434 | return $this; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return int |
||
| 439 | */ |
||
| 440 | public function getTime() |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param int $time |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | public function setTime($time) |
||
| 451 | { |
||
| 452 | $this->time = $time; |
||
| 453 | |||
| 454 | return $this; |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Devolver las acciones que se muestran en un menu |
||
| 459 | * |
||
| 460 | * @return DataGridActionInterface[] |
||
| 461 | */ |
||
| 462 | public function getDataActionsMenu() |
||
| 463 | { |
||
| 464 | return $this->actionsMenu; |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Devolver las acciones filtradas |
||
| 469 | * |
||
| 470 | * @param $filter |
||
| 471 | * |
||
| 472 | * @return DataGridActionInterface[] |
||
| 473 | */ |
||
| 474 | public function getDataActionsFiltered($filter) |
||
| 475 | { |
||
| 476 | $actions = []; |
||
| 477 | |||
| 478 | foreach ($this->actions as $action) { |
||
| 479 | if ($action->getRuntimeFilter()($filter)) { |
||
| 480 | $actions[] = $action; |
||
| 481 | } |
||
| 482 | } |
||
| 483 | |||
| 484 | return $actions; |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Devolver las acciones de menu filtradas |
||
| 489 | * |
||
| 490 | * @param $filter |
||
| 491 | * |
||
| 492 | * @return DataGridActionInterface[] |
||
| 493 | */ |
||
| 494 | public function getDataActionsMenuFiltered($filter) |
||
| 495 | { |
||
| 496 | $actions = []; |
||
| 497 | |||
| 498 | foreach ($this->actionsMenu as $action) { |
||
| 499 | if ($action->getRuntimeFilter()($filter)) { |
||
| 500 | $actions[] = $action; |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | return $actions; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | public function getDataTableTemplate() |
||
| 511 | { |
||
| 512 | return $this->tableTemplate; |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param $template |
||
| 517 | * @param null $base |
||
| 518 | * |
||
| 519 | * @return DataGridBase |
||
| 520 | */ |
||
| 521 | public function setDataTableTemplate($template, $base = null) |
||
| 522 | { |
||
| 523 | try { |
||
| 524 | $this->tableTemplate = $this->checkTemplate($template, $base); |
||
| 525 | } catch (FileNotFoundException $e) { |
||
| 526 | processException($e); |
||
| 527 | } |
||
| 528 | |||
| 529 | return $this; |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @return int |
||
| 534 | */ |
||
| 535 | public function getDataActionsMenuCount() |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @return int |
||
| 542 | */ |
||
| 543 | public function getDataActionsCount() |
||
| 544 | { |
||
| 545 | return $this->actionsCount; |
||
| 546 | } |
||
| 547 | } |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.