Passed
Push — master ( 36d135...97e9b8 )
by Sébastien
08:13
created

PropertyInfo::isNullable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Bdf\Prime\Mapper\Info;
4
5
use Bdf\Prime\Mapper\Metadata;
6
use Bdf\Prime\Types\PhpTypeInterface;
7
use Bdf\Prime\Types\TypeInterface;
8
use Bdf\Prime\Types\TypesRegistryInterface;
9
10
/**
11
 * PropertyInfo
12
 */
13
class PropertyInfo implements InfoInterface
14
{
15
    /**
16
     * The property name
17
     *
18
     * @var string
19
     */
20
    protected $name;
21
    
22
    /**
23
     * The metadata from the metadata object
24
     *
25
     * @var array
26
     */
27
    protected $metadata;
28
    
29
    /**
30
     * The metadata from the metadata object
31
     *
32
     * @var array
33
     */
34
    protected $relation;
35
36
    /**
37
     * The types registry
38
     *
39
     * @var TypesRegistryInterface
40
     */
41
    protected $typesRegistry;
42
43
44
    /**
45
     * Constructor
46
     *
47
     * @param string $name                            The property name
48
     * @param array $metadata                         The property metadata or the relation metadata
49
     * @param TypesRegistryInterface $typesRegistry   The types registry
50
     */
51 71
    public function __construct($name, array $metadata = [], TypesRegistryInterface $typesRegistry = null)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
52
    {
53 71
        $this->name = $name;
54 71
        $this->metadata = $metadata;
55 71
        $this->typesRegistry = $typesRegistry;
56 71
    }
57
    
58
    /**
59
     * {@inheritdoc}
60
     */
61 45
    public function name()
62
    {
63 45
        return $this->name;
64
    }
65
    
66
    /**
67
     * Get the property type
68
     * 
69
     * @return string
70
     */
71 54
    public function type()
72
    {
73 54
        return $this->metadata['type'];
74
    }
75
76
    /**
77
     * Get the property alias
78
     *
79
     * @return string|null
80
     */
81 1
    public function alias()
82
    {
83 1
        return $this->metadata['field'] !== $this->metadata['attribute']
84 1
            ? $this->metadata['field']
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
Loading history...
85 1
            : null;
86
    }
87
    
88
    /**
89
     * Get the php type of the property
90
     * Class should have '\' char to be resolved with namespace.
91
     * 
92
     * @return string
93
     */
94 53
    public function phpType()
95
    {
96 53
        if (isset($this->metadata['phpOptions']['className'])) {
97 7
            return '\\'.ltrim($this->metadata['phpOptions']['className'], '\\');
98
        }
99
100 51
        $type = $this->type();
101
102 51
        if ($this->typesRegistry === null || !$this->typesRegistry->has($type)) {
103 1
            return $type;
104
        }
105
106 50
        return $this->typesRegistry->get($type)->phpType();
107
    }
108
    
109
    /**
110
     * Check whether the property is a primary key
111
     * 
112
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
113
     */
114 70
    public function isPrimary()
115
    {
116 70
        return $this->metadata['primary'] !== null;
117
    }
118
119
    /**
120
     * Check if the property value is auto-generated (auto increment or sequence)
121
     *
122
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
123
     */
124 9
    public function isGenerated(): bool
125
    {
126 9
        return in_array($this->metadata['primary'], [Metadata::PK_AUTOINCREMENT, Metadata::PK_SEQUENCE], true);
127
    }
128
129
    /**
130
     * Check if the property can be null
131
     *
132
     * - Marked as nullable on mapper
133
     * - It's value is auto-generated
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
134
     *
135
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
136
     */
137 8
    public function isNullable(): bool
138
    {
139 8
        return !empty($this->metadata['nillable']) || $this->isGenerated();
0 ignored issues
show
Coding Style introduced by
Boolean operators are not allowed outside of control structure conditions
Loading history...
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 70
    public function isEmbedded()
146
    {
147 70
        return $this->metadata['embedded'] !== null;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 70
    public function belongsToRoot()
154
    {
155 70
        return !$this->isEmbedded();
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 40
    public function isObject()
162
    {
163 40
        return false;
164
    }
165
166
    /**
167
     * Check whether the property is a date time object
168
     * 
169
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
170
     */
171 29
    public function isDateTime()
172
    {
173 29
        return is_subclass_of($this->phpType(), \DateTimeInterface::class);
174
    }
175
176
    /**
177
     * Gets the date timezone
178
     *
179
     * @return string|null
180
     */
181 4
    public function getTimezone(): ?string
182
    {
183 4
        if (!$this->isDateTime()) {
184 1
            return null;
185
        }
186
187 4
        if (isset($this->metadata['phpOptions']['timezone'])) {
188 4
            return $this->metadata['phpOptions']['timezone'];
189
        }
190
191
        /** @var \DateTimeZone $timezone */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
192 4
        $timezone = $this->getType()->getTimezone();
0 ignored issues
show
Bug introduced by
The method getTimezone() does not exist on Bdf\Prime\Types\TypeInterface. It seems like you code against a sub-type of Bdf\Prime\Types\TypeInterface such as Bdf\Prime\Platform\Sql\T...AbstractSqlDateTimeType or Bdf\Prime\Types\TimestampType or Bdf\Prime\Types\AbstractDateTimeType. ( Ignorable by Annotation )

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

192
        $timezone = $this->getType()->/** @scrutinizer ignore-call */ getTimezone();
Loading history...
193
194 4
        return $timezone ? $timezone->getName() : null;
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
introduced by
$timezone is of type DateTimeZone, thus it always evaluated to true.
Loading history...
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 29
    public function isArray()
201
    {
202 29
        return $this->phpType() === PhpTypeInterface::TARRAY;
203
    }
204
    
205
    /**
206
     * Check whether the property has a default value
207
     * 
208
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
209
     */
210 29
    public function hasDefault()
211
    {
212 29
        return $this->getDefault() !== null;
213
    }
214
    
215
    /**
216
     * Get the default value
217
     * 
218
     * @return mixed
219
     */
220 30
    public function getDefault()
221
    {
222 30
        return $this->metadata['default'];
223
    }
224
    
225
    /**
226
     * Get the default value
227
     * 
228
     * @param mixed $value
229
     * @param bool  $toPhp
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
230
     * @param array $fieldOptions
231
     *
232
     * @return mixed
233
     */
234 6
    public function convert($value, $toPhp = true, array $fieldOptions = [])
235
    {
236 6
        if ($toPhp) {
237 5
            return $this->getType()->fromDatabase($value, $fieldOptions);
238
        }
239
240 1
        return $this->getType()->toDatabase($value);
241
    }
242
243
    /**
244
     * Get the type object
245
     *
246
     * @return TypeInterface
247
     */
248 7
    protected function getType()
249
    {
250 7
        return $this->typesRegistry->get($this->type());
251
    }
252
}
253