Passed
Push — master ( 9e8075...242c87 )
by Dedipyaman
01:41
created

Username   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 3 1
A __construct() 0 20 3
A __toString() 0 3 1
A getOption() 0 2 2
1
<?php
2
/*
3
 * This file is part of Phypes <https://github.com/2DSharp/Phypes>.
4
 *
5
 * (c) Dedipyaman Das <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Phypes\Type;
13
14
15
use Phypes\Result\Failure;
16
use Phypes\Validator\UsernameValidator;
17
use Phypes\Validator\Validator;
18
19
class Username implements Type
20
{
21
    const OPT_MIN_LEN = 0;
22
    const OPT_MAX_LEN = 1;
23
    const OPT_ALLOWED_SPECIAL_CHARS = 2;
24
    /**
25
     * @var string $username
26
     */
27
    private $username;
28
29
    private function getOption(int $key, $arr = [], $default) {
30
        return isset($arr[$key]) ? $arr[$key]: $default;
31
    }
32
    public function __construct(string $username, $options = [], Validator $validator = null)
33
    {
34
        if ($validator == null) {
35
            // use the default validator
36
            $validator = new UsernameValidator(
37
                $this->getOption(self::OPT_MIN_LEN, $options, 4),
38
                $this->getOption(self::OPT_MAX_LEN, $options, 12),
39
                $this->getOption(self::OPT_ALLOWED_SPECIAL_CHARS, $options, ['-', '_']));
40
        }
41
42
        $result = $validator->validate($username, $options);
0 ignored issues
show
Unused Code introduced by
The call to Phypes\Validator\Validator::validate() has too many arguments starting with $options. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        /** @scrutinizer ignore-call */ 
43
        $result = $validator->validate($username, $options);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Unused Code introduced by
The call to Phypes\Validator\UsernameValidator::validate() has too many arguments starting with $options. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        /** @scrutinizer ignore-call */ 
43
        $result = $validator->validate($username, $options);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
43
44
        if (!$result->isValid()) {
45
            /**
46
             * @var Failure $result
47
             */
48
            $error = $result->getFirstError();
49
            throw new \InvalidArgumentException($error->getMessage(), $error->getCode());
50
        }
51
        $this->username = $username;
52
    }
53
54
    public function __toString(): string
55
    {
56
        return $this->username;
57
    }
58
59
    public function getValue()
60
    {
61
        return $this->username;
62
    }
63
}