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:
Complex classes like AbstractTableController 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AbstractTableController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | abstract class AbstractTableController |
||
| 25 | { |
||
| 26 | use ValidatesRequests; |
||
| 27 | |||
| 28 | protected $viewCrudList = 'jarboe::crud.list'; |
||
| 29 | protected $viewCrudCreate = 'jarboe::crud.create'; |
||
| 30 | protected $viewCrudEdit = 'jarboe::crud.edit'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Permission group name. |
||
| 34 | * |
||
| 35 | * @var string|array |
||
| 36 | * array( |
||
| 37 | * 'list' => 'permission:list', |
||
| 38 | * 'search' => 'permission:search', |
||
| 39 | * 'create' => 'permission:create', |
||
| 40 | * 'store' => 'permission:store', |
||
| 41 | * 'edit' => 'permission:edit', |
||
| 42 | * 'update' => 'permission:update', |
||
| 43 | * 'inline' => 'permission:inline', |
||
| 44 | * 'delete' => 'permission:delete', |
||
| 45 | * 'restore' => 'permission:restore', |
||
| 46 | * 'forceDelete' => 'permission:force-delete', |
||
| 47 | * ) |
||
| 48 | */ |
||
| 49 | protected $permissions = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var CRUD |
||
| 53 | */ |
||
| 54 | protected $crud; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * ID of manipulated model. |
||
| 58 | * |
||
| 59 | * @var mixed |
||
| 60 | */ |
||
| 61 | protected $idEntity; |
||
| 62 | |||
| 63 | 17 | public function __construct() |
|
| 76 | |||
| 77 | protected function crud(): CRUD |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Handle search action. |
||
| 84 | * |
||
| 85 | * @param Request $request |
||
| 86 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 87 | * @throws UnauthorizedException |
||
| 88 | */ |
||
| 89 | 1 | public function handleSearch(Request $request) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Handle relation search action. |
||
| 105 | * Currently used for SelectField with type `select2` and `ajax = true`. |
||
| 106 | * |
||
| 107 | * @param string $field |
||
|
|
|||
| 108 | * @param string $page |
||
| 109 | * @param string $term |
||
| 110 | * @return \Illuminate\Http\JsonResponse |
||
| 111 | */ |
||
| 112 | public function searchRelation(Request $request) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Save direction by column. |
||
| 168 | * |
||
| 169 | * @param $column |
||
| 170 | * @param $direction |
||
| 171 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 172 | */ |
||
| 173 | 1 | public function orderBy($column, $direction) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Handle store action. |
||
| 182 | * |
||
| 183 | * @param Request $request |
||
| 184 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 185 | * @throws PermissionDenied |
||
| 186 | * @throws UnauthorizedException |
||
| 187 | */ |
||
| 188 | 1 | public function handleStore(Request $request) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Handle inline update action. |
||
| 209 | * |
||
| 210 | * @param Request $request |
||
| 211 | * @return \Illuminate\Http\JsonResponse |
||
| 212 | * @throws PermissionDenied |
||
| 213 | * @throws \ReflectionException |
||
| 214 | */ |
||
| 215 | 1 | public function handleInline(Request $request) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Get validation data for inline field. |
||
| 262 | * |
||
| 263 | * @param Request $request |
||
| 264 | * @param $name |
||
| 265 | * @return array |
||
| 266 | * @throws \ReflectionException |
||
| 267 | */ |
||
| 268 | protected function getValidationDataForInlineField(Request $request, $name) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Handle update action. |
||
| 301 | * |
||
| 302 | * @param Request $request |
||
| 303 | * @param $id |
||
| 304 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 305 | * @throws PermissionDenied |
||
| 306 | * @throws UnauthorizedException |
||
| 307 | */ |
||
| 308 | 1 | public function handleUpdate(Request $request, $id) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Handle delete action. |
||
| 330 | * |
||
| 331 | * @param Request $request |
||
| 332 | * @param $id |
||
| 333 | * @return \Illuminate\Http\JsonResponse |
||
| 334 | * @throws PermissionDenied |
||
| 335 | * @throws UnauthorizedException |
||
| 336 | */ |
||
| 337 | 1 | public function handleDelete(Request $request, $id) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Handle setting per page param. |
||
| 374 | * |
||
| 375 | * @param $perPage |
||
| 376 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 377 | */ |
||
| 378 | 1 | public function perPage($perPage) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Show table list page. |
||
| 390 | * |
||
| 391 | * @param Request $request |
||
| 392 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 393 | * @throws UnauthorizedException |
||
| 394 | */ |
||
| 395 | 1 | View Code Duplication | public function handleList(Request $request) |
| 411 | |||
| 412 | /** |
||
| 413 | * Get array of view's objects, that should be rendered above content of `list` view. |
||
| 414 | * |
||
| 415 | * @return array |
||
| 416 | */ |
||
| 417 | 2 | protected function getListViewsAbove(): array |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Get array of view's objects, that should be rendered below content of `list` view. |
||
| 424 | * |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | 2 | protected function getListViewsBelow(): array |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Show edit form page. |
||
| 434 | * |
||
| 435 | * @param Request $request |
||
| 436 | * @param $id |
||
| 437 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 438 | * @throws PermissionDenied |
||
| 439 | * @throws UnauthorizedException |
||
| 440 | */ |
||
| 441 | 1 | public function handleEdit(Request $request, $id) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Get array of view's objects, that should be rendered above content of `edit` view. |
||
| 467 | * |
||
| 468 | * @return array |
||
| 469 | */ |
||
| 470 | 2 | protected function getEditViewsAbove(): array |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Get array of view's objects, that should be rendered below content of `edit` view. |
||
| 477 | * |
||
| 478 | * @return array |
||
| 479 | */ |
||
| 480 | 2 | protected function getEditViewsBelow(): array |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Show create form page. |
||
| 487 | * |
||
| 488 | * @param Request $request |
||
| 489 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 490 | * @throws PermissionDenied |
||
| 491 | * @throws UnauthorizedException |
||
| 492 | */ |
||
| 493 | 1 | View Code Duplication | public function handleCreate(Request $request) |
| 512 | |||
| 513 | /** |
||
| 514 | * Get array of view's objects, that should be rendered above content of `create` view. |
||
| 515 | * |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | 2 | protected function getCreateViewsAbove(): array |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Get array of view's objects, that should be rendered below content of `create` view. |
||
| 525 | * |
||
| 526 | * @return array |
||
| 527 | */ |
||
| 528 | 2 | protected function getCreateViewsBelow(): array |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Handle toolbar's tool request. |
||
| 535 | * |
||
| 536 | * @param Request $request |
||
| 537 | * @param $identifier |
||
| 538 | * @return mixed |
||
| 539 | * @throws PermissionDenied |
||
| 540 | */ |
||
| 541 | public function toolbar(Request $request, $identifier) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Switch table order for making sortable table. |
||
| 557 | * |
||
| 558 | * @return \Illuminate\Http\RedirectResponse |
||
| 559 | * @throws PermissionDenied |
||
| 560 | */ |
||
| 561 | public function switchSortable() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Change sort weight of dragged row. |
||
| 578 | * |
||
| 579 | * @param $id |
||
| 580 | * @param Request $request |
||
| 581 | * @return \Illuminate\Http\JsonResponse |
||
| 582 | * @throws PermissionDenied |
||
| 583 | */ |
||
| 584 | public function moveItem($id, Request $request) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Restore record by its id. |
||
| 602 | * |
||
| 603 | * @param $id |
||
| 604 | * @param Request $request |
||
| 605 | * @return \Illuminate\Http\JsonResponse |
||
| 606 | * @throws PermissionDenied |
||
| 607 | * @throws UnauthorizedException |
||
| 608 | */ |
||
| 609 | 1 | View Code Duplication | public function handleRestore(Request $request, $id) |
| 635 | |||
| 636 | /** |
||
| 637 | * Force delete record by its id. |
||
| 638 | * |
||
| 639 | * @param $id |
||
| 640 | * @param Request $request |
||
| 641 | * @return \Illuminate\Http\JsonResponse |
||
| 642 | * @throws PermissionDenied |
||
| 643 | * @throws UnauthorizedException |
||
| 644 | */ |
||
| 645 | 1 | View Code Duplication | public function handleForceDelete(Request $request, $id) |
| 669 | |||
| 670 | /* |
||
| 671 | |-------------------------------------------------------------------------- |
||
| 672 | | Helpers |
||
| 673 | |-------------------------------------------------------------------------- |
||
| 674 | */ |
||
| 675 | 1 | protected function addTools(array $tools) |
|
| 681 | |||
| 682 | 2 | protected function addTool(ToolInterface $tool) |
|
| 687 | |||
| 688 | /** |
||
| 689 | * @param string|AbstractField $column |
||
| 690 | */ |
||
| 691 | protected function addColumn($column) |
||
| 695 | |||
| 696 | protected function addColumns(array $columns) |
||
| 702 | |||
| 703 | 13 | protected function addField(AbstractField $field) |
|
| 707 | |||
| 708 | 13 | protected function addFields(array $fields) |
|
| 714 | |||
| 715 | protected function addTab($title, array $fields) |
||
| 722 | |||
| 723 | 13 | protected function setModel($model) |
|
| 727 | |||
| 728 | protected function paginate($perPage) |
||
| 732 | |||
| 733 | protected function order(string $column, string $direction = 'asc') |
||
| 737 | |||
| 738 | 13 | protected function filter(\Closure $callback) |
|
| 742 | |||
| 743 | protected function action($ident) |
||
| 747 | |||
| 748 | protected function removeAction($ident) |
||
| 752 | |||
| 753 | public function enableBatchCheckboxes(bool $enabled = true) |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Enable soft deletes for table. |
||
| 760 | * |
||
| 761 | * @param bool $enabled |
||
| 762 | */ |
||
| 763 | 13 | public function softDeletes(bool $enabled = true) |
|
| 767 | |||
| 768 | /** |
||
| 769 | * Allows to reorder table rows. |
||
| 770 | * |
||
| 771 | * @param string $field |
||
| 772 | */ |
||
| 773 | public function sortable(string $field) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Add row action button with optional changing order. |
||
| 780 | * |
||
| 781 | * @param AbstractAction $action |
||
| 782 | * @param null|string $moveDirection Move action 'before' or 'after' $baseActionIdent |
||
| 783 | * @param null|string $baseActionIdent |
||
| 784 | */ |
||
| 785 | protected function addAction(AbstractAction $action, $moveDirection = null, $baseActionIdent = null) |
||
| 796 | |||
| 797 | protected function addActions(array $actions) |
||
| 801 | |||
| 802 | protected function setActions(array $actions = []) |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Check if user has permission for the action. |
||
| 809 | * |
||
| 810 | * @param $action |
||
| 811 | * @return bool |
||
| 812 | */ |
||
| 813 | 11 | protected function can($action): bool |
|
| 830 | |||
| 831 | /** |
||
| 832 | * Get admin user object. |
||
| 833 | * |
||
| 834 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
| 835 | */ |
||
| 836 | protected function admin() |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Add locales for all translatable fields. |
||
| 843 | * |
||
| 844 | * @param array $locales |
||
| 845 | */ |
||
| 846 | protected function locales(array $locales) |
||
| 850 | |||
| 851 | 10 | public function __call($name, $arguments) |
|
| 852 | { |
||
| 853 | /** @var Request $request */ |
||
| 854 | 10 | $request = RequestFacade::instance(); |
|
| 855 | |||
| 856 | 10 | $id = null; |
|
| 857 | 10 | if (isset($arguments[0])) { |
|
| 858 | 9 | $id = $arguments[1] ?? $arguments[0]; |
|
| 859 | } |
||
| 860 | |||
| 861 | try { |
||
| 862 | 10 | switch ($name) { |
|
| 863 | 10 | case 'list': |
|
| 864 | 1 | return $this->handleList($request); |
|
| 865 | 9 | case 'search': |
|
| 866 | 1 | return $this->handleSearch($request); |
|
| 867 | 8 | case 'create': |
|
| 868 | 1 | return $this->handleCreate($request); |
|
| 869 | 7 | case 'store': |
|
| 870 | 1 | return $this->handleStore($request); |
|
| 871 | 6 | case 'edit': |
|
| 872 | 1 | return $this->handleEdit($request, $id); |
|
| 873 | 5 | case 'update': |
|
| 874 | 1 | return $this->handleUpdate($request, $id); |
|
| 875 | 4 | case 'delete': |
|
| 876 | 1 | return $this->handleDelete($request, $id); |
|
| 877 | 3 | case 'restore': |
|
| 878 | 1 | return $this->handleRestore($request, $id); |
|
| 879 | 2 | case 'forceDelete': |
|
| 880 | 1 | return $this->handleForceDelete($request, $id); |
|
| 881 | 1 | case 'inline': |
|
| 882 | 1 | return $this->handleInline($request); |
|
| 883 | |||
| 884 | default: |
||
| 885 | throw new \RuntimeException('Invalid method ' . $name); |
||
| 886 | } |
||
| 887 | } catch (ValidationException $e) { |
||
| 888 | throw $e; |
||
| 889 | } catch (UnauthorizedException $e) { |
||
| 890 | return $this->createUnauthorizedResponse($request, $e); |
||
| 891 | } catch (\Exception $e) { |
||
| 892 | $this->notifyBigDanger(get_class($e), $e->getMessage(), 0); |
||
| 893 | return redirect()->back()->withInput($request->input()); |
||
| 894 | } |
||
| 895 | } |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Bound fields/tools/etc with global data. |
||
| 899 | */ |
||
| 900 | 13 | protected function bound() |
|
| 901 | { |
||
| 902 | /** @var AbstractField $field */ |
||
| 903 | 13 | foreach ($this->crud()->getAllFieldObjects() as $field) { |
|
| 904 | 13 | $field->prepare($this->crud); |
|
| 905 | } |
||
| 906 | |||
| 907 | /** @var AbstractField $field */ |
||
| 908 | 13 | foreach ($this->crud()->getFieldsWithoutMarkup() as $field) { |
|
| 909 | 13 | if ($field->isTranslatable()) { |
|
| 910 | $this->addTool(new TranslationLocalesSelectorTool()); |
||
| 911 | break; |
||
| 912 | } |
||
| 913 | } |
||
| 914 | |||
| 915 | 13 | $this->crud()->actions()->setCrud($this->crud()); |
|
| 916 | 13 | } |
|
| 917 | |||
| 918 | /** |
||
| 919 | * Create response for unauthorized request. |
||
| 920 | * |
||
| 921 | * @param Request $request |
||
| 922 | * @param UnauthorizedException $exception |
||
| 923 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
| 924 | */ |
||
| 925 | protected function createUnauthorizedResponse(Request $request, UnauthorizedException $exception) |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Get model for current request. |
||
| 938 | * |
||
| 939 | * @return string |
||
| 940 | * @throws \RuntimeException |
||
| 941 | */ |
||
| 942 | protected function model() |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Add notification. |
||
| 953 | * |
||
| 954 | * @param string $title |
||
| 955 | * @param string|null $content |
||
| 956 | * @param int $timeout |
||
| 957 | * @param string|null $color |
||
| 958 | * @param string|null $icon |
||
| 959 | * @param string $type |
||
| 960 | */ |
||
| 961 | 1 | protected function notify(string $title, string $content = null, int $timeout = 4000, string $color = null, string $icon = null, string $type = 'small') |
|
| 976 | |||
| 977 | protected function notifySmall(string $title, string $content = null, int $timeout = 4000, string $color = null, string $icon = null) |
||
| 981 | |||
| 982 | protected function notifyBig(string $title, string $content = null, int $timeout = 4000, string $color = null, string $icon = null) |
||
| 986 | |||
| 987 | 1 | protected function notifySmallSuccess(string $title, string $content = null, int $timeout = 4000) |
|
| 991 | |||
| 992 | 1 | protected function notifySmallDanger(string $title, string $content = null, int $timeout = 4000) |
|
| 996 | |||
| 997 | 1 | protected function notifySmallWarning(string $title, string $content = null, int $timeout = 4000) |
|
| 1001 | |||
| 1002 | 1 | protected function notifySmallInfo(string $title, string $content = null, int $timeout = 4000) |
|
| 1006 | |||
| 1007 | 1 | protected function notifyBigSuccess(string $title, string $content = null, int $timeout = 4000) |
|
| 1011 | |||
| 1012 | 1 | protected function notifyBigDanger(string $title, string $content = null, int $timeout = 4000) |
|
| 1016 | |||
| 1017 | 1 | protected function notifyBigWarning(string $title, string $content = null, int $timeout = 4000) |
|
| 1021 | |||
| 1022 | 1 | protected function notifyBigInfo(string $title, string $content = null, int $timeout = 4000) |
|
| 1026 | |||
| 1027 | /* |
||
| 1028 | |-------------------------------------------------------------------------- |
||
| 1029 | | Abstract |
||
| 1030 | |-------------------------------------------------------------------------- |
||
| 1031 | */ |
||
| 1032 | |||
| 1033 | abstract protected function init(); |
||
| 1034 | } |
||
| 1035 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.