Passed
Push — main ( 6044d0...c28500 )
by Paul
09:22
created

SanitizeUserName   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
eloc 13
c 2
b 1
f 0
dl 0
loc 26
ccs 12
cts 13
cp 0.9231
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 17 4
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Sanitizers;
4
5
class SanitizeUserName extends StringSanitizer
6
{
7
    /**
8
     * \p{L} = any kind of letter from any language.
9
     * \p{M} = any character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.).
10
     * \p{N} = any kind of numeric character in any script.
11
     * \p{Pf} = any kind of closing quote.
12
     * @see https://www.regular-expressions.info/unicode.html
13
     */
14 26
    public function run(): string
15
    {
16 26
        $value = wp_strip_all_tags($this->value());
17 26
        $value = preg_replace('/%([a-fA-F0-9][a-fA-F0-9])/', '', $value); // Remove percent-encoded characters.
18 26
        $value = preg_replace('/&.+?;/', '', $value); // Remove HTML entities.
19 26
        $value = preg_replace('/[^\p{L}\p{M}\p{N}\p{Pf}\'\.\,\- ]/u', '', $value);
20 26
        $value = sanitize_text_field($value); // Remove extra whitespace.
21 26
        if (defined('WP_IMPORTING')) {
22
            return $value;
23
        }
24 26
        if (empty($value)) {
25 9
            $user = wp_get_current_user();
26 9
            if ($user->exists()) {
27 2
                return $user->display_name;
28
            }
29
        }
30 26
        return $value;
31
    }
32
}
33