Passed
Pull Request — master (#3)
by
unknown
11:20
created

MapperTrait::getPrimaryKey()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 1
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
/********************************************************************************
3
 *   Apache License, Version 2.0                                                *
4
 *                                                                              *
5
 *   Copyright [2020] [Nurlan Mukhanov <[email protected]>]                      *
6
 *                                                                              *
7
 *   Licensed under the Apache License, Version 2.0 (the "License");            *
8
 *   you may not use this file except in compliance with the License.           *
9
 *   You may obtain a copy of the License at                                    *
10
 *                                                                              *
11
 *       http://www.apache.org/licenses/LICENSE-2.0                             *
12
 *                                                                              *
13
 *   Unless required by applicable law or agreed to in writing, software        *
14
 *   distributed under the License is distributed on an "AS IS" BASIS,          *
15
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
16
 *   See the License for the specific language governing permissions and        *
17
 *   limitations under the License.                                             *
18
 *                                                                              *
19
 ********************************************************************************/
20
21
declare(strict_types=1);
22
23
namespace DBD\Entity;
24
25
use DBD\Entity\Common\EntityException;
26
use ReflectionException;
27
28
/**
29
 * Trait MapperTrait
30
 *
31
 * @package DBD\Entity
32
 */
33
trait MapperTrait
34
{
35
    /**
36
     * @return Complex[]
37
     */
38
    public function getComplex(): array
39
    {
40
        return MapperCache::me()->complex[$this->name()] ?? [];
0 ignored issues
show
Bug introduced by
It seems like name() 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

40
        return MapperCache::me()->complex[$this->/** @scrutinizer ignore-call */ name()] ?? [];
Loading history...
41
    }
42
43
    /**
44
     * @return Column[]
45
     */
46
    public function getColumns(): array
47
    {
48
        return MapperCache::me()->columns[$this->name()] ?? [];
49
    }
50
51
    /**
52
     * @return Constraint[]
53
     */
54
    public function getConstraints(): array
55
    {
56
        return MapperCache::me()->constraints[$this->name()];
57
    }
58
59
    /**
60
     * @return Embedded[]
61
     */
62
    public function getEmbedded(): array
63
    {
64
        return MapperCache::me()->embedded[$this->name()] ?? [];
65
    }
66
67
    /**
68
     * @param string $originName
69
     *
70
     * @return Column
71
     * @throws EntityException
72
     */
73
    public function findColumnByOriginName(string $originName): Column
74
    {
75
        foreach ($this->getColumns() as $column) {
76
            if ($column->name == $originName) {
77
                return $column;
78
            }
79
        }
80
81
        throw new EntityException(sprintf("Can't find origin column '%s' in %s", $originName, get_class($this)));
82
    }
83
84
    /**
85
     * @param Column $column
86
     * @return int|string
87
     * @throws EntityException
88
     */
89
    public function getVarNameByColumn(Column $column): int|string
90
    {
91
        foreach ($this->getOriginFieldNames() as $varName => $originFieldName) {
92
            if ($originFieldName == $column->name) {
93
                return $varName;
94
            }
95
        }
96
97
        throw new EntityException(sprintf("Seems column '%s' does not belong to this mapper", $column->name));
98
    }
99
100
    /**
101
     * @return Column[] that is associative array where key is property name
102
     */
103
    public function getPrimaryKey(): array {
104
        $keys = [];
105
106
        foreach ($this->getColumns() as $columnName => $column) {
107
            if (isset($column->{Column::KEY}) and $column->{Column::KEY} === true) {
108
                $keys[$columnName] = $column;
109
            }
110
        }
111
112
        return $keys;
113
    }
114
115
    /**
116
     * @return array<string, string>
117
     */
118
    public function getOriginFieldNames(): array
119
    {
120
        $thisName = $this->name();
121
122
        if (!isset(MapperCache::me()->originFieldNames[$thisName])) {
123
            MapperCache::me()->originFieldNames[$thisName] = [];
124
125
            foreach ($this->getColumns() as $columnName => $column) {
126
                MapperCache::me()->originFieldNames[$thisName][$columnName] = $column->name;
127
            }
128
        }
129
130
        return MapperCache::me()->originFieldNames[$thisName];
131
    }
132
133
    /**
134
     * @return Table
135
     * @throws ReflectionException
136
     * @throws EntityException
137
     */
138
    public function getTable(): Table
139
    {
140
        $thisName = $this->name();
141
142
        if (!isset(MapperCache::me()->tables[$thisName])) {
0 ignored issues
show
Bug introduced by
The property tables does not seem to exist on DBD\Common\Singleton.
Loading history...
143
            $table = new Table();
144
145
            $table->name = $this->getTableName();
0 ignored issues
show
Bug introduced by
The method getTableName() does not exist on DBD\Entity\MapperTrait. Did you maybe mean getTable()? ( Ignorable by Annotation )

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

145
            /** @scrutinizer ignore-call */ 
146
            $table->name = $this->getTableName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
            $table->scheme = $this->getScheme();
0 ignored issues
show
Bug introduced by
It seems like getScheme() 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

146
            /** @scrutinizer ignore-call */ 
147
            $table->scheme = $this->getScheme();
Loading history...
147
            $table->columns = $this->getColumns();
148
            $table->constraints = $this->getConstraints();
149
            $table->keys = $this->getPrimaryKey();
150
            $table->annotation = $this->getAnnotation();
0 ignored issues
show
Bug introduced by
It seems like getAnnotation() 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

150
            /** @scrutinizer ignore-call */ 
151
            $table->annotation = $this->getAnnotation();
Loading history...
151
152
            MapperCache::me()->table[$thisName] = $table;
153
        }
154
155
        return MapperCache::me()->table[$thisName];
156
    }
157
}
158