Completed
Push — Api_opt ( 47a261...24829b )
by
unknown
69:01 queued 36:29
created

start.php ➔ transfer_file_to_new_user()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 76
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 54
nc 4
nop 3
dl 0
loc 76
rs 8.623
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
elgg_register_event_handler('init', 'system', 'merge_users_init');
3
4
function merge_users_init() {
5
6
    elgg_register_action('users/merge', elgg_get_plugins_path() . "/merge_users/actions/users/merge.php");
7
8
    elgg_register_ajax_view("merge_users/display");
9
10
    elgg_register_admin_menu_item('administer', 'merge_users', 'users');
11
12
}
13
14
//we don't use this but it's good to have it
15
function transfer_file_to_new_user($object, $NU_guid, $OLD_guid) {
16
17
  $prefix = 'file/';
18
19
  $file = new FilePluginFile($object->guid);
20
21
  $nfh = new ElggFile();
22
  $nfh->owner_guid = $NU_guid;
23
  if($file->container_guid != $OLD_guid){
24
    $nfh->container_guid = $file->container_guid;
25
  } else {
26
    $nfh->container_guid = $NU_guid;
27
  }
28
  $nfh->subtype = 'file';
29
  $nfh->title = $file->title;
30
  $nfh->description = $file->description;
31
  $nfh->access_id = $file->access_id;
32
  $nfh->tags = $file->tags;
33
34
  $filename = $file->getFilenameOnFilestore();
35
36
  $name = end(explode('/', $filename));
0 ignored issues
show
Bug introduced by
explode('/', $filename) cannot be passed to end() as the parameter $array expects a reference.
Loading history...
37
  $nfh->setFilename($prefix . $name);
38
39
  $file->open("read");
40
  $nfh->open("write");
41
42
  $nfh->write($file->grabFile());
43
44
  if($file->simpletype == "image"){
45
    //lets create some thumbnails
46
    $thumb = new ElggFile();
47
  	$thumb->owner_guid = $nfh->owner_guid;
48
49
  	$sizes = [
50
  		'small' => [
51
  			'w' => 60,
52
  			'h' => 60,
53
  			'square' => true,
54
  			'metadata_name' => 'thumbnail',
55
  			'filename_prefix' => 'thumb',
56
  		],
57
  		'medium' => [
58
  			'w' => 153,
59
  			'h' => 153,
60
  			'square' => true,
61
  			'metadata_name' => 'smallthumb',
62
  			'filename_prefix' => 'smallthumb',
63
  		],
64
  		'large' => [
65
  			'w' => 600,
66
  			'h' => 600,
67
  			'square' => false,
68
  			'metadata_name' => 'largethumb',
69
  			'filename_prefix' => 'largethumb',
70
  		],
71
  	];
72
73
    foreach ($sizes as $size => $data) {
74
      $image_bytes = get_resized_image_from_existing_file($nfh->getFilenameOnFilestore(), $data['w'], $data['h'], $data['square']);
75
      $name = explode('.', $name);
76
      $filename2 = "{$prefix}{$data['filename_prefix']}{$name[0]}.jpg";
77
      $thumb->setFilename($filename2);
78
      $thumb->open("write");
79
      $thumb->write($image_bytes);
0 ignored issues
show
Security Bug introduced by
It seems like $image_bytes defined by get_resized_image_from_e...['h'], $data['square']) on line 74 can also be of type false; however, ElggFile::write() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
80
      $thumb->close();
81
      unset($image_bytes);
82
83
      $nfh->{$data['metadata_name']} = $filename2;
84
    }
85
  }
86
87
  // close file
88
  $file->close();
89
  $nfh->close();
90
}
91