From 1cc81b7a42e4506f584334429ab3fb5e8fa610b7 Mon Sep 17 00:00:00 2001 From: Pavel Alexeev Date: Sat, 8 Jan 2022 17:01:11 +0300 Subject: [PATCH] #33 Add ClassParser parameter into interface of parsing property --- src/Parser/AvroClassPropertyParser.php | 8 +++---- src/Parser/ClassParser.php | 4 ++-- src/Parser/ClassPropertyParser.php | 16 ++++--------- src/Parser/ClassPropertyParserInterface.php | 5 ++-- tests/Unit/Parser/ClassPropertyParserTest.php | 23 +++++++------------ 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/Parser/AvroClassPropertyParser.php b/src/Parser/AvroClassPropertyParser.php index b8835be..1bf9d3e 100644 --- a/src/Parser/AvroClassPropertyParser.php +++ b/src/Parser/AvroClassPropertyParser.php @@ -3,11 +3,11 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Parser; use PhpKafka\PhpAvroSchemaGenerator\Exception\SkipPropertyException; -use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyType; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyTypeItem; +use PhpParser\Node\Stmt\Property; /** * We will skip transient and private properties like starting at 'o_', '_', 'omitMandatoryCheck', 'allLazyKeysMarkedAsLoaded' @@ -15,10 +15,10 @@ class AvroClassPropertyParser extends ClassPropertyParser { /** - * @throws SkipPropertyException + * @inheritdoc */ - public function parseProperty($property): PhpClassPropertyInterface { - $prop = parent::parseProperty($property); + public function parseProperty(Property $property, ClassParserInterface $classParser): PhpClassPropertyInterface { + $prop = parent::parseProperty($property, $classParser); if (str_starts_with($prop->getPropertyName(), 'o_') or str_starts_with($prop->getPropertyName(), '_') or in_array($prop->getPropertyName(), ['omitMandatoryCheck', 'allLazyKeysMarkedAsLoaded'])) { throw new SkipPropertyException(); diff --git a/src/Parser/ClassParser.php b/src/Parser/ClassParser.php index 1162eff..a6e7ba4 100644 --- a/src/Parser/ClassParser.php +++ b/src/Parser/ClassParser.php @@ -18,7 +18,7 @@ use ReflectionClass; use ReflectionException; -final class ClassParser implements ClassParserInterface +class ClassParser implements ClassParserInterface { private ClassPropertyParserInterface $propertyParser; private Parser $parser; @@ -185,7 +185,7 @@ private function getAllClassProperties(Class_ $class, array $properties): array foreach ($class->stmts as $pStatement) { if ($pStatement instanceof Property) { try { - $properties[] = $this->propertyParser->parseProperty($pStatement); + $properties[] = $this->propertyParser->parseProperty($pStatement, $this); } catch(SkipPropertyException $skip){ } } diff --git a/src/Parser/ClassPropertyParser.php b/src/Parser/ClassPropertyParser.php index 1639f29..f67fd61 100644 --- a/src/Parser/ClassPropertyParser.php +++ b/src/Parser/ClassPropertyParser.php @@ -4,7 +4,6 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Parser; -use JetBrains\PhpStorm\Pure; use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; @@ -14,9 +13,9 @@ use PhpParser\Comment\Doc; use PhpParser\Node\Identifier; use PhpParser\Node\NullableType; +use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; use PhpParser\Node\UnionType; -use RuntimeException; class ClassPropertyParser implements ClassPropertyParserInterface { @@ -31,16 +30,11 @@ public function __construct(DocCommentParserInterface $docParser) } /** - * @param Property|mixed $property - * @return PhpClassPropertyInterface + * @inheritdoc */ - public function parseProperty($property): PhpClassPropertyInterface + public function parseProperty(Property $property, ClassParserInterface $classParser): PhpClassPropertyInterface { - if (false === $property instanceof Property) { - throw new RuntimeException(sprintf('Property must be of type: %s', Property::class)); - } - - $propertyAttributes = $this->getPropertyAttributes($property); + $propertyAttributes = $this->getPropertyAttributes($property, $classParser); return new PhpClassProperty( $propertyAttributes['name'], @@ -55,7 +49,7 @@ public function parseProperty($property): PhpClassPropertyInterface * @param Property $property * @return array */ - private function getPropertyAttributes(Property $property): array + protected function getPropertyAttributes(Property $property): array { $attributes = $this->getEmptyAttributesArray(); $docComments = $this->getAllPropertyDocComments($property); diff --git a/src/Parser/ClassPropertyParserInterface.php b/src/Parser/ClassPropertyParserInterface.php index 79c5025..0d4a865 100644 --- a/src/Parser/ClassPropertyParserInterface.php +++ b/src/Parser/ClassPropertyParserInterface.php @@ -11,9 +11,10 @@ interface ClassPropertyParserInterface { /** - * @param Property|mixed $property + * @param Property $property + * @param ClassParserInterface $classParser * @return PhpClassPropertyInterface * @throws SkipPropertyException Such property will then just skipped */ - public function parseProperty($property): PhpClassPropertyInterface; + public function parseProperty(Property $property, ClassParserInterface $classParser): PhpClassPropertyInterface; } diff --git a/tests/Unit/Parser/ClassPropertyParserTest.php b/tests/Unit/Parser/ClassPropertyParserTest.php index 1811e1f..94bdc37 100644 --- a/tests/Unit/Parser/ClassPropertyParserTest.php +++ b/tests/Unit/Parser/ClassPropertyParserTest.php @@ -4,12 +4,14 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Unit\Parser; +use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser; use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser; use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParserInterface; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; use PhpParser\Comment\Doc; use PhpParser\Node\Identifier; use PhpParser\Node\NullableType; +use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\PropertyProperty; use PhpParser\Node\UnionType; @@ -20,6 +22,7 @@ class ClassPropertyParserTest extends TestCase { public function testParseProperty(): void { + $classParser = $this->getMockBuilder(ClassParser::class)->disableOriginalConstructor()->getMock(); $doc = $this->getMockBuilder(Doc::class)->disableOriginalConstructor()->getMock(); $varId = $this->getMockBuilder(VarLikeIdentifier::class)->disableOriginalConstructor()->getMock(); $varId->name = 'bla'; @@ -48,19 +51,9 @@ public function testParseProperty(): void $property4->props = [$propertyProperty]; $cpp = new ClassPropertyParser($docParser); - self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property1)); - self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property2)); - self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property3)); - self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property4)); + self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property1, $classParser)); + self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property2, $classParser)); + self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property3, $classParser)); + self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property4, $classParser)); } - - public function testParsePropertyExceptionOnNonProperty(): void - { - self::expectException(\RuntimeException::class); - self::expectExceptionMessage('Property must be of type: PhpParser\Node\Stmt\Property'); - $docParser = $this->getMockForAbstractClass(DocCommentParserInterface::class); - $cpp = new ClassPropertyParser($docParser); - - $cpp->parseProperty(1); - } -} \ No newline at end of file +}