CleanUserNames   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A clean() 0 13 3
1
<?php
2
namespace Alister\ReservedNamesBundle\Services;
3
4
/**
5
 * Remove characters around the username to get to the essence of the username.
6
 *
7
 * This means any whitespace, or numbers. I'm assuming that usernames can only
8
 * contain characters in the set [a-zA-Z01-9_-], and will start with -_ or alpha
9
 */
10
class CleanUserNames implements CleanUserNamesInterface
11
{
12
    /**
13
     * Strip numbers and '-_' chars from around a usename.
14
     *
15
     *  * __php-33_   => php
16
     *  * __php_hello => php_hello
17
     *
18
     * @param string $username string to trim down
19
     *
20
     * @return string 'clean' username without some chars around the first 'word'
21
     */
22
    public function clean($username)
23
    {
24
        $username = strtolower($username);
25
        // remove leading and trailing digits/-_
26
        $username = trim($username, " \t\n\r\0\x0B0123456789-_");
27
28
        // now only return a string only up to the first number/-_ char
29
        $lenOfAlts = strcspn($username, '0123456789_-');
30
        if ($lenOfAlts > 1 && strlen($username) > $lenOfAlts) {
31
            $username = substr($username, 0, $lenOfAlts);
32
        }
33
34
        return $username;
35
    }
36
}
37