From ec10e844f8caca31219f6e056cfacfb476bca85d Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 7 Jan 2022 20:02:47 +0100 Subject: [PATCH] allow null in union types, make classes final (#44) * allow null in union types, make classes final * remove unused use statement * unify if --- src/Converter/PhpClassConverter.php | 31 +++++++++++++++++++---------- src/Parser/ClassParser.php | 2 +- src/Parser/ClassPropertyParser.php | 2 +- src/Parser/DocCommentParser.php | 2 +- src/PhpClass/PhpClass.php | 2 +- src/PhpClass/PhpClassProperty.php | 2 +- src/Registry/ClassRegistry.php | 1 - 7 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/Converter/PhpClassConverter.php b/src/Converter/PhpClassConverter.php index a2f2778..542d1f5 100644 --- a/src/Converter/PhpClassConverter.php +++ b/src/Converter/PhpClassConverter.php @@ -4,7 +4,6 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Converter; -use PHP_CodeSniffer\Tokenizers\PHP; use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro; use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParserInterface; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClass; @@ -12,14 +11,14 @@ use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; -class PhpClassConverter implements PhpClassConverterInterface +final class PhpClassConverter implements PhpClassConverterInterface { private ClassParserInterface $parser; /** * @var array */ - private array $typesToSkip = [ + private array $singleTypesToSkip = [ 'null' => 1, 'object' => 1, 'callable' => 1, @@ -27,6 +26,16 @@ class PhpClassConverter implements PhpClassConverterInterface 'mixed' => 1 ]; + /** + * @var array + */ + private array $unionTypesToSkip = [ + 'object' => 1, + 'callable' => 1, + 'resource' => 1, + 'mixed' => 1 + ]; + /** * @param ClassParserInterface $parser */ @@ -99,13 +108,17 @@ private function getConvertedType(string $type) return $this->getConvertedUnionType($types); } - private function getFullTypeName(string $type): ?string + private function getFullTypeName(string $type, bool $isUnionType = false): ?string { + if (true === isset(Avro::MAPPED_TYPES[$type])) { $type = Avro::MAPPED_TYPES[$type]; } - if (true === isset($this->typesToSkip[$type])) { + if ( + (false === $isUnionType && true === isset($this->singleTypesToSkip[$type])) + || (true === $isUnionType && true === isset($this->unionTypesToSkip[$type])) + ) { return null; } @@ -135,12 +148,8 @@ private function getConvertedUnionType(array $types): array $convertedUnionType = []; foreach ($types as $type) { - if (true === isset($this->typesToSkip[$type])) { - continue; - } - - if (false === $this->isArrayType($type)) { - $convertedUnionType[] = $this->getFullTypeName($type); + if (false === $this->isArrayType($type) && null !== $formattedType = $this->getFullTypeName($type, true)) { + $convertedUnionType[] = $formattedType; } } diff --git a/src/Parser/ClassParser.php b/src/Parser/ClassParser.php index 323f266..d6baa50 100644 --- a/src/Parser/ClassParser.php +++ b/src/Parser/ClassParser.php @@ -17,7 +17,7 @@ use ReflectionClass; use ReflectionException; -class ClassParser implements ClassParserInterface +final class ClassParser implements ClassParserInterface { private ClassPropertyParserInterface $propertyParser; private Parser $parser; diff --git a/src/Parser/ClassPropertyParser.php b/src/Parser/ClassPropertyParser.php index 45cd171..5c813e1 100644 --- a/src/Parser/ClassPropertyParser.php +++ b/src/Parser/ClassPropertyParser.php @@ -14,7 +14,7 @@ use PhpParser\Node\UnionType; use RuntimeException; -class ClassPropertyParser implements ClassPropertyParserInterface +final class ClassPropertyParser implements ClassPropertyParserInterface { private DocCommentParserInterface $docParser; diff --git a/src/Parser/DocCommentParser.php b/src/Parser/DocCommentParser.php index 5652d02..1e04b25 100644 --- a/src/Parser/DocCommentParser.php +++ b/src/Parser/DocCommentParser.php @@ -4,7 +4,7 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Parser; -class DocCommentParser implements DocCommentParserInterface +final class DocCommentParser implements DocCommentParserInterface { /** * @param string $docComment diff --git a/src/PhpClass/PhpClass.php b/src/PhpClass/PhpClass.php index a629d40..15b52e6 100644 --- a/src/PhpClass/PhpClass.php +++ b/src/PhpClass/PhpClass.php @@ -4,7 +4,7 @@ namespace PhpKafka\PhpAvroSchemaGenerator\PhpClass; -class PhpClass implements PhpClassInterface +final class PhpClass implements PhpClassInterface { private string $classBody; private string $className; diff --git a/src/PhpClass/PhpClassProperty.php b/src/PhpClass/PhpClassProperty.php index 16e06a9..099d2a9 100644 --- a/src/PhpClass/PhpClassProperty.php +++ b/src/PhpClass/PhpClassProperty.php @@ -6,7 +6,7 @@ use PhpKafka\PhpAvroSchemaGenerator\Parser\PropertyAttributesInterface; -class PhpClassProperty implements PhpClassPropertyInterface +final class PhpClassProperty implements PhpClassPropertyInterface { /** @var mixed */ private $propertyDefault; diff --git a/src/Registry/ClassRegistry.php b/src/Registry/ClassRegistry.php index c3bcdc2..7458d00 100644 --- a/src/Registry/ClassRegistry.php +++ b/src/Registry/ClassRegistry.php @@ -8,7 +8,6 @@ use PhpKafka\PhpAvroSchemaGenerator\Converter\PhpClassConverterInterface; use PhpKafka\PhpAvroSchemaGenerator\Exception\ClassRegistryException; use PhpKafka\PhpAvroSchemaGenerator\Parser\TokenParser; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClass; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface; use RecursiveDirectoryIterator; use RecursiveIteratorIterator;