Complex classes like Control 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 Control, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Control extends ViewAbstract |
||
22 | { |
||
23 | /** |
||
24 | * options of the panel |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | |||
29 | protected $_optionArray = |
||
30 | [ |
||
31 | 'className' => |
||
32 | [ |
||
33 | 12 | 'list' => 'rs-admin-list-control', |
|
34 | 'item' => |
||
35 | 12 | [ |
|
36 | 12 | 'control' => 'rs-admin-item-control', |
|
37 | 12 | 'disable' => 'rs-admin-item-disable', |
|
38 | 12 | 'enable' => 'rs-admin-item-enable', |
|
39 | 'future-posting' => 'rs-admin-item-future-posting', |
||
40 | 'unpublish' => 'rs-admin-item-unpublish', |
||
41 | 'publish' => 'rs-admin-item-publish', |
||
42 | 12 | 'edit' => 'rs-admin-item-edit', |
|
43 | 'delete' => 'rs-admin-item-delete', |
||
44 | 12 | 'install' => 'rs-admin-item-install', |
|
45 | 12 | 'uninstall' => 'rs-admin-item-uninstall' |
|
46 | ], |
||
47 | 12 | 'link' => |
|
48 | [ |
||
49 | 'delete' => 'rs-admin-js-delete', |
||
50 | 12 | 'uninstall' => 'rs-admin-js-uninstall' |
|
51 | 12 | ] |
|
52 | ] |
||
53 | 12 | ]; |
|
54 | |||
55 | /** |
||
56 | 12 | * init the class |
|
57 | 12 | * |
|
58 | * @since 4.0.0 |
||
59 | 12 | * |
|
60 | 12 | * @param array $optionArray options of the panel |
|
61 | */ |
||
62 | |||
63 | public function init(array $optionArray = []) |
||
67 | |||
68 | 2 | /** |
|
69 | 2 | * render the view |
|
70 | 2 | * |
|
71 | * @since 4.0.0 |
||
72 | 2 | * |
|
73 | 2 | * @param string $table name of the table |
|
74 | 2 | * @param int $id identifier of the item |
|
75 | * @param string $alias alias of the item |
||
76 | * @param int $status status of the item |
||
77 | * |
||
78 | * @return string|null |
||
79 | */ |
||
80 | 12 | ||
81 | public function render(string $table = null, int $id = null, string $alias = null, int $status = null) : ?string |
||
227 | |||
228 | /** |
||
229 | * has the permission |
||
230 | * |
||
231 | 9 | * @since 4.0.0 |
|
232 | * |
||
233 | 1 | * @param string $table name of the table |
|
234 | * @param string $type |
||
235 | 9 | * |
|
236 | 9 | * @return bool |
|
237 | 9 | */ |
|
238 | 8 | ||
239 | 7 | protected function _hasPermission(string $table = null, string $type = null) : bool |
|
243 | |||
244 | /** |
||
245 | * show the action |
||
246 | * |
||
247 | * @since 4.0.0 |
||
248 | * |
||
249 | * @param string $table name of the table |
||
250 | * @param string $type |
||
251 | * @param int $id |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | |||
256 | protected function _showAction(string $table = null, string $type = null, int $id = null) : bool |
||
291 | } |
||
292 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.