Passed
Pull Request — main (#39)
by Michael
12:39
created

Url::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
7
 *
8
 * @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
9
 * @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
10
 * @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
11
 */
12
13
namespace MichaelRubel\ValueObjects\Collection\Complex;
14
15
use Illuminate\Support\Facades\Validator;
16
use Illuminate\Support\Stringable;
17
use Illuminate\Validation\ValidationException;
18
use MichaelRubel\ValueObjects\Collection\Primitive\Text;
19
20
/**
21
 * "Url" object presenting a URL.
22
 *
23
 * @author Michael Rubél <[email protected]>
24
 *
25
 * @template TKey of array-key
26
 * @template TValue
27
 *
28
 * @method static static make(string|Stringable $value)
29
 * @method static static from(string|Stringable $value)
30
 * @method static static makeOrNull(string|Stringable|null $value)
31
 *
32
 * @extends Text<TKey, TValue>
33
 */
34
class Url extends Text
35
{
36
    /**
37
     * Create a new instance of the value object.
38
     *
39
     * @param  string|Stringable  $value
40
     */
41 16
    public function __construct(string|Stringable $value)
42
    {
43 16
        parent::__construct($value);
44
45 14
        $this->value = url($value);
0 ignored issues
show
Documentation Bug introduced by
It seems like url($value) can also be of type Illuminate\Contracts\Routing\UrlGenerator. However, the property $value is declared as type Illuminate\Support\Stringable|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
46
47 14
        $validator = Validator::make(
48 14
            ['url' => $this->value()],
49 14
            ['url' => $this->validationRules()],
50 14
        );
51
52 14
        if ($validator->fails()) {
53 2
            throw ValidationException::withMessages([__('Your URL is invalid.')]);
54
        }
55
    }
56
57
    /**
58
     * Define the rules for email validator.
59
     *
60
     * @return array
61
     */
62 14
    protected function validationRules(): array
63
    {
64 14
        return ['required', 'url'];
65
    }
66
}
67