diff --git a/src/Parser/ClassPropertyParser.php b/src/Parser/ClassPropertyParser.php index c90e08f..45cd171 100644 --- a/src/Parser/ClassPropertyParser.php +++ b/src/Parser/ClassPropertyParser.php @@ -124,11 +124,42 @@ private function getTypeFromDocComment(array $docComments): ?string /** * @param array $docComments - * @return string + * @return string|int|float|null */ - private function getDefaultFromDocComment(array $docComments): string + private function getDefaultFromDocComment(array $docComments) { - return $docComments['avro-default'] ?? PhpClassPropertyInterface::NO_DEFAULT; + if (false === isset($docComments['avro-default'])) { + return PhpClassPropertyInterface::NO_DEFAULT; + } + + if (PhpClassPropertyInterface::EMPTY_STRING_DEFAULT === $docComments['avro-default']) { + return ''; + } + + if (true === is_string($docComments['avro-default']) && true === is_numeric($docComments['avro-default'])) { + $docComments['avro-default'] = $this->convertStringToNumber($docComments['avro-default']); + } + + if ('null' === $docComments['avro-default']) { + return null; + } + + return $docComments['avro-default']; + } + + /** + * @param string $number + * @return float|int + */ + private function convertStringToNumber(string $number) + { + $int = (int) $number; + + if (strval($int) == $number) { + return $int; + } + + return (float) $number; } /** diff --git a/src/PhpClass/PhpClassPropertyInterface.php b/src/PhpClass/PhpClassPropertyInterface.php index b8c6fce..5a4d716 100644 --- a/src/PhpClass/PhpClassPropertyInterface.php +++ b/src/PhpClass/PhpClassPropertyInterface.php @@ -7,6 +7,8 @@ interface PhpClassPropertyInterface { public const NO_DEFAULT = 'there-was-no-default-set'; + public const EMPTY_STRING_DEFAULT = 'empty-string-default'; + /** * @return mixed diff --git a/tests/Integration/Parser/ClassPropertyParserTest.php b/tests/Integration/Parser/ClassPropertyParserTest.php new file mode 100644 index 0000000..751e11c --- /dev/null +++ b/tests/Integration/Parser/ClassPropertyParserTest.php @@ -0,0 +1,90 @@ +create(ParserFactory::PREFER_PHP7), $propertyParser); + $parser->setCode(' + getProperties(); + self::assertEquals(1, count($properties)); + self::assertNull($properties[0]->getPropertyDefault()); + } + + public function testIntDefaultProperty(): void + { + $propertyParser = new ClassPropertyParser(new DocCommentParser()); + $parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser); + $parser->setCode(' + getProperties(); + self::assertEquals(1, count($properties)); + self::assertIsInt($properties[0]->getPropertyDefault()); + } + + public function testFloatDefaultProperty(): void + { + $propertyParser = new ClassPropertyParser(new DocCommentParser()); + $parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser); + $parser->setCode(' + getProperties(); + self::assertEquals(1, count($properties)); + self::assertIsFloat($properties[0]->getPropertyDefault()); + } + + public function testEmptyStringDefaultProperty(): void + { + $propertyParser = new ClassPropertyParser(new DocCommentParser()); + $parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser); + $parser->setCode(' + getProperties(); + self::assertEquals(1, count($properties)); + self::assertEquals('', $properties[0]->getPropertyDefault()); + } +}