ValidatesReferences::isReference()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 16
ccs 6
cts 6
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 30
    public function isVariable(mixed $value): bool
31
    {
32 30
        if (is_string($value) && preg_match('/^\$?[a-zA-Z_][a-zA-Z0-9_]*+$/', $value)) {
33 30
            return true;
34
        }
35
36 5
        return false;
37
    }
38
39
    /**
40
     * @param  array<array-key, mixed>  $registeredVariables
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
41
     */
42 1
    public function isRegisteredVariable(
43
        int|string $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<array-key, null|object|scalar> $registeredVariables
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, null|object|scalar> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, null|object|scalar>.
Loading history...
65
     */
66 27
    public function isReference(
67
        mixed $value,
68
        array $registeredVariables = []
69
    ): bool {
70
        /** @psalm-suppress  ArgumentTypeCoercion */
71 27
        $variables = implode('|', $registeredVariables);
72
73 27
        if (! is_string($value) || empty($value)) {
74 2
            return false;
75
        }
76
77 27
        return (bool) preg_match(
78 27
            '/^('
79
            . $variables
80
            . '|CURRENT|NEW|OLD)(\[\`.+\`\]|\[[\d\w\*]*\])*(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/',
81
            $value
82
        );
83
    }
84
}
85