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

Import   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 73
Duplicated Lines 15.07 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 11
loc 73
rs 10
c 0
b 0
f 0
ccs 0
cts 55
cp 0
wmc 14
lcom 0
cbo 0

4 Methods

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

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}