|
4 | 4 |
|
5 | 5 | namespace PhpKafka\PhpAvroSchemaGenerator\Converter;
|
6 | 6 |
|
7 |
| -use PHP_CodeSniffer\Tokenizers\PHP; |
8 | 7 | use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro;
|
9 | 8 | use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParserInterface;
|
10 | 9 | use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClass;
|
11 | 10 | use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface;
|
12 | 11 | use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty;
|
13 | 12 | use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
|
14 | 13 |
|
15 |
| -class PhpClassConverter implements PhpClassConverterInterface |
| 14 | +final class PhpClassConverter implements PhpClassConverterInterface |
16 | 15 | {
|
17 | 16 | private ClassParserInterface $parser;
|
18 | 17 |
|
19 | 18 | /**
|
20 | 19 | * @var array<string,int>
|
21 | 20 | */
|
22 |
| - private array $typesToSkip = [ |
| 21 | + private array $singleTypesToSkip = [ |
23 | 22 | 'null' => 1,
|
24 | 23 | 'object' => 1,
|
25 | 24 | 'callable' => 1,
|
26 | 25 | 'resource' => 1,
|
27 | 26 | 'mixed' => 1
|
28 | 27 | ];
|
29 | 28 |
|
| 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 | + |
30 | 39 | /**
|
31 | 40 | * @param ClassParserInterface $parser
|
32 | 41 | */
|
@@ -99,13 +108,17 @@ private function getConvertedType(string $type)
|
99 | 108 | return $this->getConvertedUnionType($types);
|
100 | 109 | }
|
101 | 110 |
|
102 |
| - private function getFullTypeName(string $type): ?string |
| 111 | + private function getFullTypeName(string $type, bool $isUnionType = false): ?string |
103 | 112 | {
|
| 113 | + |
104 | 114 | if (true === isset(Avro::MAPPED_TYPES[$type])) {
|
105 | 115 | $type = Avro::MAPPED_TYPES[$type];
|
106 | 116 | }
|
107 | 117 |
|
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 | + ) { |
109 | 122 | return null;
|
110 | 123 | }
|
111 | 124 |
|
@@ -135,12 +148,8 @@ private function getConvertedUnionType(array $types): array
|
135 | 148 | $convertedUnionType = [];
|
136 | 149 |
|
137 | 150 | 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; |
144 | 153 | }
|
145 | 154 | }
|
146 | 155 |
|
|
0 commit comments