ro_contacthandler.php ➔ convertmycontacts()   B
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 56

Duplication

Lines 10
Ratio 17.86 %

Importance

Changes 0
Metric Value
cc 8
nc 5
nop 1
dl 10
loc 56
rs 7.7155
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
3
/**
4
 * @param $strcontact
5
 * @return string
6
 */
7
function convertmycontacts($strcontact)
8
{
9
    // Ermitteln aller einzelnen Wörter von Kontakt aus Termin piCal
10
    // Umwandeln der einzelnen Namen in Link auf Benutzerkonto, wenn Name ein Mitgliedsname ist
11
    $strsearch    = ' ';
12
    $strnew       = '';
13
    $strseperator = '';
0 ignored issues
show
Unused Code introduced by
$strseperator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
14
15
    $pos1 = 0;
16
    $pos2 = strpos($strcontact, $strsearch, $pos1);
17
18
    if ($pos2 === false) {
19
        //echo "<br>kein leerzeichen";
20
        $struser = $strcontact;
21
        $struid  = getuid($struser);
22 View Code Duplication
        if ($struid == -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
            $strnew = $struser;
24
        } else {
25
            $strnew = "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $struid . "' title='" . $struser . "'>" . $struser . '</a>';
26
        }
27
    } else {
28
        //Leerzeichen vorhanden
29
        while ($pos2 !== false) {
30
            //alle wörter zwischen Leerzeichen ermitteln
31
            $struser = substr($strcontact, $pos1, $pos2 - $pos1);
32
            if (substr($struser, -1) === ',') {
33
                $struser      = substr($struser, 0, strlen($struser) - 1);
34
                $strseperator = ', ';
35
            } else {
36
                $strseperator = ' ';
37
            }
38
            $struid = getuid($struser);
39
            if ($struid == -1) {
40
                $strnew = $strnew . $struser . $strseperator;
41
            } else {
42
                $strnew = $strnew . "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $struid . "' title='" . $struser . "'>" . $struser . '</a>' . $strseperator;
43
            }
44
            $pos1 = $pos2 + 1;
45
            $pos2 = strpos($strcontact, $strsearch, $pos1);
46
        }
47
48
        if ($pos2 == 0) {
49
            //Rest ab letztem Leerzeichen einlesen
50
            $struser = substr($strcontact, $pos1);
51
            $struid  = getuid($struser);
52 View Code Duplication
            if ($struid == -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
                $strnew .= $struser;
54
            } else {
55
                $strnew = $strnew . "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $struid . "' title='" . $struser . "'>" . $struser . '</a>';
56
            }
57
        } else {
58
        }
59
    }
60
61
    return $strnew;
62
}
63
64
/**
65
 * @param $UserName
66
 * @return int
67
 */
68
function getuid($UserName)
69
{
70
    $rc  = -1;
71
    $db  = XoopsDatabaseFactory::getDatabaseConnection();
72
    $sql = 'SELECT uid FROM ' . $db->prefix('users') . " WHERE uname = '" . $UserName . "' LIMIT 0,1";
73
74
    $result = $db->query($sql);
75
    if ($result) {
76
        if ($db->getRowsNum($result) == 1) {
77
            $member = $GLOBALS['xoopsDB']->fetchObject($result);
78
            $userid = $member->uid;
79
            $rc     = $userid;
80
        }
81
    }
82
83
    return $rc;
84
}
85