| Conditions | 7 |
| Paths | 20 |
| Total Lines | 51 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 61 | public function activation(?Domain $domain, Module $module, Request $request) |
||
| 62 | { |
||
| 63 | // Pre-process |
||
| 64 | $this->preProcess($domain, $module, $request); |
||
| 65 | |||
| 66 | // Activate or deactivate a module on the current domain |
||
| 67 | $success = false; |
||
| 68 | $error = ''; |
||
| 69 | if (request('src_module')) { |
||
| 70 | $sourceModule = ucmodule(request('src_module')); |
||
| 71 | $isActive = request('active') == 1; |
||
| 72 | |||
| 73 | if ($sourceModule) { |
||
| 74 | // Activate the module on the current domain |
||
| 75 | if ($isActive === true) { |
||
| 76 | $domain->modules()->attach($sourceModule); |
||
| 77 | $success = true; |
||
| 78 | $message = 'message.module_activated'; |
||
| 79 | } |
||
| 80 | // Deactivate the module on the current domain only if it is not mandatory |
||
| 81 | elseif (!$sourceModule->isMandatory()) { |
||
| 82 | $domain->modules()->detach($sourceModule); |
||
| 83 | $success = true; |
||
| 84 | $message = 'message.module_deactivated'; |
||
| 85 | } |
||
| 86 | // Impossible to deactivate a mandatory module |
||
| 87 | else { |
||
| 88 | $error = 'error.module_is_mandatory'; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | // Module name is not defined |
||
| 93 | else { |
||
| 94 | $error = 'error.module_not_defined'; |
||
| 95 | } |
||
| 96 | |||
| 97 | $result = [ |
||
| 98 | 'success' => $success |
||
| 99 | ]; |
||
| 100 | |||
| 101 | // Add message if defined |
||
| 102 | if (!empty($message)) { |
||
| 103 | $result['message'] = uctrans($message, $module); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Add error if defined |
||
| 107 | if (!empty($error)) { |
||
| 108 | $result['error'] = uctrans($error, $module); |
||
| 109 | } |
||
| 110 | |||
| 111 | return $result; |
||
| 112 | } |
||
| 113 | } |