Passed
Push — master ( cd2db1...fdabae )
by Alexander
02:29
created

Url::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 38
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 38
ccs 3
cts 4
cp 0.75
rs 10
cc 3
nc 2
nop 8
crap 3.1406

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use RuntimeException;
10
use Yiisoft\Validator\BeforeValidationInterface;
11
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
12
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13
use Yiisoft\Validator\SerializableRuleInterface;
14
use Yiisoft\Validator\ValidationContext;
15
16
use function function_exists;
17
18
/**
19
 * Validates that the value is a valid HTTP or HTTPS URL.
20
 *
21
 * Note that this rule only checks if the URL scheme and host part are correct.
22
 * It does not check the remaining parts of a URL.
23
 */
24
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
25
final class Url implements SerializableRuleInterface, BeforeValidationInterface
26
{
27
    use BeforeValidationTrait;
28
    use RuleNameTrait;
29
30 3
    public function __construct(
31
        /**
32
         * @var string the regular expression used to validate the value.
33
         * The pattern may contain a `{schemes}` token that will be replaced
34
         * by a regular expression which represents the {@see $schemes}.
35
         *
36
         * Note that if you want to reuse the pattern in HTML5 input it should have ^ and $, should not have any
37
         * modifiers and should not be case-insensitive.
38
         */
39
        private string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
40
        /**
41
         * @var array list of URI schemes which should be considered valid. By default, http and https
42
         * are considered to be valid schemes.
43
         */
44
        private array $validSchemes = ['http', 'https'],
45
        /**
46
         * @var bool whether validation process should take into account IDN (internationalized
47
         * domain names). Defaults to false meaning that validation of URLs containing IDN will always
48
         * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP
49
         * extension, otherwise an exception would be thrown.
50
         */
51
        private bool $enableIDN = false,
52
        private string $message = 'This value is not a valid URL.',
53
        private bool $skipOnEmpty = false,
54
        /**
55
         * @var callable
56
         */
57
        private $skipOnEmptyCallback = null,
58
        private bool $skipOnError = false,
59
        /**
60
         * @var Closure(mixed, ValidationContext):bool|null
61
         */
62
        private ?Closure $when = null,
63
    ) {
64 3
        $this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
65
66 3
        if ($enableIDN && !function_exists('idn_to_ascii')) {
67
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
68
        }
69
    }
70
71 41
    public function getPattern(): string
72
    {
73 41
        if (str_contains($this->pattern, '{schemes}')) {
74 38
            return str_replace('{schemes}', '((?i)' . implode('|', $this->validSchemes) . ')', $this->pattern);
75
        }
76
77 3
        return $this->pattern;
78
    }
79
80
    /**
81
     * @return array|string[]
82
     */
83
    public function getValidSchemes(): array
84
    {
85
        return $this->validSchemes;
86
    }
87
88
    /**
89
     * @return bool
90
     */
91 37
    public function isEnableIDN(): bool
92
    {
93 37
        return $this->enableIDN;
94
    }
95
96
    /**
97
     * @return string
98
     */
99 16
    public function getMessage(): string
100
    {
101 16
        return $this->message;
102
    }
103
104 4
    public function getOptions(): array
105
    {
106
        return [
107 4
            'pattern' => $this->getPattern(),
108 4
            'validSchemes' => $this->validSchemes,
109 4
            'enableIDN' => $this->enableIDN,
110
            'message' => [
111 4
                'message' => $this->message,
112
            ],
113 4
            'skipOnEmpty' => $this->skipOnEmpty,
114 4
            'skipOnError' => $this->skipOnError,
115
        ];
116
    }
117
118 1
    public function getHandlerClassName(): string
119
    {
120 1
        return UrlHandler::class;
121
    }
122
}
123