Passed
Push — main ( 35cea8...263c2f )
by Andrey
21:51 queued 20:06
created

Validation   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateType() 0 7 2
A validateGetType() 0 3 1
A validateNeedles() 0 6 1
1
<?php
2
3
namespace Helldar\Support\Concerns;
4
5
use Helldar\Support\Exceptions\ForbiddenVariableTypeException;
6
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
7
use Helldar\Support\Facades\Helpers\Str;
8
9
trait Validation
10
{
11
    /**
12
     * @param  mixed  $haystack
13
     * @param  array|string  $needles
14
     */
15 65
    protected function validateType($haystack, $needles): void
16
    {
17 65
        $type    = $this->validateGetType($haystack);
18 65
        $needles = $this->validateNeedles($needles);
19
20 65
        if (! Str::contains($type, $needles)) {
21 1
            throw new ForbiddenVariableTypeException($type, $needles);
22
        }
23 64
    }
24
25 65
    protected function validateNeedles($values): array
26
    {
27 65
        return Arrayable::of($values)
28 65
            ->map(static function ($value) {
29 65
                return Str::lower($value);
30 65
            })->get();
31
    }
32
33 65
    protected function validateGetType($haystack): string
34
    {
35 65
        return Str::lower(gettype($haystack));
0 ignored issues
show
Bug Best Practice introduced by
The expression return Helldar\Support\F...wer(gettype($haystack)) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
36
    }
37
}
38