Skip to content

Commit

Permalink
Chore/improve default types (#45)
Browse files Browse the repository at this point in the history
* support null,int,float defaults

* support empty string default
  • Loading branch information
nick-zh authored Jan 7, 2022
1 parent a256373 commit 85a99f6
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/Parser/ClassPropertyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,42 @@ private function getTypeFromDocComment(array $docComments): ?string

/**
* @param array<string, mixed> $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;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/PhpClass/PhpClassPropertyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 90 additions & 0 deletions tests/Integration/Parser/ClassPropertyParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Integration\Parser;

use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser;
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;

/**
* @covers PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser
*/
class ClassPropertyParserTest extends TestCase
{
public function testNullDefaultProperty(): void
{
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$parser->setCode('
<?php
class foo {
/**
* @avro-default null
*/
public $bla;
}
');
$properties = $parser->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('
<?php
class foo {
/**
* @avro-default 1
*/
public $bla;
}
');
$properties = $parser->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('
<?php
class foo {
/**
* @avro-default 1.2
*/
public $bla;
}
');
$properties = $parser->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('
<?php
class foo {
/**
* @avro-default empty-string-default
*/
public $bla;
}
');
$properties = $parser->getProperties();
self::assertEquals(1, count($properties));
self::assertEquals('', $properties[0]->getPropertyDefault());
}
}

0 comments on commit 85a99f6

Please sign in to comment.