| Conditions | 8 |
| Paths | 12 |
| Total Lines | 58 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 45 | public function get(Server $server) |
||
| 46 | { |
||
| 47 | /** @var User $currentUser */ |
||
| 48 | $currentUser = $this->authFactory->guard()->user(); |
||
| 49 | |||
| 50 | $this->authorize('server-control', $server); |
||
| 51 | $this->authorize('server-settings', $server); |
||
| 52 | |||
| 53 | $isAdmin = $currentUser->can(PermissionHelper::ADMIN_PERMISSIONS); |
||
| 54 | |||
| 55 | $settings = [ |
||
| 56 | $server::AUTOSTART_SETTING_KEY => [ |
||
| 57 | 'name' => $server::AUTOSTART_SETTING_KEY, |
||
| 58 | 'value' => $server->getSetting($server::AUTOSTART_SETTING_KEY)->value, |
||
| 59 | 'type' => 'bool', |
||
| 60 | 'label' => __('servers.autostart_setting'), |
||
| 61 | ], |
||
| 62 | $server::UPDATE_BEFORE_START_SETTING_KEY => [ |
||
| 63 | 'name' => $server::UPDATE_BEFORE_START_SETTING_KEY, |
||
| 64 | 'value' => $server->getSetting($server::UPDATE_BEFORE_START_SETTING_KEY)->value, |
||
| 65 | 'type' => 'bool', |
||
| 66 | 'label' => __('servers.update_before_start_setting'), |
||
| 67 | ], |
||
| 68 | ]; |
||
| 69 | |||
| 70 | foreach($server->gameMod->vars as $var) { |
||
| 71 | if (!empty($var['admin_var']) && !$isAdmin) { |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | |||
| 75 | $settings[$var['var']] = [ |
||
| 76 | 'name' => $var['var'], |
||
| 77 | 'value' => $var['default'], |
||
| 78 | 'type' => $var['type'] ?? 'string', |
||
| 79 | 'label' => $var['info'] ?? $var['var'], |
||
| 80 | ]; |
||
| 81 | } |
||
| 82 | |||
| 83 | foreach($server->settings as $setting) { |
||
| 84 | if (!isset($settings[$setting->name])) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | |||
| 88 | if (!empty($settings[$setting->name]['admin_var']) && !$isAdmin) { |
||
| 89 | continue; |
||
| 90 | } |
||
| 91 | |||
| 92 | $settings[$setting->name] = [ |
||
| 93 | 'name' => $setting->name, |
||
| 94 | 'value' => $setting->value, |
||
| 95 | 'label' => $settings[$setting->name]['label'] ?? $setting->name, |
||
| 96 | 'type' => $settings[$setting->name]['type'] ?? 'string', |
||
| 97 | ]; |
||
| 98 | } |
||
| 99 | |||
| 100 | unset($settings[$server::AUTOSTART_CURRENT_SETTING_KEY]); |
||
| 101 | |||
| 102 | return array_values($settings); |
||
| 103 | } |
||
| 167 | } |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: