Validation::validateNeedles()   A
last analyzed

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
 * This file is part of the "andrey-helldar/support" project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Andrey Helldar <[email protected]>
9
 *
10
 * @copyright 2021 Andrey Helldar
11
 *
12
 * @license MIT
13
 *
14
 * @see https://github.com/andrey-helldar/support
15
 */
16
17
namespace Helldar\Support\Concerns;
18
19
use Helldar\Support\Exceptions\ForbiddenVariableTypeException;
20
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
21
use Helldar\Support\Facades\Helpers\Str;
22
23
trait Validation
24
{
25
    /**
26
     * @param  mixed  $haystack
27
     * @param  array|string  $needles
28
     */
29 65
    protected function validateType($haystack, $needles): void
30
    {
31 65
        $type    = $this->validateGetType($haystack);
32 65
        $needles = $this->validateNeedles($needles);
33
34 65
        if (! Str::contains($type, $needles)) {
35 1
            throw new ForbiddenVariableTypeException($type, $needles);
36
        }
37 64
    }
38
39 65
    protected function validateNeedles($values): array
40
    {
41 65
        return Arrayable::of($values)
42 65
            ->map(static function ($value) {
43 65
                return Str::lower($value);
44 65
            })->get();
45
    }
46
47 65
    protected function validateGetType($haystack): string
48
    {
49 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...
50
    }
51
}
52