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

Validation::validateNeedles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
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