diff --git a/src/Optimizer/FullNameOptimizer.php b/src/Optimizer/FullNameOptimizer.php index 3f79667..18c164b 100644 --- a/src/Optimizer/FullNameOptimizer.php +++ b/src/Optimizer/FullNameOptimizer.php @@ -126,7 +126,7 @@ private function removeNamespaceFromString(string $currentNamespace, $data) $dataNameSpacePaths = explode('.', $data); foreach ($dataNameSpacePaths as $idx => $dataNameSpacePath) { - if ($currentNameSpacePaths[$idx] === $dataNameSpacePath) { + if ( isset($currentNameSpacePaths[$idx]) and $currentNameSpacePaths[$idx] === $dataNameSpacePath) { unset($dataNameSpacePaths[$idx]); } else { break; diff --git a/src/Parser/ClassParser.php b/src/Parser/ClassParser.php index a6e7ba4..94c4f13 100644 --- a/src/Parser/ClassParser.php +++ b/src/Parser/ClassParser.php @@ -24,7 +24,15 @@ class ClassParser implements ClassParserInterface private Parser $parser; /** @var Stmt[]|null */ - private ?array $statements; + protected ?array $statements; + + /** + * @return Stmt[]|null + */ + public function getStatements(): ?array + { + return $this->statements; + } public function __construct(Parser $parser, ClassPropertyParserInterface $propertyParser) { @@ -55,6 +63,10 @@ public function getClassName(): ?string } } } + } elseif ($statement instanceof Class_){ + if ($statement->name instanceof Identifier) { + return $statement->name->name; + } } } @@ -180,7 +192,7 @@ private function getClassProperties(array $statements): array * @param PhpClassPropertyInterface[] $properties * @return PhpClassPropertyInterface[] */ - private function getAllClassProperties(Class_ $class, array $properties): array + public function getAllClassProperties(Class_ $class, array $properties): array { foreach ($class->stmts as $pStatement) { if ($pStatement instanceof Property) { @@ -231,40 +243,4 @@ private function getParentClassStatements(): ?array return $this->parser->parse($parentClass); } - -// /** -// * @return Stmt[]|null -// * @throws ReflectionException -// */ -// private function getParentClassStatements(): ?array -// { -// /** @var class-string[] $usedClasses */ -// $usedClasses = $this->getUsedClasses(); -// $parentClass = $this->getParentClassName(); -// -// if (null === $parentClass) { -// return []; -// } -// -// if (null !== $usedClasses[$this->getParentClassName()]) { -// $parentClass = $usedClasses[$this->getParentClassName()]; -// } -// -// $rc = new ReflectionClass($parentClass); -// $filename = $rc->getFileName(); -// -// if (false === $filename) { -// return []; -// } -// -// $parentClass = file_get_contents($filename); -// -// if (false === $parentClass) { -// // @codeCoverageIgnoreStart -// return []; -// // @codeCoverageIgnoreEnd -// } -// -// return $this->parser->parse($parentClass); -// } } diff --git a/src/Parser/ClassPropertyParser.php b/src/Parser/ClassPropertyParser.php index f67fd61..808845d 100644 --- a/src/Parser/ClassPropertyParser.php +++ b/src/Parser/ClassPropertyParser.php @@ -13,13 +13,12 @@ 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; class ClassPropertyParser implements ClassPropertyParserInterface { - private DocCommentParserInterface $docParser; + protected DocCommentParserInterface $docParser; /** * @param DocCommentParserInterface $docParser diff --git a/tests/Integration/Parser/ClassParserTest.php b/tests/Integration/Parser/ClassParserTest.php index c15653f..77ac77a 100644 --- a/tests/Integration/Parser/ClassParserTest.php +++ b/tests/Integration/Parser/ClassParserTest.php @@ -171,9 +171,11 @@ public function testClassWithNoParentFile(): void { $propertyParser = new ClassPropertyParser(new DocCommentParser()); $parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser); - $parser->setCode('setCode('<' . '?php ' . $code); $properties = $parser->getProperties(); self::assertEquals(1, count($properties)); - self::assertEquals('string', $properties[0]->getPropertyType()); + self::assertEquals(new PhpClassPropertyType(), $properties[0]->getPropertyType()); } } diff --git a/tests/Integration/Registry/ClassRegistryTest.php b/tests/Integration/Registry/ClassRegistryTest.php index 7cef762..bd633e8 100644 --- a/tests/Integration/Registry/ClassRegistryTest.php +++ b/tests/Integration/Registry/ClassRegistryTest.php @@ -49,7 +49,7 @@ public function testLoad() $classes = $registry->getClasses(); - self::assertCount(4, $classes); + self::assertCount(5, $classes); foreach ($classes as $class) { self::assertInstanceOf(PhpClassInterface::class, $class); diff --git a/tests/Integration/Registry/SchemaRegistryTest.php b/tests/Integration/Registry/SchemaRegistryTest.php index 87a0e00..c2066d1 100644 --- a/tests/Integration/Registry/SchemaRegistryTest.php +++ b/tests/Integration/Registry/SchemaRegistryTest.php @@ -47,9 +47,7 @@ public function testLoad() self::assertContains($schema->getSchemaId(), $schemaIds); } - $expectedNames = ['CD', 'Collection', 'Page', 'Library']; - - self::assertSame(sort($expectedNames), sort($registry->getSchemaNamesPerNamespace('com.example'))); + self::assertSame(['Library', 'CD', 'Collection', 'Page'], $registry->getSchemaNamesPerNamespace('com.example')); } public function testGetRootSchemas() diff --git a/tests/Unit/PhpClass/PhpClassPropertyTest.php b/tests/Unit/PhpClass/PhpClassPropertyTest.php index 4a59531..f3b743a 100644 --- a/tests/Unit/PhpClass/PhpClassPropertyTest.php +++ b/tests/Unit/PhpClass/PhpClassPropertyTest.php @@ -5,6 +5,8 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Unit\PhpClass; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty; +use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyType; +use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyTypeItem; use PHPUnit\Framework\TestCase; /** @@ -14,10 +16,10 @@ class PhpClassPropertyTest extends TestCase { public function testGetters() { - $property = new PhpClassProperty('propertyName', 'array', 'default', 'doc', 'logicalType'); + $property = new PhpClassProperty('propertyName', new PhpClassPropertyType(new PhpClassPropertyTypeItem('array', true)), 'default', 'doc', 'logicalType'); self::assertEquals('propertyName', $property->getPropertyName()); - self::assertEquals('array', $property->getPropertyType()); + self::assertEquals(new PhpClassPropertyType(new PhpClassPropertyTypeItem('array', true)), $property->getPropertyType()); self::assertEquals('default', $property->getPropertyDefault()); self::assertEquals('doc', $property->getPropertyDoc()); self::assertEquals('logicalType', $property->getPropertyLogicalType()); diff --git a/tests/Unit/PhpClassConverterTest.php b/tests/Unit/PhpClassConverterTest.php index 154971f..75f5305 100644 --- a/tests/Unit/PhpClassConverterTest.php +++ b/tests/Unit/PhpClassConverterTest.php @@ -8,6 +8,8 @@ use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParserInterface; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface; use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; +use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyType; +use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyTypeItem; use PHPUnit\Framework\TestCase; class PhpClassConverterTest extends TestCase @@ -15,17 +17,17 @@ class PhpClassConverterTest extends TestCase public function testConvert(): void { $property1 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property1->expects(self::once())->method('getPropertyType')->willReturn(1); + $property1->expects(self::once())->method('getPropertyType')->willReturn(new PhpClassPropertyType(new PhpClassPropertyTypeItem('array??', true))); $property2 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property2->expects(self::exactly(2))->method('getPropertyType')->willReturn('string|array|int[]|mixed[]'); + $property2->expects(self::once())->method('getPropertyType')->willReturn(new PhpClassPropertyType(new PhpClassPropertyTypeItem('array??', true))); $property3 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property3->expects(self::exactly(2))->method('getPropertyType')->willReturn('string'); + $property3->expects(self::once())->method('getPropertyType')->willReturn(new PhpClassPropertyType(new PhpClassPropertyTypeItem('array??', true))); $property4 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property4->expects(self::exactly(2))->method('getPropertyType')->willReturn('object|XYZ|UC'); + $property4->expects(self::once())->method('getPropertyType')->willReturn(new PhpClassPropertyType(new PhpClassPropertyTypeItem('array??', true))); $property5 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property5->expects(self::exactly(2))->method('getPropertyType')->willReturn('mixed'); + $property5->expects(self::once())->method('getPropertyType')->willReturn(new PhpClassPropertyType(new PhpClassPropertyTypeItem('array??', true))); $property6 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property6->expects(self::exactly(2))->method('getPropertyType')->willReturn('array|mixed[]'); + $property6->expects(self::once())->method('getPropertyType')->willReturn(new PhpClassPropertyType(new PhpClassPropertyTypeItem('array??', true))); $parser = $this->getMockForAbstractClass(ClassParserInterface::class); @@ -34,25 +36,25 @@ public function testConvert(): void $parser->expects(self::once())->method('getProperties')->willReturn( [$property1, $property2, $property3, $property4, $property5, $property6] ); - $parser->expects(self::exactly(2))->method('getUsedClasses')->willReturn(['XYZ' => 'a\\b\\ZYX']); - $parser->expects(self::exactly(3))->method('getNamespace')->willReturn('x\\y'); +// $parser->expects(self::exactly(2))->method('getUsedClasses')->willReturn(['XYZ' => 'a\\b\\ZYX']); +// $parser->expects(self::exactly(3))->method('getNamespace')->willReturn('x\\y'); $converter = new PhpClassConverter($parser); self::assertInstanceOf(PhpClassInterface::class, $converter->convert('some class stuff')); } - public function testConvertWithNoNamesace(): void + public function testConvertWithNoNamespace(): void { $property1 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property1->expects(self::exactly(2))->method('getPropertyType')->willReturn('ABC'); + $property1->expects(self::once())->method('getPropertyType')->willReturn(new PhpClassPropertyType(new PhpClassPropertyTypeItem('ABC'))); $parser = $this->getMockForAbstractClass(ClassParserInterface::class); $parser->expects(self::once())->method('setCode')->with('some class stuff'); $parser->expects(self::exactly(2))->method('getClassName')->willReturn('foo'); $parser->expects(self::once())->method('getProperties')->willReturn([$property1]); - $parser->expects(self::exactly(1))->method('getUsedClasses')->willReturn([]); - $parser->expects(self::exactly(2))->method('getNamespace')->willReturn(null); +// $parser->expects(self::exactly(1))->method('getUsedClasses')->willReturn([]); +// $parser->expects(self::exactly(2))->method('getNamespace')->willReturn(null); $converter = new PhpClassConverter($parser); self::assertInstanceOf(PhpClassInterface::class, $converter->convert('some class stuff')); @@ -65,4 +67,4 @@ public function testConvertOfNonClass(): void $converter = new PhpClassConverter($parser); self::assertNull($converter->convert('some class stuff')); } -} \ No newline at end of file +}