EntityField   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 20
eloc 45
c 3
b 0
f 0
dl 0
loc 213
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimitiveType() 0 3 1
A setFieldType() 0 3 1
A primitiveTypeToEdmType() 0 6 2
A getIsNullable() 0 3 1
A getReadOnly() 0 3 1
A getCreateOnly() 0 3 1
A getIsKeyField() 0 3 1
A getName() 0 3 1
A getDefaultValue() 0 3 1
A setPrimitiveType() 0 5 2
A setCreateOnly() 0 3 1
A getEdmFieldType() 0 3 1
A setName() 0 3 1
A setDefaultValue() 0 3 1
A setIsKeyField() 0 3 1
A setReadOnly() 0 3 1
A getFieldType() 0 3 1
A setIsNullable() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities;
6
7
use POData\Providers\Metadata\Type\EdmPrimitiveType;
8
use POData\Providers\Metadata\Type\TypeCode;
9
10
/**
11
 * Class EntityField
12
 * @package AlgoWeb\PODataLaravel\Models\ObjectMap\Entities
13
 */
14
class EntityField
15
{
16
    /**
17
     * @var string
18
     */
19
    private $name;
20
21
    /**
22
     * @var EntityFieldType;
23
     */
24
    private $fieldType;
25
26
    /**
27
     * @var bool
28
     */
29
    private $isNullable = false;
30
31
    /**
32
     * @var mixed
33
     */
34
    private $defaultValue;
35
36
    /**
37
     * @var bool
38
     */
39
    private $readOnly = false;
40
41
    /**
42
     * @var bool
43
     */
44
    private $createOnly = false;
45
46
    /**
47
     * @var bool
48
     */
49
    private $isKeyField = false;
50
51
    /**
52
     * @var EntityFieldPrimitiveType
53
     */
54
    private $primitiveType;
55
    /**
56
     * @var EdmPrimitiveType
57
     */
58
    private $edmFieldType;
59
60
    /**
61
     * @return EdmPrimitiveType
62
     */
63
    public function getEdmFieldType()
64
    {
65
        return $this->edmFieldType;
66
    }
67
68
    /**
69
     * @return \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldPrimitiveType
70
     */
71
    public function getPrimitiveType()
72
    {
73
        return $this->primitiveType;
74
    }
75
76
    /**
77
     * @param \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldPrimitiveType $primitiveType
78
     */
79
    public function setPrimitiveType(EntityFieldPrimitiveType $primitiveType): void
80
    {
81
        $this->primitiveType = $primitiveType;
82
        $rawType             = $this->primitiveTypeToEdmType($primitiveType);
83
        $this->edmFieldType  = 'stream' === $rawType ? $rawType : new EdmPrimitiveType($rawType);
0 ignored issues
show
Documentation Bug introduced by
It seems like 'stream' === $rawType ? ...PrimitiveType($rawType) can also be of type string. However, the property $edmFieldType is declared as type POData\Providers\Metadata\Type\EdmPrimitiveType. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getName()
90
    {
91
        return $this->name;
92
    }
93
94
    /**
95
     * @param string $name
96
     */
97
    public function setName($name): void
98
    {
99
        $this->name = $name;
100
    }
101
102
    /**
103
     * @return EntityFieldType
104
     */
105
    public function getFieldType()
106
    {
107
        return $this->fieldType;
108
    }
109
110
    /**
111
     * @param EntityFieldType $fieldType
112
     */
113
    public function setFieldType(EntityFieldType $fieldType): void
114
    {
115
        $this->fieldType = $fieldType;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    public function getIsNullable(): bool
122
    {
123
        return $this->isNullable;
124
    }
125
126
    /**
127
     * @param bool $isNullable
128
     */
129
    public function setIsNullable($isNullable): void
130
    {
131
        $this->isNullable = boolval($isNullable);
132
    }
133
134
    /**
135
     * @return mixed
136
     */
137
    public function getDefaultValue()
138
    {
139
        return $this->defaultValue;
140
    }
141
142
    /**
143
     * @param mixed $defaultValue
144
     */
145
    public function setDefaultValue($defaultValue): void
146
    {
147
        $this->defaultValue = $defaultValue;
148
    }
149
150
    /**
151
     * @return bool
152
     */
153
    public function getReadOnly()
154
    {
155
        return $this->readOnly;
156
    }
157
158
    /**
159
     * @param bool $readOnly
160
     */
161
    public function setReadOnly($readOnly): void
162
    {
163
        $this->readOnly = boolval($readOnly);
164
    }
165
166
    /**
167
     * @return bool
168
     */
169
    public function getCreateOnly()
170
    {
171
        return $this->createOnly;
172
    }
173
174
    /**
175
     * @param bool $createOnly
176
     */
177
    public function setCreateOnly($createOnly): void
178
    {
179
        $this->createOnly = boolval($createOnly);
180
    }
181
182
    /**
183
     * @return bool
184
     */
185
    public function getIsKeyField()
186
    {
187
        return $this->isKeyField;
188
    }
189
190
    /**
191
     * @param bool $keyField
192
     */
193
    public function setIsKeyField($keyField): void
194
    {
195
        $this->isKeyField = boolval($keyField);
196
    }
197
198
    /**
199
     * @var array<string, int|string>
200
     */
201
    private static $primitiveToEdmMapping = [
202
        EntityFieldPrimitiveType::BINARY => EdmPrimitiveType::BINARY,
203
        EntityFieldPrimitiveType::BIGINT => EdmPrimitiveType::INT64,
204
        EntityFieldPrimitiveType::INTEGER => EdmPrimitiveType::INT32,
205
        EntityFieldPrimitiveType::STRING => EdmPrimitiveType::STRING,
206
        EntityFieldPrimitiveType::DATE => EdmPrimitiveType::DATETIME,
207
        EntityFieldPrimitiveType::DATETIME => EdmPrimitiveType::DATETIME,
208
        EntityFieldPrimitiveType::DATETIMETZ => EdmPrimitiveType::DATETIME,
209
        EntityFieldPrimitiveType::FLOAT => EdmPrimitiveType::SINGLE,
210
        EntityFieldPrimitiveType::DECIMAL => EdmPrimitiveType::DECIMAL,
211
        EntityFieldPrimitiveType::TEXT => EdmPrimitiveType::STRING,
212
        EntityFieldPrimitiveType::BOOLEAN => EdmPrimitiveType::BOOLEAN,
213
        EntityFieldPrimitiveType::BLOB => 'stream'
214
    ];
215
216
    /**
217
     * @param \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldPrimitiveType $primitiveType
218
     *
219
     * @return int|string
220
     */
221
    private function primitiveTypeToEdmType(EntityFieldPrimitiveType $primitiveType)
222
    {
223
        $value = $primitiveType->getValue();
224
225
        return array_key_exists($value, self::$primitiveToEdmMapping) ?
226
            self::$primitiveToEdmMapping[$value] : EdmPrimitiveType::STRING;
227
    }
228
}
229