| Conditions | 9 |
| Paths | 9 |
| Total Lines | 132 |
| Code Lines | 77 |
| 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 |
||
| 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 | // handle next task |
||
| 171 | $args = json_decode($task_to_perform['task'], true); |
||
| 172 | provideLog('[TASK][#'. $task_to_perform['increment_id'].'][START]Task '.$args['step'], $SETTINGS); |
||
| 173 | |||
| 174 | // flag as in progress |
||
| 175 | DB::update( |
||
| 176 | prefixTable('processes'), |
||
| 177 | array( |
||
| 178 | 'updated_at' => time(), |
||
| 179 | 'is_in_progress' => 1, |
||
| 180 | ), |
||
| 181 | 'increment_id = %i', |
||
| 182 | $processId |
||
| 183 | ); |
||
| 184 | |||
| 185 | // flag task as on going |
||
| 186 | if ((int) $args['index'] === 0) { |
||
| 187 | DB::update( |
||
| 188 | prefixTable('processes_tasks'), |
||
| 189 | array( |
||
| 190 | 'is_in_progress' => 1, |
||
| 191 | ), |
||
| 192 | 'increment_id = %i', |
||
| 193 | $task_to_perform['increment_id'] |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | |||
| 197 | // flag sub task in progress as on going |
||
| 198 | DB::update( |
||
| 199 | prefixTable('processes_tasks'), |
||
| 200 | array( |
||
| 201 | 'sub_task_in_progress' => 1, |
||
| 202 | ), |
||
| 203 | 'increment_id = %i', |
||
| 204 | $task_to_perform['increment_id'] |
||
| 205 | ); |
||
| 206 | |||
| 207 | // handle the task step |
||
| 208 | handleTaskStep($args, $ProcessArguments, $SETTINGS); |
||
| 209 | |||
| 210 | // update the task status |
||
| 211 | DB::update( |
||
| 212 | prefixTable('processes_tasks'), |
||
| 213 | array( |
||
| 214 | 'sub_task_in_progress' => 0, // flag sub task is no more in prgoress |
||
| 215 | 'task' => json_encode(["status" => "Done"]), |
||
| 216 | 'is_in_progress' => -1, |
||
| 217 | 'finished_at' => time(), |
||
| 218 | 'updated_at' => time(), |
||
| 219 | ), |
||
| 220 | 'increment_id = %i', |
||
| 221 | $task_to_perform['increment_id'] |
||
| 222 | ); |
||
| 223 | |||
| 224 | provideLog('[TASK]['.$args['step'].'] starting at '.$args['index'].' is done.', $SETTINGS); |
||
| 225 | |||
| 226 | // are all tasks done? |
||
| 227 | DB::query( |
||
| 228 | 'SELECT * |
||
| 229 | FROM ' . prefixTable('processes_tasks') . ' |
||
| 230 | WHERE process_id = %i AND finished_at IS NULL', |
||
| 231 | $processId |
||
| 232 | ); |
||
| 233 | if (DB::count() === 0) { |
||
| 234 | // all tasks are done |
||
| 235 | provideLog('[PROCESS]['.$processId.'][FINISHED]', $SETTINGS); |
||
| 236 | DB::debugmode(false); |
||
| 237 | DB::update( |
||
| 238 | prefixTable('processes'), |
||
| 239 | array( |
||
| 240 | 'finished_at' => time(), |
||
| 241 | 'is_in_progress' => -1, |
||
| 242 | 'arguments' => json_encode([ |
||
| 243 | 'new_user_id' => isset($ProcessArguments['new_user_id']) === true ? $ProcessArguments['new_user_id'] : '', |
||
| 244 | ]) |
||
| 245 | ), |
||
| 246 | 'increment_id = %i', |
||
| 247 | $processId |
||
| 248 | ); |
||
| 249 | |||
| 250 | // if item was being updated then remove the edition lock |
||
| 251 | if (is_null($itemId) === false) { |
||
| 252 | DB::delete(prefixTable('items_edition'), 'item_id = %i', $itemId); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | return false; |
||
| 256 | |||
| 257 | } else { |
||
| 258 | // Task is currently being in progress by another server process |
||
| 259 | provideLog('[TASK][#'. $task_to_perform['increment_id'].'][WARNING] Similar task already being processes', $SETTINGS); |
||
| 260 | return false; |
||
| 261 | } |
||
| 262 | } else { |
||
| 263 | // no more task to perform |
||
| 264 | provideLog('[PROCESS]['.$processId.'][FINISHED]', $SETTINGS); |
||
| 265 | DB::update( |
||
| 266 | prefixTable('processes'), |
||
| 267 | array( |
||
| 268 | 'finished_at' => time(), |
||
| 269 | 'is_in_progress' => -1, |
||
| 270 | 'arguments' => json_encode([ |
||
| 271 | 'item_id' => isset($ProcessArguments['item_id']) === true ? $ProcessArguments['item_id'] : '', |
||
| 272 | ]) |
||
| 273 | ), |
||
| 274 | 'increment_id = %i', |
||
| 275 | $processId |
||
| 276 | ); |
||
| 277 | |||
| 278 | // if item was being updated then remove the edition lock |
||
| 279 | if (is_null($itemId) === false) { |
||
| 280 | DB::delete(prefixTable('items_edition'), 'item_id = %i', $itemId); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | return false; |
||
| 284 | } |
||
| 349 | } |
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: