| Conditions | 53 |
| Paths | > 20000 |
| Total Lines | 150 |
| Code Lines | 97 |
| Lines | 28 |
| Ratio | 18.67 % |
| 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 268 | function post_blog($user, $title, $excerpt, $body, $container_guid, $comments, $access, $status, $lang) |
||
| 269 | { |
||
| 270 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
| 271 | if (!$user_entity) { |
||
| 272 | return "User was not found. Please try a different GUID, username, or email address"; |
||
| 273 | } |
||
| 274 | if (!$user_entity instanceof ElggUser) { |
||
| 275 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
| 276 | } |
||
| 277 | if (!elgg_is_logged_in()) { |
||
| 278 | login($user_entity); |
||
| 279 | } |
||
| 280 | $error = FALSE; |
||
| 281 | //check required fields being not empty |
||
| 282 | $titles = json_decode($title); |
||
| 283 | $bodies = json_decode($body); |
||
| 284 | $excerpts = json_decode($excerpt); |
||
| 285 | //Check Required |
||
| 286 | if (!$titles->en && !$titles->fr) { return elgg_echo("blog:error:missing:title"); } |
||
| 287 | if (!$bodies->en && !$bodies->fr) { return elgg_echo("blog:error:missing:description"); } |
||
| 288 | if (!($titles->en && $bodies->en) && !($titles->fr && $bodies->fr)) { return "require-same-lang"; } |
||
| 289 | //Default any Missing or faulty |
||
| 290 | if (!$titles->en) { $titles->en = ''; } |
||
| 291 | if (!$titles->fr) { $titles->fr = ''; } |
||
| 292 | if (!$bodies->en) { $bodies->en = ''; } |
||
| 293 | if (!$bodies->fr) { $bodies->fr = ''; } |
||
| 294 | if (!$excerpts->en) { $excerpts->en = ''; } |
||
| 295 | if (!$excerpts->fr) { $excerpts->fr = ''; } |
||
| 296 | if ($comments != 0 && $comments != 1) { $comments = 1; } |
||
| 297 | if ($access != 0 && $access != 1 && $access != -2 ) { $access = 1; } |
||
| 298 | if ($status != 0 && $status != 1) { $status = 0; } |
||
| 299 | |||
| 300 | //If no group container, use user guid. |
||
| 301 | if ($container_guid==''){ $container_guid = $user_entity->guid; } |
||
| 302 | |||
| 303 | //Set int variables to correct |
||
| 304 | if ($status == 1) { $status = 'published'; } else { $status = 'draft'; } |
||
| 305 | if ($comments == 1) { $comments = 'On'; } else { $comments = 'Off'; } |
||
| 306 | if ($access == 1 ) { $access = ACCESS_PUBLIC; } |
||
| 307 | if ($status == 'draft') { $access = ACCESS_PRIVATE; } |
||
| 308 | $titles->en = htmlspecialchars($titles->en, ENT_QUOTES, 'UTF-8'); |
||
| 309 | $titles->fr = htmlspecialchars($titles->fr, ENT_QUOTES, 'UTF-8'); |
||
| 310 | $excerpts->en = elgg_get_excerpt($excerpts->en); |
||
| 311 | $excerpts->fr = elgg_get_excerpt($excerpts->fr); |
||
| 312 | |||
| 313 | $values = array( |
||
| 314 | 'title' => JSON_encode($titles), |
||
| 315 | 'title2' => '', |
||
| 316 | //'title3' => '', |
||
| 317 | 'description' => JSON_encode($bodies), |
||
| 318 | 'description2' => '', |
||
| 319 | 'description3' => '', |
||
| 320 | 'status' => $status, |
||
| 321 | 'access_id' => $access, |
||
| 322 | 'comments_on' => $comments, |
||
| 323 | 'excerpt' => JSON_encode($excerpts), |
||
| 324 | 'excerpt2' => '', |
||
| 325 | 'excerpt3' => '', |
||
| 326 | 'tags' => '', |
||
| 327 | 'publication_date' => '', |
||
| 328 | 'expiration_date' => '', |
||
| 329 | 'show_owner' => 'no' |
||
| 330 | ); |
||
| 331 | |||
| 332 | //return $values; |
||
| 333 | //return elgg_echo("test: pre blog creation"); |
||
| 334 | //Create blog |
||
| 335 | $blog = new ElggBlog(); |
||
| 336 | $blog->subtype = 'blog'; |
||
| 337 | $blog->container_guid = $container_guid; |
||
| 338 | $new_post = TRUE; |
||
| 339 | |||
| 340 | $old_status = $blog->status; |
||
| 341 | |||
| 342 | // assign values to the entity, stopping on error. |
||
| 343 | View Code Duplication | if (!$error) { |
|
| 344 | foreach ($values as $name => $value) { |
||
| 345 | if (($name != 'title2') && ($name != 'description2') && ($name != 'excerpt2')){ // remove input 2 in metastring table |
||
| 346 | $blog->$name = $value; |
||
| 347 | } |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | if (!$error){ |
||
| 352 | if ($blog->save()){ |
||
| 353 | |||
| 354 | $icon_file = get_resized_image_from_uploaded_file("icon", 100, 100); |
||
| 355 | $icon_sizes = elgg_get_config("icon_sizes"); |
||
| 356 | |||
| 357 | View Code Duplication | if (!empty($icon_file) && !empty($icon_sizes)) { |
|
| 358 | // create icon |
||
| 359 | $prefix = "blogs/" . $blog->getGUID(); |
||
| 360 | |||
| 361 | $fh = new ElggFile(); |
||
| 362 | $fh->owner_guid = $blog->getOwnerGUID(); |
||
| 363 | |||
| 364 | foreach ($icon_sizes as $icon_name => $icon_info) { |
||
| 365 | $icon_file = get_resized_image_from_uploaded_file("icon", $icon_info["w"], $icon_info["h"], $icon_info["square"], $icon_info["upscale"]); |
||
| 366 | if (!empty($icon_file)) { |
||
| 367 | $fh->setFilename($prefix . $icon_name . ".jpg"); |
||
| 368 | |||
| 369 | if ($fh->open("write")) { |
||
| 370 | $fh->write($icon_file); |
||
| 371 | $fh->close(); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | $blog->icontime = time(); |
||
| 377 | } |
||
| 378 | // no longer a brand new post. |
||
| 379 | $blog->deleteMetadata('new_post'); |
||
| 380 | |||
| 381 | $status = $blog->status; |
||
| 382 | // add to river if changing status or published, regardless of new post |
||
| 383 | // because we remove it for drafts. |
||
| 384 | if (($new_post || $old_status == 'draft' || $old_status == 'published') && $status == 'published') { |
||
| 385 | elgg_create_river_item(array( |
||
| 386 | 'view' => 'river/object/blog/create', |
||
| 387 | 'action_type' => 'create', |
||
| 388 | 'subject_guid' => $blog->owner_guid, |
||
| 389 | 'object_guid' => $blog->getGUID(), |
||
| 390 | )); |
||
| 391 | // we only want notifications sent when post published |
||
| 392 | elgg_trigger_event('publish', 'object', $blog); |
||
|
|
|||
| 393 | |||
| 394 | // reset the creation time for posts that move from draft to published |
||
| 395 | if ($guid) { |
||
| 396 | $blog->time_created = time(); |
||
| 397 | $blog->save(); |
||
| 398 | } |
||
| 399 | } elseif ($old_status == 'published' && $status == 'draft') { |
||
| 400 | elgg_delete_river(array( |
||
| 401 | 'object_guid' => $blog->guid, |
||
| 402 | 'action_type' => 'create', |
||
| 403 | )); |
||
| 404 | } |
||
| 405 | if ($blog->status == 'published' || $save == false) { |
||
| 406 | return ($blog->getURL()); |
||
| 407 | } else { |
||
| 408 | return ("blog/edit/$blog->guid"); |
||
| 409 | } |
||
| 410 | |||
| 411 | } else { |
||
| 412 | return elgg_echo('blog:error:cannot_save'); |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | return "end, unknown result"; |
||
| 417 | } |
||
| 418 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: