Passed
Branch next (ee2197)
by Bas
02:37
created

ValidatesReferences::isVariable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\FluentAQL\Traits;
6
7
trait ValidatesReferences
8
{
9 7
    public function isBind(mixed $value): bool
10
    {
11 7
        if (is_string($value)) {
12 6
            return true;
13
        }
14 1
        if (is_object($value)) {
15
            return true;
16
        }
17
18 1
        return false;
19
    }
20
21 1
    public function isCollectionBind(mixed $value): bool
22
    {
23 1
        if (is_string($value)) {
24 1
            return true;
25
        }
26
27
        return false;
28
    }
29
30 28
    public function isVariable(mixed $value): bool
31
    {
32 28
        if (is_string($value) && preg_match('/^\$?[a-zA-Z_][a-zA-Z0-9_]*+$/', $value)) {
33 28
            return true;
34
        }
35
36 5
        return false;
37
    }
38
39
    /**
40
     * @param  array<mixed>  $registeredVariables
41
     */
42 1
    public function isRegisteredVariable(
43
        mixed $value,
44
        array $registeredVariables = []
45
    ): bool {
46 1
        return isset($registeredVariables[$value]);
47
    }
48
49 1
    public function isAttribute(mixed $value): bool
50
    {
51 1
        $pattern = '/^(@?[\d\w_]+|`@?[\d\w_]+`)(\[\`.+\`\]|\[[\d\w\*]*\])*'
52
            . '(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/';
53
        if (
54 1
            is_string($value) &&
55 1
            preg_match($pattern, $value)
56
        ) {
57 1
            return true;
58
        }
59
60 1
        return false;
61
    }
62
63
    /**
64
     * @param array<mixed> $registeredVariables
65
     */
66 25
    public function isReference(
67
        mixed $value,
68
        array $registeredVariables = []
69
    ): bool {
70 25
        $variables = '';
71 25
        if (!empty($registeredVariables)) {
72 23
            $variables = implode('|', $registeredVariables);
73
        }
74
75 25
        if (! is_string($value) || empty($value)) {
76 2
            return false;
77
        }
78
79 25
        return (bool) preg_match(
80 25
            '/^('
81 25
            . $variables
82 25
            . '|CURRENT|NEW|OLD)(\[\`.+\`\]|\[[\d\w\*]*\])*(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/',
83
            $value
84
        );
85
    }
86
}
87