From 4180d29b65c8bc416520256d916c31b44af98cec Mon Sep 17 00:00:00 2001 From: Anthony Kuske Date: Fri, 29 May 2020 19:16:43 +0100 Subject: [PATCH 1/2] Set nullable fields as nullable in the docblock --- src/Coders/Model/Model.php | 30 +++++++---- tests/Coders/Console/Model/ModelTest.php | 67 ++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 tests/Coders/Console/Model/ModelTest.php diff --git a/src/Coders/Model/Model.php b/src/Coders/Model/Model.php index 25c03849..09471774 100644 --- a/src/Coders/Model/Model.php +++ b/src/Coders/Model/Model.php @@ -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()) { @@ -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; } /** diff --git a/tests/Coders/Console/Model/ModelTest.php b/tests/Coders/Console/Model/ModelTest.php new file mode 100644 index 00000000..f265afd9 --- /dev/null +++ b/tests/Coders/Console/Model/ModelTest.php @@ -0,0 +1,67 @@ + [ + '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); + } +} From 4411fd258778aaa9bd5f9f0fde8954255940e07d Mon Sep 17 00:00:00 2001 From: Anthony Kuske Date: Fri, 29 May 2020 19:20:37 +0100 Subject: [PATCH 2/2] Style fixes --- tests/Coders/Console/Model/ModelTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Coders/Console/Model/ModelTest.php b/tests/Coders/Console/Model/ModelTest.php index f265afd9..f981faa7 100644 --- a/tests/Coders/Console/Model/ModelTest.php +++ b/tests/Coders/Console/Model/ModelTest.php @@ -12,40 +12,40 @@ public function dataForTestPhpTypeHint() 'Non-nullable int' => [ 'castType' => 'int', 'nullable' => false, - 'expect' => 'int' + 'expect' => 'int', ], 'Nullable int' => [ 'castType' => 'int', 'nullable' => true, - 'expect' => 'int|null' + 'expect' => 'int|null', ], 'Non-nullable json' => [ 'castType' => 'json', 'nullable' => false, - 'expect' => 'array' + 'expect' => 'array', ], 'Nullable json' => [ 'castType' => 'json', 'nullable' => true, - 'expect' => 'array|null' + 'expect' => 'array|null', ], 'Non-nullable date' => [ 'castType' => 'date', 'nullable' => false, - 'expect' => '\Carbon\Carbon' + 'expect' => '\Carbon\Carbon', ], 'Nullable date' => [ 'castType' => 'date', 'nullable' => true, - 'expect' => '\Carbon\Carbon|null' - ] + 'expect' => '\Carbon\Carbon|null', + ], ]; } /** * @dataProvider dataForTestPhpTypeHint * - * @param string$castType + * @param string $castType * @param bool $nullable * @param string $expect */