UrlType::isValid()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Api\Scalar;
6
7
final class UrlType extends AbstractStringBasedType
8
{
9
    public ?string $description = 'An absolute web URL that must start with `http` or `https` or be an empty string.';
10
11
    /**
12
     * Validate an URL.
13
     */
14 46
    protected function isValid(?string $value): bool
15
    {
16
        // Here we use a naive pattern that should ideally be kept in sync with Natural validator
17 46
        return $value === '' || is_string($value) && preg_match('~^https?://(?:[^.\s]+\.)+[^.\s]+$~', $value);
18
    }
19
}
20