Completed
Push — Submit-Comment-Mobile-API ( dc14cd )
by
unknown
74:57 queued 54:25
created

Import::run()   D

Complexity

Conditions 16
Paths 66

Size

Total Lines 84
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
cc 16
eloc 51
nc 66
nop 3
dl 0
loc 84
rs 4.8736
c 0
b 0
f 0
ccs 0
cts 67
cp 0
crap 272

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Import::registerUser() 0 19 4
A Import::generateUniqueUsername() 11 23 3
A Import::getUserByAttributes() 0 14 4

How to fix   Long Method    Complexity   

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
namespace ModPleio;
3
4
class Import {
5
    public function extractData($columns, $data) {
6
        $result = [];
7
        foreach ($columns as $id => $metadata_name) {
8
            if (!$metadata_name) {
9
                continue;
10
            }
11
12
            $result[$metadata_name] = $data[$id];
13
        }
14
15
        return $result;
16
    }
17
18
    public function registerUser($data) {
19
        if (!$data["name"] || !$data["email"]) {
20
            return false;
21
        }
22
23
        $user = false;
24
25
        try {
26
            $username = Import::generateUniqueUsername($data);
27
            $password = generate_random_cleartext_password();
28
            $guid = register_user($username, $password, $data["name"], $data["email"]);
29
            elgg_set_user_validation_status($guid, true, "email");
0 ignored issues
show
Bug introduced by
It seems like $guid defined by register_user($username,...name'], $data['email']) on line 28 can also be of type false or null; however, elgg_set_user_validation_status() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
30
            $user = get_entity($guid);
0 ignored issues
show
Bug introduced by
It seems like $guid defined by register_user($username,...name'], $data['email']) on line 28 can also be of type false or null; however, get_entity() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
31
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class ModPleio\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
32
            elgg_log("Could not register user " . $e->getMessage(), "ERROR");
33
        }
34
35
        return $user;
36
    }
37
38
    public function generateUniqueUsername($data) {
39
        $email = $data["email"];
40
        list($name, $email) = explode("@", $email);
0 ignored issues
show
Unused Code introduced by
The assignment to $email is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
41
        $name = trim($name);
42
43
        $hidden = access_show_hidden_entities(true);
44
45 View Code Duplication
        if (get_user_by_username($name)) {
46
            $i = 1;
47
48
            while (get_user_by_username($name . $i)) {
49
                $i++;
50
            }
51
            
52
            $result = $name . $i;
53
        } else {
54
            $result = $name;
55
        }
56
57
        access_show_hidden_entities($hidden);
58
59
        return $result;
60
    }
61
62
    public function getUserByAttributes($data) {
63
        $user = false;
64
65
        if ($data["guid"]) {
66
            $user = get_entity($data["guid"]);
67
        } elseif ($data["username"]) {
68
            $user = get_user_by_username($data["username"]);
69
        } elseif ($data["email"]) {
70
            $users = get_user_by_email($data["email"]);
71
            $user = $users[0];
72
        }
73
74
        return $user;
75
    }
76
}