Skip to content

Commit

Permalink
Merge pull request #168 from antriver/nullable-fields
Browse files Browse the repository at this point in the history
Set nullable fields as nullable in the docblock
  • Loading branch information
Cristian Llanos authored May 31, 2020
2 parents 2fe0684 + 4411fd2 commit f4a95c8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/Coders/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ protected function parseColumn(Fluent $column)
}

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

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

/**
* @param string $castType
* @param bool $nullable
*
* @todo Make tests
*
* @return string
*/
public function phpTypeHint($castType)
public function phpTypeHint($castType, $nullable)
{
$type = $castType;

switch ($castType) {
case 'object':
return '\stdClass';
$type = '\stdClass';
break;
case 'array':
case 'json':
return 'array';
$type = 'array';
break;
case 'collection':
return '\Illuminate\Support\Collection';
$type = '\Illuminate\Support\Collection';
break;
case 'date':
return '\Carbon\Carbon';
$type = '\Carbon\Carbon';
break;
case 'binary':
return 'string';
default:
return $castType;
$type = 'string';
break;
}

if ($nullable) {
return $type.'|null';
}

return $type;
}

/**
Expand Down
67 changes: 67 additions & 0 deletions tests/Coders/Console/Model/ModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

use Reliese\Coders\Model\Factory;
use Reliese\Coders\Model\Model;
use Reliese\Meta\Blueprint;

class ModelTest extends TestCase
{
public function dataForTestPhpTypeHint()
{
return [
'Non-nullable int' => [
'castType' => 'int',
'nullable' => false,
'expect' => 'int',
],
'Nullable int' => [
'castType' => 'int',
'nullable' => true,
'expect' => 'int|null',
],
'Non-nullable json' => [
'castType' => 'json',
'nullable' => false,
'expect' => 'array',
],
'Nullable json' => [
'castType' => 'json',
'nullable' => true,
'expect' => 'array|null',
],
'Non-nullable date' => [
'castType' => 'date',
'nullable' => false,
'expect' => '\Carbon\Carbon',
],
'Nullable date' => [
'castType' => 'date',
'nullable' => true,
'expect' => '\Carbon\Carbon|null',
],
];
}

/**
* @dataProvider dataForTestPhpTypeHint
*
* @param string $castType
* @param bool $nullable
* @param string $expect
*/
public function testPhpTypeHint($castType, $nullable, $expect)
{
$model = new Model(
new Blueprint('test', 'test', 'test'),
new Factory(
\Mockery::mock(\Illuminate\Database\DatabaseManager::class),
\Mockery::mock(Illuminate\Filesystem\Filesystem::class),
\Mockery::mock(\Reliese\Support\Classify::class),
new \Reliese\Coders\Model\Config()
)
);

$result = $model->phpTypeHint($castType, $nullable);
$this->assertSame($expect, $result);
}
}

0 comments on commit f4a95c8

Please sign in to comment.