Passed
Push — 5.x ( 65a76f...3a1e20 )
by Enjoys
59s queued 13s
created

Email   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 17
c 1
b 0
f 0
dl 0
loc 46
ccs 17
cts 19
cp 0.8947
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 11 2
A setMessage() 0 6 2
A validate() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Rule;
6
7
use Enjoys\Forms\Element;
8
use Enjoys\Forms\Interfaces\Ruleable;
9
use Enjoys\Forms\Rules;
10
11
class Email extends Rules implements RuleInterface
12
{
13
//    private $idn_to_ascii = false;
14
15 3
    public function setMessage(?string $message = null): ?string
16
    {
17 3
        if (is_null($message)) {
18 3
            $message = 'Не правильно введен email';
19
        }
20 3
        return parent::setMessage($message);
21
    }
22
23
    /**
24
     * @psalm-suppress PossiblyNullReference
25
     * @param Ruleable&Element $element
26
     * @return bool
27
     */
28 3
    public function validate(Ruleable $element): bool
29
    {
30
31 3
        $method = $this->getRequest()->getRequest()->getMethod();
32 3
        $requestData = match (strtolower($method)) {
33 3
            'get' => $this->getRequest()->getQueryData()->toArray(),
34
            'post' => $this->getRequest()->getPostData()->toArray(),
35
            default => []
36
        };
37 3
        $value = \getValueByIndexPath($element->getName(), $requestData);
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Enjoys\Forms\Interfaces\Ruleable. Since it exists in all sub-types, consider adding an abstract or default implementation to Enjoys\Forms\Interfaces\Ruleable. ( Ignorable by Annotation )

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

37
        $value = \getValueByIndexPath($element->/** @scrutinizer ignore-call */ getName(), $requestData);
Loading history...
38 3
        if (!$this->check(\trim($value))) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type false; however, parameter $string of trim() 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

38
        if (!$this->check(\trim(/** @scrutinizer ignore-type */ $value))) {
Loading history...
39 1
            $element->setRuleError($this->getMessage());
40 1
            return false;
41
        }
42 2
        return true;
43
    }
44
45
46 3
    private function check(string $value)
47
    {
48 3
        if (empty($value)) {
49 1
            return true;
50
        }
51
52
//        if ($this->idn_to_ascii === true) {
53
//            $value = idn_to_ascii($value);
54
//        }
55
56 2
        return filter_var($value, \FILTER_VALIDATE_EMAIL);
57
    }
58
}
59