ValidateUname::validate()   F
last analyzed

Complexity

Conditions 14
Paths 768

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 34
c 0
b 0
f 0
nc 768
nop 0
dl 0
loc 51
rs 2.4222

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 declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp\Validation;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Brian Wahoff <[email protected]>
19
 * @author       Eric Juden <[email protected]>
20
 * @author       XOOPS Development Team
21
 */
22
23
/**
24
 *  ValidatorUname subclass of Validator
25
 *  Validates a username
26
 */
27
class ValidateUname extends Validator
28
{
29
    /**
30
     * Private
31
     * $uname the username to validate
32
     */
33
    public $uname;
34
    //! A constructor.
35
36
    /**
37
     * Constructs a new ValidateUname object subclass or Validator
38
     * @param string $uname the string to validate
39
     */
40
    public function __construct(string $uname)
41
    {
42
        $this->uname = $uname;
43
        parent::__construct();
44
    }
45
46
    //! A manipulator
47
48
    /**
49
     * Validates an email address
50
     */
51
    public function validate()
52
    {
53
        /** @var \XoopsConfigHandler $configHandler */
54
        $configHandler = \xoops_getHandler('config');
55
        //$xoopsConfigUser = $configHandler->getConfigsByCat(XOOPS_CONF_USER);
56
        $xoopsConfigUser = [];
57
        $criteria        = new \Criteria('conf_catid', 2);
58
        $myConfigs       = $configHandler->getConfigs($criteria);
59
        foreach ($myConfigs as $myConf) {
60
            $xoopsConfigUser[$myConf->getVar('conf_name')] = $myConf->getVar('conf_value');
61
        }
62
        $xoopsDB = \XoopsDatabaseFactory::getDatabaseConnection();
63
64
        switch ($xoopsConfigUser['uname_test_level']) {
65
            case 0:
66
                // strict
67
                $restriction = '/[^a-zA-Z0-9\_\-]/';
68
                break;
69
            case 1:
70
                // medium
71
                $restriction = '/[^a-zA-Z0-9\_\-\<\>\,\.\$\%\#\@\!\\\'\"]/';
72
                break;
73
            case 2:
74
                // loose
75
                $restriction = '/[\000-\040]/';
76
                break;
77
        }
78
79
        if (empty($this->uname) || \preg_match($restriction, $this->uname)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $restriction does not seem to be defined for all execution paths leading up to this point.
Loading history...
80
            $this->setError(\_XHELP_MESSAGE_INVALID);
81
        }
82
        if (mb_strlen($this->uname) > $xoopsConfigUser['maxuname']) {
83
            $this->setError(\sprintf(\_XHELP_MESSAGE_LONG, $xoopsConfigUser['maxuname']));
84
        }
85
        if (mb_strlen($this->uname) < $xoopsConfigUser['minuname']) {
86
            $this->setError(\sprintf(\_XHELP_MESSAGE_SHORT, $xoopsConfigUser['minuname']));
87
        }
88
        foreach ($xoopsConfigUser['bad_unames'] as $bu) {
89
            if (!empty($bu) && \preg_match('/' . $bu . '/i', $this->uname)) {
90
                $this->setError(\_XHELP_MESSAGE_RESERVED);
91
                break;
92
            }
93
        }
94
        if (mb_strrpos($this->uname, ' ') > 0) {
95
            $this->setError(\_XHELP_MESSAGE_NO_SPACES);
96
        }
97
        $sql    = 'SELECT COUNT(*) FROM ' . $xoopsDB->prefix('users') . " WHERE uname='" . \addslashes($this->uname) . "'";
98
        $result = $xoopsDB->query($sql);
99
        [$count] = $xoopsDB->fetchRow($result);
100
        if ($count > 0) {
101
            $this->setError(\_XHELP_MESSAGE_UNAME_TAKEN);
102
        }
103
    }
104
}
105