Passed
Push — master ( ae9f09...aff02c )
by Dedipyaman
01:51
created

Username::getOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 3
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
use function Phypes\getOptionalValue;
19
20
class Username implements Type
21
{
22
    const OPT_MIN_LEN = 0;
23
    const OPT_MAX_LEN = 1;
24
    const OPT_ALLOWED_SPECIAL_CHARS = 2;
25
    /**
26
     * @var string $username
27
     */
28
    private $username;
29
30
31
    public function __construct(string $username, $options = [], Validator $validator = null)
32
    {
33
        if ($validator == null) {
34
            // use the default validator
35
            $validator = new UsernameValidator(
36
                getOptionalValue(self::OPT_MIN_LEN, $options, 4),
37
                getOptionalValue(self::OPT_MAX_LEN, $options, 12),
38
                getOptionalValue(self::OPT_ALLOWED_SPECIAL_CHARS, $options, ['-', '_']));
39
        }
40
41
        $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

41
        /** @scrutinizer ignore-call */ 
42
        $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

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