Skip to content

Commit ec10e84

Browse files
authored
allow null in union types, make classes final (#44)
* allow null in union types, make classes final * remove unused use statement * unify if
1 parent 85a99f6 commit ec10e84

File tree

7 files changed

+25
-17
lines changed

7 files changed

+25
-17
lines changed

src/Converter/PhpClassConverter.php

+20-11
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,38 @@
44

55
namespace PhpKafka\PhpAvroSchemaGenerator\Converter;
66

7-
use PHP_CodeSniffer\Tokenizers\PHP;
87
use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro;
98
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParserInterface;
109
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClass;
1110
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface;
1211
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty;
1312
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
1413

15-
class PhpClassConverter implements PhpClassConverterInterface
14+
final class PhpClassConverter implements PhpClassConverterInterface
1615
{
1716
private ClassParserInterface $parser;
1817

1918
/**
2019
* @var array<string,int>
2120
*/
22-
private array $typesToSkip = [
21+
private array $singleTypesToSkip = [
2322
'null' => 1,
2423
'object' => 1,
2524
'callable' => 1,
2625
'resource' => 1,
2726
'mixed' => 1
2827
];
2928

29+
/**
30+
* @var array<string,int>
31+
*/
32+
private array $unionTypesToSkip = [
33+
'object' => 1,
34+
'callable' => 1,
35+
'resource' => 1,
36+
'mixed' => 1
37+
];
38+
3039
/**
3140
* @param ClassParserInterface $parser
3241
*/
@@ -99,13 +108,17 @@ private function getConvertedType(string $type)
99108
return $this->getConvertedUnionType($types);
100109
}
101110

102-
private function getFullTypeName(string $type): ?string
111+
private function getFullTypeName(string $type, bool $isUnionType = false): ?string
103112
{
113+
104114
if (true === isset(Avro::MAPPED_TYPES[$type])) {
105115
$type = Avro::MAPPED_TYPES[$type];
106116
}
107117

108-
if (true === isset($this->typesToSkip[$type])) {
118+
if (
119+
(false === $isUnionType && true === isset($this->singleTypesToSkip[$type]))
120+
|| (true === $isUnionType && true === isset($this->unionTypesToSkip[$type]))
121+
) {
109122
return null;
110123
}
111124

@@ -135,12 +148,8 @@ private function getConvertedUnionType(array $types): array
135148
$convertedUnionType = [];
136149

137150
foreach ($types as $type) {
138-
if (true === isset($this->typesToSkip[$type])) {
139-
continue;
140-
}
141-
142-
if (false === $this->isArrayType($type)) {
143-
$convertedUnionType[] = $this->getFullTypeName($type);
151+
if (false === $this->isArrayType($type) && null !== $formattedType = $this->getFullTypeName($type, true)) {
152+
$convertedUnionType[] = $formattedType;
144153
}
145154
}
146155

src/Parser/ClassParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use ReflectionClass;
1818
use ReflectionException;
1919

20-
class ClassParser implements ClassParserInterface
20+
final class ClassParser implements ClassParserInterface
2121
{
2222
private ClassPropertyParserInterface $propertyParser;
2323
private Parser $parser;

src/Parser/ClassPropertyParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use PhpParser\Node\UnionType;
1515
use RuntimeException;
1616

17-
class ClassPropertyParser implements ClassPropertyParserInterface
17+
final class ClassPropertyParser implements ClassPropertyParserInterface
1818
{
1919
private DocCommentParserInterface $docParser;
2020

src/Parser/DocCommentParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PhpKafka\PhpAvroSchemaGenerator\Parser;
66

7-
class DocCommentParser implements DocCommentParserInterface
7+
final class DocCommentParser implements DocCommentParserInterface
88
{
99
/**
1010
* @param string $docComment

src/PhpClass/PhpClass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PhpKafka\PhpAvroSchemaGenerator\PhpClass;
66

7-
class PhpClass implements PhpClassInterface
7+
final class PhpClass implements PhpClassInterface
88
{
99
private string $classBody;
1010
private string $className;

src/PhpClass/PhpClassProperty.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PhpKafka\PhpAvroSchemaGenerator\Parser\PropertyAttributesInterface;
88

9-
class PhpClassProperty implements PhpClassPropertyInterface
9+
final class PhpClassProperty implements PhpClassPropertyInterface
1010
{
1111
/** @var mixed */
1212
private $propertyDefault;

src/Registry/ClassRegistry.php

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpKafka\PhpAvroSchemaGenerator\Converter\PhpClassConverterInterface;
99
use PhpKafka\PhpAvroSchemaGenerator\Exception\ClassRegistryException;
1010
use PhpKafka\PhpAvroSchemaGenerator\Parser\TokenParser;
11-
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClass;
1211
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface;
1312
use RecursiveDirectoryIterator;
1413
use RecursiveIteratorIterator;

0 commit comments

Comments
 (0)