Test Failed
Pull Request — main (#8)
by Michael
03:26
created

Email::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
5
 *
6
 * @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
7
 * @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
8
 * @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
9
 */
10
11
namespace MichaelRubel\ValueObjects\Collection\Complex;
12
13
use Illuminate\Support\Stringable;
14
use Illuminate\Validation\Concerns\ValidatesAttributes;
15
use Illuminate\Validation\ValidationException;
16
use MichaelRubel\ValueObjects\Collection\Primitive\Text;
17
18
/**
19
 * "Email" object that represents the email address.
20
 *
21
 * @author Michael Rubél <[email protected]>
22
 *
23
 * @template TKey of array-key
24
 * @template TValue
25
 *
26
 * @method static static make(string|Stringable $value)
27
 * @method static static from(string|Stringable $value)
28
 * @method static static makeOrNull(string|Stringable|null $value)
29
 *
30
 * @extends Text<TKey, TValue>
31
 */
32
class Email extends Text
33
{
34
    use ValidatesAttributes;
35
36
    /**
37
     * Create a new instance of the value object.
38
     *
39
     * @param  string|Stringable  $value
40
     */
41
    public function __construct(protected string|Stringable $value)
42
    {
43
        parent::__construct($this->value);
44
45
        $this->validate();
46
    }
47
48
    /**
49
     * Validate the email.
50
     *
51
     * @return void
52
     */
53
    protected function validate(): void
54
    {
55
        $toValidate = ['email', $this->value(), $this->validationParameters()];
56
57
        if (! $this->validateEmail(...$toValidate)) {
0 ignored issues
show
Bug introduced by
It seems like $toValidate can also be of type array<integer,string>; however, parameter $attribute of MichaelRubel\ValueObject...\Email::validateEmail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

57
        if (! $this->validateEmail(/** @scrutinizer ignore-type */ ...$toValidate)) {
Loading history...
58
            throw ValidationException::withMessages([__('Your email is invalid.')]);
59
        }
60
    }
61
62
    /**
63
     * Define how you want to validate the email.
64
     *
65
     * @return array
66
     */
67
    protected function validationParameters(): array
68
    {
69
        return ['filter', 'spoof'];
70
    }
71
}
72