Skip to content

Commit 85a99f6

Browse files
authored
Chore/improve default types (#45)
* support null,int,float defaults * support empty string default
1 parent a256373 commit 85a99f6

File tree

3 files changed

+126
-3
lines changed

3 files changed

+126
-3
lines changed

src/Parser/ClassPropertyParser.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,42 @@ private function getTypeFromDocComment(array $docComments): ?string
124124

125125
/**
126126
* @param array<string, mixed> $docComments
127-
* @return string
127+
* @return string|int|float|null
128128
*/
129-
private function getDefaultFromDocComment(array $docComments): string
129+
private function getDefaultFromDocComment(array $docComments)
130130
{
131-
return $docComments['avro-default'] ?? PhpClassPropertyInterface::NO_DEFAULT;
131+
if (false === isset($docComments['avro-default'])) {
132+
return PhpClassPropertyInterface::NO_DEFAULT;
133+
}
134+
135+
if (PhpClassPropertyInterface::EMPTY_STRING_DEFAULT === $docComments['avro-default']) {
136+
return '';
137+
}
138+
139+
if (true === is_string($docComments['avro-default']) && true === is_numeric($docComments['avro-default'])) {
140+
$docComments['avro-default'] = $this->convertStringToNumber($docComments['avro-default']);
141+
}
142+
143+
if ('null' === $docComments['avro-default']) {
144+
return null;
145+
}
146+
147+
return $docComments['avro-default'];
148+
}
149+
150+
/**
151+
* @param string $number
152+
* @return float|int
153+
*/
154+
private function convertStringToNumber(string $number)
155+
{
156+
$int = (int) $number;
157+
158+
if (strval($int) == $number) {
159+
return $int;
160+
}
161+
162+
return (float) $number;
132163
}
133164

134165
/**

src/PhpClass/PhpClassPropertyInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
interface PhpClassPropertyInterface
88
{
99
public const NO_DEFAULT = 'there-was-no-default-set';
10+
public const EMPTY_STRING_DEFAULT = 'empty-string-default';
11+
1012

1113
/**
1214
* @return mixed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Integration\Parser;
6+
7+
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser;
8+
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
9+
use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser;
10+
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
11+
use PhpParser\ParserFactory;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* @covers PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser
16+
*/
17+
class ClassPropertyParserTest extends TestCase
18+
{
19+
public function testNullDefaultProperty(): void
20+
{
21+
$propertyParser = new ClassPropertyParser(new DocCommentParser());
22+
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
23+
$parser->setCode('
24+
<?php
25+
class foo {
26+
/**
27+
* @avro-default null
28+
*/
29+
public $bla;
30+
}
31+
');
32+
$properties = $parser->getProperties();
33+
self::assertEquals(1, count($properties));
34+
self::assertNull($properties[0]->getPropertyDefault());
35+
}
36+
37+
public function testIntDefaultProperty(): void
38+
{
39+
$propertyParser = new ClassPropertyParser(new DocCommentParser());
40+
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
41+
$parser->setCode('
42+
<?php
43+
class foo {
44+
/**
45+
* @avro-default 1
46+
*/
47+
public $bla;
48+
}
49+
');
50+
$properties = $parser->getProperties();
51+
self::assertEquals(1, count($properties));
52+
self::assertIsInt($properties[0]->getPropertyDefault());
53+
}
54+
55+
public function testFloatDefaultProperty(): void
56+
{
57+
$propertyParser = new ClassPropertyParser(new DocCommentParser());
58+
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
59+
$parser->setCode('
60+
<?php
61+
class foo {
62+
/**
63+
* @avro-default 1.2
64+
*/
65+
public $bla;
66+
}
67+
');
68+
$properties = $parser->getProperties();
69+
self::assertEquals(1, count($properties));
70+
self::assertIsFloat($properties[0]->getPropertyDefault());
71+
}
72+
73+
public function testEmptyStringDefaultProperty(): void
74+
{
75+
$propertyParser = new ClassPropertyParser(new DocCommentParser());
76+
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
77+
$parser->setCode('
78+
<?php
79+
class foo {
80+
/**
81+
* @avro-default empty-string-default
82+
*/
83+
public $bla;
84+
}
85+
');
86+
$properties = $parser->getProperties();
87+
self::assertEquals(1, count($properties));
88+
self::assertEquals('', $properties[0]->getPropertyDefault());
89+
}
90+
}

0 commit comments

Comments
 (0)