| Conditions | 17 |
| Paths | 27 |
| Total Lines | 173 |
| Code Lines | 104 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 152 | function handleTask(int $processId, array $ProcessArguments, array $SETTINGS, int $itemId = null): bool |
||
| 153 | { |
||
| 154 | provideLog('[PROCESS][#'. $processId.'][START]', $SETTINGS); |
||
| 155 | $task_to_perform = DB::queryfirstrow( |
||
| 156 | 'SELECT * |
||
| 157 | FROM ' . prefixTable('processes_tasks') . ' |
||
| 158 | WHERE process_id = %i AND finished_at IS NULL |
||
| 159 | ORDER BY increment_id ASC', |
||
| 160 | $processId |
||
| 161 | ); |
||
| 162 | |||
| 163 | // get the process object |
||
| 164 | $processObject = json_decode($ProcessArguments['object_key'], true); |
||
| 165 | |||
| 166 | if (DB::count() > 0) { |
||
| 167 | // check if a linux process is not currently on going |
||
| 168 | // if sub_task_in_progress === 1 then exit |
||
| 169 | if ((int) $task_to_perform['sub_task_in_progress'] === 0) { |
||
| 170 | provideLog('[TASK][#'. $task_to_perform['increment_id'].'][START]', $SETTINGS); |
||
| 171 | |||
| 172 | // handle next task |
||
| 173 | $args = json_decode($task_to_perform['task'], true); |
||
| 174 | //print_r($args);return false; |
||
| 175 | |||
| 176 | // flag as in progress |
||
| 177 | DB::update( |
||
| 178 | prefixTable('processes'), |
||
| 179 | array( |
||
| 180 | 'updated_at' => time(), |
||
| 181 | 'is_in_progress' => 1, |
||
| 182 | ), |
||
| 183 | 'increment_id = %i', |
||
| 184 | $processId |
||
| 185 | ); |
||
| 186 | |||
| 187 | // flag task as on going |
||
| 188 | if ((int) $args['index'] === 0) { |
||
| 189 | DB::update( |
||
| 190 | prefixTable('processes_tasks'), |
||
| 191 | array( |
||
| 192 | 'is_in_progress' => 1, |
||
| 193 | ), |
||
| 194 | 'increment_id = %i', |
||
| 195 | $task_to_perform['increment_id'] |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | |||
| 199 | // flag sub task in progress as on going |
||
| 200 | DB::update( |
||
| 201 | prefixTable('processes_tasks'), |
||
| 202 | array( |
||
| 203 | 'sub_task_in_progress' => 1, |
||
| 204 | ), |
||
| 205 | 'increment_id = %i', |
||
| 206 | $task_to_perform['increment_id'] |
||
| 207 | ); |
||
| 208 | |||
| 209 | // perform the task step "create_users_files_key" |
||
| 210 | if ($args['step'] === 'create_users_files_key') { |
||
| 211 | storeUsersShareKey( |
||
| 212 | prefixTable('sharekeys_files'), |
||
| 213 | 0, |
||
| 214 | -1, |
||
| 215 | (int) $ProcessArguments['item_id'], |
||
| 216 | '', |
||
| 217 | false, |
||
| 218 | false, |
||
| 219 | $processObject['files'], |
||
| 220 | array_key_exists('all_users_except_id', $ProcessArguments) === true ? $ProcessArguments['all_users_except_id'] : -1, |
||
| 221 | (int) in_array('parent_id', $ProcessArguments) === true ? $ProcessArguments['parent_id'] : -1 |
||
| 222 | ); |
||
| 223 | } elseif ($args['step'] === 'create_users_fields_key') { |
||
| 224 | storeUsersShareKey( |
||
| 225 | prefixTable('sharekeys_fields'), |
||
| 226 | 0, |
||
| 227 | -1, |
||
| 228 | (int) $ProcessArguments['item_id'], |
||
| 229 | (string) $ProcessArguments['pwd'], |
||
| 230 | false, |
||
| 231 | false, |
||
| 232 | [], |
||
| 233 | array_key_exists('all_users_except_id', $ProcessArguments) === true ? $ProcessArguments['all_users_except_id'] : -1, |
||
| 234 | (int) in_array('parent_id', $ProcessArguments) === true ? $ProcessArguments['parent_id'] : -1 |
||
| 235 | ); |
||
| 236 | } elseif ($args['step'] === 'create_users_pwd_key') { |
||
| 237 | //error_log('create_users_pwd_key - '. print_r($ProcessArguments, true)." - inArray: ".array_key_exists('all_users_except_id', $ProcessArguments)); |
||
| 238 | storeUsersShareKey( |
||
| 239 | prefixTable('sharekeys_items'), |
||
| 240 | 0, |
||
| 241 | -1, |
||
| 242 | (int) $ProcessArguments['item_id'], |
||
| 243 | (string) $ProcessArguments['pwd'], |
||
| 244 | false, |
||
| 245 | false, |
||
| 246 | [], |
||
| 247 | array_key_exists('all_users_except_id', $ProcessArguments) === true ? $ProcessArguments['all_users_except_id'] : -1 |
||
| 248 | ); |
||
| 249 | } |
||
| 250 | |||
| 251 | // update the task status |
||
| 252 | DB::update( |
||
| 253 | prefixTable('processes_tasks'), |
||
| 254 | array( |
||
| 255 | 'sub_task_in_progress' => 0, // flag sub task is no more in prgoress |
||
| 256 | 'task' => json_encode(["status" => "Done"]), |
||
| 257 | 'is_in_progress' => -1, |
||
| 258 | 'finished_at' => time(), |
||
| 259 | 'updated_at' => time(), |
||
| 260 | ), |
||
| 261 | 'increment_id = %i', |
||
| 262 | $task_to_perform['increment_id'] |
||
| 263 | ); |
||
| 264 | |||
| 265 | provideLog('[TASK]['.$args['step'].'] starting at '.$args['index'].' is done.', $SETTINGS); |
||
| 266 | |||
| 267 | // are all tasks done? |
||
| 268 | $tasks = DB::query( |
||
| 269 | 'SELECT * |
||
| 270 | FROM ' . prefixTable('processes_tasks') . ' |
||
| 271 | WHERE process_id = %i AND finished_at IS NULL', |
||
| 272 | $processId |
||
| 273 | ); |
||
| 274 | if (DB::count() === 0) { |
||
| 275 | // all tasks are done |
||
| 276 | provideLog('[PROCESS]['.$processId.'][FINISHED]', $SETTINGS); |
||
| 277 | DB::debugmode(false); |
||
| 278 | DB::update( |
||
| 279 | prefixTable('processes'), |
||
| 280 | array( |
||
| 281 | 'finished_at' => time(), |
||
| 282 | 'is_in_progress' => -1, |
||
| 283 | 'arguments' => json_encode([ |
||
| 284 | 'new_user_id' => isset($ProcessArguments['new_user_id']) === true ? $ProcessArguments['new_user_id'] : '', |
||
| 285 | ]) |
||
| 286 | ), |
||
| 287 | 'increment_id = %i', |
||
| 288 | $processId |
||
| 289 | ); |
||
| 290 | |||
| 291 | // if item was being updated then remove the edition lock |
||
| 292 | if (is_null($itemId) === false) { |
||
| 293 | DB::delete(prefixTable('items_edition'), 'item_id = %i', $itemId); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | return false; |
||
| 297 | |||
| 298 | } else { |
||
| 299 | // Task is currently being in progress by another server process |
||
| 300 | provideLog('[TASK][#'. $task_to_perform['increment_id'].'][WARNING] Similar task already being processes', $SETTINGS); |
||
| 301 | return false; |
||
| 302 | } |
||
| 303 | } else { |
||
| 304 | // no more task to perform |
||
| 305 | provideLog('[PROCESS]['.$processId.'][FINISHED]', $SETTINGS); |
||
| 306 | DB::update( |
||
| 307 | prefixTable('processes'), |
||
| 308 | array( |
||
| 309 | 'finished_at' => time(), |
||
| 310 | 'is_in_progress' => -1, |
||
| 311 | 'arguments' => json_encode([ |
||
| 312 | 'item_id' => isset($ProcessArguments['item_id']) === true ? $ProcessArguments['item_id'] : '', |
||
| 313 | ]) |
||
| 314 | ), |
||
| 315 | 'increment_id = %i', |
||
| 316 | $processId |
||
| 317 | ); |
||
| 318 | |||
| 319 | // if item was being updated then remove the edition lock |
||
| 320 | if (is_null($itemId) === false) { |
||
| 321 | DB::delete(prefixTable('items_edition'), 'item_id = %i', $itemId); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | return false; |
||
| 325 | } |
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: