Skip to content

Commit f4a95c8

Browse files
author
Cristian Llanos
authored
Merge pull request #168 from antriver/nullable-fields
Set nullable fields as nullable in the docblock
2 parents 2fe0684 + 4411fd2 commit f4a95c8

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

src/Coders/Model/Model.php

+21-9
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ protected function parseColumn(Fluent $column)
292292
}
293293

294294
// Track PHP type hints
295-
$hint = $this->phpTypeHint($cast);
295+
$hint = $this->phpTypeHint($cast, $column->nullable);
296296
$this->properties[$column->name] = $hint;
297297

298298
if ($column->name == $this->getPrimaryKey()) {
@@ -333,28 +333,40 @@ public function makeRelationModel(Fluent $relation)
333333

334334
/**
335335
* @param string $castType
336+
* @param bool $nullable
336337
*
337338
* @todo Make tests
338339
*
339340
* @return string
340341
*/
341-
public function phpTypeHint($castType)
342+
public function phpTypeHint($castType, $nullable)
342343
{
344+
$type = $castType;
345+
343346
switch ($castType) {
344347
case 'object':
345-
return '\stdClass';
348+
$type = '\stdClass';
349+
break;
346350
case 'array':
347351
case 'json':
348-
return 'array';
352+
$type = 'array';
353+
break;
349354
case 'collection':
350-
return '\Illuminate\Support\Collection';
355+
$type = '\Illuminate\Support\Collection';
356+
break;
351357
case 'date':
352-
return '\Carbon\Carbon';
358+
$type = '\Carbon\Carbon';
359+
break;
353360
case 'binary':
354-
return 'string';
355-
default:
356-
return $castType;
361+
$type = 'string';
362+
break;
363+
}
364+
365+
if ($nullable) {
366+
return $type.'|null';
357367
}
368+
369+
return $type;
358370
}
359371

360372
/**
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
use Reliese\Coders\Model\Factory;
4+
use Reliese\Coders\Model\Model;
5+
use Reliese\Meta\Blueprint;
6+
7+
class ModelTest extends TestCase
8+
{
9+
public function dataForTestPhpTypeHint()
10+
{
11+
return [
12+
'Non-nullable int' => [
13+
'castType' => 'int',
14+
'nullable' => false,
15+
'expect' => 'int',
16+
],
17+
'Nullable int' => [
18+
'castType' => 'int',
19+
'nullable' => true,
20+
'expect' => 'int|null',
21+
],
22+
'Non-nullable json' => [
23+
'castType' => 'json',
24+
'nullable' => false,
25+
'expect' => 'array',
26+
],
27+
'Nullable json' => [
28+
'castType' => 'json',
29+
'nullable' => true,
30+
'expect' => 'array|null',
31+
],
32+
'Non-nullable date' => [
33+
'castType' => 'date',
34+
'nullable' => false,
35+
'expect' => '\Carbon\Carbon',
36+
],
37+
'Nullable date' => [
38+
'castType' => 'date',
39+
'nullable' => true,
40+
'expect' => '\Carbon\Carbon|null',
41+
],
42+
];
43+
}
44+
45+
/**
46+
* @dataProvider dataForTestPhpTypeHint
47+
*
48+
* @param string $castType
49+
* @param bool $nullable
50+
* @param string $expect
51+
*/
52+
public function testPhpTypeHint($castType, $nullable, $expect)
53+
{
54+
$model = new Model(
55+
new Blueprint('test', 'test', 'test'),
56+
new Factory(
57+
\Mockery::mock(\Illuminate\Database\DatabaseManager::class),
58+
\Mockery::mock(Illuminate\Filesystem\Filesystem::class),
59+
\Mockery::mock(\Reliese\Support\Classify::class),
60+
new \Reliese\Coders\Model\Config()
61+
)
62+
);
63+
64+
$result = $model->phpTypeHint($castType, $nullable);
65+
$this->assertSame($expect, $result);
66+
}
67+
}

0 commit comments

Comments
 (0)