Conditions | 9 |
Paths | 24 |
Total Lines | 61 |
Code Lines | 36 |
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 |
||
105 | public function save(Server $server, ServerSettingSaveRequest $request) |
||
106 | { |
||
107 | /** @var User $currentUser */ |
||
108 | $currentUser = $this->authFactory->guard()->user(); |
||
109 | |||
110 | $this->authorize('server-control', $server); |
||
111 | $this->authorize('server-settings', $server); |
||
112 | |||
113 | $isAdmin = $currentUser->can(PermissionHelper::ADMIN_PERMISSIONS); |
||
114 | |||
115 | $settingsInput = $request->all(); |
||
116 | |||
117 | $settings = [ |
||
118 | $server::AUTOSTART_SETTING_KEY => [ |
||
119 | 'name' => $server::AUTOSTART_SETTING_KEY, |
||
120 | 'value' => $server->getSetting($server::AUTOSTART_SETTING_KEY)->value, |
||
121 | 'type' => 'bool', |
||
122 | 'label' => __('servers.autostart_setting'), |
||
123 | ], |
||
124 | $server::UPDATE_BEFORE_START_SETTING_KEY => [ |
||
125 | 'name' => $server::UPDATE_BEFORE_START_SETTING_KEY, |
||
126 | 'value' => $server->getSetting($server::UPDATE_BEFORE_START_SETTING_KEY)->value, |
||
127 | 'type' => 'bool', |
||
128 | 'label' => __('servers.update_before_start_setting'), |
||
129 | ], |
||
130 | ]; |
||
131 | |||
132 | foreach($server->gameMod->vars as $var) { |
||
133 | if (!empty($var['admin_var']) && !$isAdmin) { |
||
134 | continue; |
||
135 | } |
||
136 | |||
137 | $settings[$var['var']] = [ |
||
138 | 'name' => $var['var'], |
||
139 | 'value' => $var['default'], |
||
140 | 'type' => $var['type'] ?? 'string', |
||
141 | 'label' => $var['info'] ?? $var['var'], |
||
142 | ]; |
||
143 | } |
||
144 | |||
145 | /** @var ServerSettingSaveRequest[][] $settingsInputMap */ |
||
146 | $settingsInputMap = []; |
||
147 | foreach($settingsInput as $setting) { |
||
148 | $settingsInputMap[$setting['name']] = $setting['value']; |
||
149 | } |
||
150 | |||
151 | foreach($settings as $setting) { |
||
152 | if (!isset($settingsInputMap[$setting['name']])) { |
||
153 | continue; |
||
154 | } |
||
155 | |||
156 | if (!empty($setting['admin_var']) && !$isAdmin) { |
||
157 | continue; |
||
158 | } |
||
159 | |||
160 | $value = $server->getSetting($setting['name']); |
||
161 | $value->value = $settingsInputMap[$setting['name']]; |
||
162 | $value->save(); |
||
163 | } |
||
164 | |||
165 | $this->repository->save($server); |
||
166 | } |
||
167 | } |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: