Failed Conditions
Push — refactor/improve-static-analys... ( 8da3ef...a7b39f )
by Bas
09:53
created

HasAliases::normalizeColumnReferences()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.6111
cc 5
nc 4
nop 2
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
6
7
use Exception;
8
use Illuminate\Support\Str;
9
10
trait HasAliases
11
{
12
    protected $tableAliases = [];
13
14
    protected $columnAliases = [];
15
16
    /**
17
     * @param  string  $table
18
     * @param  string|null  $alias
19
     * @return string
20
     */
21 162
    public function registerTableAlias(string $table, string $alias = null): string
22
    {
23 162
        if ($alias == null && stripos($table, ' as ') !== false) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $alias of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
24
            [$table, $alias] = explode(' as ', $table);
25
        }
26 162
        if ($alias == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $alias of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
27 162
            $alias = $this->generateTableAlias($table);
28
        }
29 162
        $this->tableAliases[$table] = $alias;
30
31 162
        return $alias;
32
    }
33
34 117
    protected function isTableAlias(string $alias)
35
    {
36 117
        return in_array($alias, $this->tableAliases);
37
    }
38
39 138
    protected function getTableAlias(string $table): string|null
40
    {
41 138
        if (isset($this->tableAliases[$table])) {
42 118
            return $this->tableAliases[$table];
43
        }
44
45 117
        return null;
46
    }
47
48
    /**
49
     * Extract table and alias from sql alias notation (entity AS `alias`)
50
     *
51
     * @param string $entity
52
     * @param int|string|null $key
53
     * @return array<mixed>
54 34
     * @throws Exception
55
     */
56 34
    protected function extractAlias(string $entity, int|string $key = null): array
57 34
    {
58 14
        $results = preg_split("/\sas\s/i", $entity);
59
60 34
        if ($results === false) {
61 23
            throw new Exception('Column splitting failed');
62
        }
63
64 34
        if (isset($results[1])) {
65
            $results[1] = trim($results[1], '`');
66
        }
67 162
        if (! isset($results[1]) && is_string($key)) {
68
            $results[1] = $key;
69 162
        }
70
        if (! isset($results[1])) {
71
            $results[1] = $results[0];
72
        }
73
74
        return $results;
75
    }
76
77
    protected function generateTableAlias(string $table, string $postfix = 'Doc'): string
78
    {
79
        return Str::camel(Str::singular($table)) . $postfix;
80
    }
81
82
    protected function replaceTableForAlias($reference): string
83
    {
84
        $referenceParts = explode('.', $reference);
85
        $first = array_shift($referenceParts);
86
        $alias = $this->getTableAlias($first);
87
        if ($alias == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $alias of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
88
            $alias = $first;
89
        }
90
        array_unshift($referenceParts, $alias);
91
92
        return implode('.', $referenceParts);
93
    }
94
95
    protected function prefixAlias(string $target, string $value): string
96
    {
97
        $alias = $this->getTableAlias($target);
98
99
        if (Str::startsWith($value, $alias . '.')) {
100
            return $value;
101 1
        }
102
103 1
        return $alias . '.' . $value;
104
    }
105
106
    /**
107 1
     * @param string $column
108 1
     * @param string|null $alias
109 1
     * @return bool
110
     * @throws Exception
111
     */
112
    public function registerColumnAlias(string $column, string $alias = null): bool
113
    {
114
        if (preg_match("/\sas\s/i", $column)) {
115
            [$column, $alias] = $this->extractAlias($column);
116
        }
117
118
        if (isset($alias)) {
119
            $this->columnAliases[$column] = $alias;
120
            return true;
121
        }
122
123
        return false;
124 121
    }
125
126 121
    protected function getColumnAlias(string $column): string|null
127 3
    {
128
        if (isset($this->columnAliases[$column])) {
129
            return $this->columnAliases[$column];
130 120
        }
131
132 120
        return null;
133 1
    }
134
135
    /**
136 120
     * @param array<mixed>|string $column
137 3
     * @return array<mixed>|string
138
     */
139
    protected function convertColumnId(array|string $column): array|string
140 117
    {
141 117
        if (is_string($column) || is_array($column)) {
0 ignored issues
show
introduced by
The condition is_array($column) is always true.
Loading history...
142
            $column = $this->convertIdToKey($column);
0 ignored issues
show
Bug introduced by
It seems like convertIdToKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
            /** @scrutinizer ignore-call */ 
143
            $column = $this->convertIdToKey($column);
Loading history...
143
        }
144
145 117
        return $column;
146
    }
147
}
148