Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with php8.2 #62

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
"license": "MIT",
"require": {
"ext-json": "*",
"flix-tech/avro-php": "^3.0|^4.0",
"flix-tech/avro-php": "^3.0|^4.0|^5.0",
"symfony/console": "^4.3|^5.1|^6.0",
"nikic/php-parser": "^4.13",
"pimple/pimple": "^3.5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.15",
"infection/infection": "^0.25",
"friendsofphp/php-cs-fixer": "^2.19|^3.15",
"infection/infection": "^0.25|^0.27",
"composer/xdebug-handler": "^2.0|^3.0",
"phpstan/phpstan": "^1.2",
"phpunit/phpunit": "^9.3",
"rregeer/phpunit-coverage-check": "^0.3",
Expand Down
1 change: 0 additions & 1 deletion example/classes/SomeOtherTestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class SomeOtherTestClass
{

}
1 change: 0 additions & 1 deletion example/classes/SomeTestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class SomeTestClass extends SomeBaseClass
{

/**
* @var string
*/
Expand Down
1 change: 0 additions & 1 deletion example/classes/SomeTestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

interface SomeTestInterface
{

}
1 change: 0 additions & 1 deletion example/classes/Wonderland/Wonderland.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class Wonderland
{

}
9 changes: 8 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
parameters:
level: 8
paths: [ src ]
checkGenericClassInNonGenericObjectType: false
checkGenericClassInNonGenericObjectType: false
excludePaths:
- vendor
ignoreErrors:
# Disable error in class for example of generation
-
message: '#Property PhpKafka\\PhpAvroSchemaGenerator\\Example\\SomeTestClass::\$string is unused.#'
path: example/classes/SomeTestClass.php
4 changes: 2 additions & 2 deletions src/Optimizer/FullNameOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ private function removeNamespaceFromString(string $currentNamespace, $data)
$currentNameSpacePaths = explode('.', $currentNamespace);
$dataNameSpacePaths = explode('.', $data);

foreach ($dataNameSpacePaths as $idx => $dataNameSpacePath) {
if ($currentNameSpacePaths[$idx] === $dataNameSpacePath) {
foreach ($currentNameSpacePaths as $idx => $currentNameSpacePath) {
if ($currentNameSpacePath === $dataNameSpacePaths[$idx]) {
unset($dataNameSpacePaths[$idx]);
} else {
break;
Expand Down
30 changes: 22 additions & 8 deletions src/Parser/ClassParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getClassName(): ?string
}

/**
* @return string|null
* @return class-string|null
*/
public function getParentClassName(): ?string
{
Expand All @@ -74,14 +74,14 @@ public function getParentClassName(): ?string
foreach ($statement->stmts as $nsStatement) {
if ($nsStatement instanceof Class_) {
if (null !== $nsStatement->extends) {
return implode('\\', $nsStatement->extends->parts);
return $this->buildClassName($nsStatement->extends->getParts());
}
}
}
} else {
if ($statement instanceof Class_) {
if (null !== $statement->extends) {
return implode('\\', $statement->extends->parts);
return $this->buildClassName($statement->extends->getParts());
}
}
}
Expand All @@ -90,6 +90,9 @@ public function getParentClassName(): ?string
return null;
}

/**
* @return class-string[]
*/
public function getUsedClasses(): array
{
$usedClasses = [];
Expand All @@ -104,8 +107,8 @@ public function getUsedClasses(): array
if ($nStatement instanceof Use_) {
/** @var UseUse $use */
foreach ($nStatement->uses as $use) {
$className = $use->name->parts[array_key_last($use->name->parts)];
$usedClasses[$className] = implode('\\', $use->name->parts);
$className = $use->name->getParts()[array_key_last($use->name->getParts())];
$usedClasses[$className] = $this->buildClassName($use->name->getParts());
}
}
}
Expand All @@ -115,6 +118,18 @@ public function getUsedClasses(): array
return $usedClasses;
}

/**
* @param string[] $parts
* @return class-string
*/
public function buildClassName(array $parts): string
{
/** @var class-string $classname */
$classname = implode('\\', $parts);

return $classname;
}

/**
* @return string
*/
Expand Down Expand Up @@ -196,16 +211,15 @@ private function getAllClassProperties(Class_ $class, array $properties): array
*/
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()];
if (array_key_exists($parentClass, $usedClasses) && null !== $usedClasses[$parentClass]) {
$parentClass = $usedClasses[$parentClass];
}

$rc = new ReflectionClass($parentClass);
Expand Down
3 changes: 2 additions & 1 deletion src/Registry/SchemaRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,13 @@ private function registerSchemaFile(\SplFileInfo $fileInfo): void
}

$schemaData = json_decode($fileContent, true, JSON_THROW_ON_ERROR);
$namespace = (string) $schemaData['namespace'];

if (null === $schemaData) {
throw new SchemaRegistryException(sprintf(SchemaRegistryException::FILE_INVALID, $fileName));
}

$namespace = array_key_exists('namespace', $schemaData) ? (string) $schemaData['namespace'] : '';

$template = (new SchemaTemplate())
->withFilename($fileInfo->getBasename())
->withSchemaDefinition($fileContent)
Expand Down
20 changes: 10 additions & 10 deletions tests/Integration/Parser/ClassParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,45 @@
use PHPUnit\Framework\TestCase;

/**
* @covers PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser
* @covers \PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser
*/
class ClassParserTest extends TestCase
{
public function testGetClassName()
public function testGetClassName(): void
{
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$parser->setCode(file_get_contents($filePath));
$parser->setCode((string) file_get_contents($filePath));
self::assertEquals('SomeTestClass', $parser->getClassName());
self::assertEquals('SomeTestClass', $parser->getClassName());
}

public function testGetClassNameForInterface()
public function testGetClassNameForInterface(): void
{
$filePath = __DIR__ . '/../../../example/classes/SomeTestInterface.php';
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$parser->setCode(file_get_contents($filePath));
$parser->setCode((string) file_get_contents($filePath));
self::assertNull($parser->getClassName());
}

public function testGetNamespace()
public function testGetNamespace(): void
{
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$parser->setCode(file_get_contents($filePath));
$parser->setCode((string) file_get_contents($filePath));
self::assertEquals('PhpKafka\\PhpAvroSchemaGenerator\\Example', $parser->getNamespace());
self::assertEquals('PhpKafka\\PhpAvroSchemaGenerator\\Example', $parser->getNamespace());
}

public function testGetProperties()
public function testGetProperties(): void
{
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$parser->setCode(file_get_contents($filePath));
$parser->setCode((string) file_get_contents($filePath));
$properties = $parser->getProperties();
self::assertCount(16, $properties);

Expand All @@ -65,7 +65,7 @@ public function testClassAndNamespaceAreNullWithNoCode(): void
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$refObject = new \ReflectionObject($parser);
$refProperty = $refObject->getProperty('statements');
$refProperty->setAccessible( true );
$refProperty->setAccessible(true);
$refProperty->setValue($parser, null);
self::assertNull($parser->getClassName());
self::assertNull($parser->getNamespace());
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/Parser/DocCommentParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PHPUnit\Framework\TestCase;

/**
* @covers PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser
* @covers \PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser
*/
class DocCommentParserTest extends TestCase
{
Expand All @@ -23,9 +23,9 @@ public function testParseDoc(): void
self::assertEquals(
[
'var' => 'string',
'function-description' =>''
'function-description' => ''
],
$result
);
}
}
}
10 changes: 5 additions & 5 deletions tests/Integration/Registry/ClassRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
use SplFileInfo;

/**
* @covers PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry
* @covers \PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry
*/
class ClassRegistryTest extends TestCase
{
public function testClassDirectory()
public function testClassDirectory(): void
{
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
Expand All @@ -34,7 +34,7 @@ public function testClassDirectory()
self::assertEquals(['/tmp' => 1], $result->getClassDirectories());
}

public function testLoad()
public function testLoad(): void
{
$classDir = __DIR__ . '/../../../example/classes';

Expand All @@ -54,7 +54,7 @@ public function testLoad()
}
}

public function testRegisterSchemaFileThatDoesntExist()
public function testRegisterSchemaFileThatDoesntExist(): void
{
$fileInfo = new SplFileInfo('somenonexistingfile');
$propertyParser = new ClassPropertyParser(new DocCommentParser());
Expand All @@ -71,7 +71,7 @@ public function testRegisterSchemaFileThatDoesntExist()
$method->invokeArgs($registry, [$fileInfo]);
}

public function testRegisterSchemaFileThatIsNotReadable()
public function testRegisterSchemaFileThatIsNotReadable(): void
{
touch('testfile');
chmod('testfile', 222);
Expand Down
22 changes: 12 additions & 10 deletions tests/Integration/Registry/SchemaRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
use SplFileInfo;

/**
* @covers PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistry
* @covers \PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistry
*/
class SchemaRegistryTest extends TestCase
{
public function testSchemaDirectories()
public function testSchemaDirectories(): void
{
$registry = new SchemaRegistry();
$result = $registry->addSchemaTemplateDirectory('/tmp');
Expand All @@ -24,7 +24,7 @@ public function testSchemaDirectories()
self::assertEquals(['/tmp' => 1], $result->getSchemaDirectories());
}

public function testLoad()
public function testLoad(): void
{
$schemaIds = [
'Book',
Expand All @@ -49,10 +49,12 @@ public function testLoad()

$expectedNames = ['CD', 'Collection', 'Page', 'Library'];

self::assertSame(sort($expectedNames), sort($registry->getSchemaNamesPerNamespace('com.example')));
$actualNamespaces = $registry->getSchemaNamesPerNamespace('com.example');

self::assertSame(sort($expectedNames), sort($actualNamespaces));
}

public function testGetRootSchemas()
public function testGetRootSchemas(): void
{
$schemaDir = __DIR__ . '/../../../example/schemaTemplates';
$registry = (new SchemaRegistry())->addSchemaTemplateDirectory($schemaDir)->load();
Expand All @@ -66,14 +68,14 @@ public function testGetRootSchemas()
}
}

public function testGetSchemaByIdNotExisting()
public function testGetSchemaByIdNotExisting(): void
{
$registry = new SchemaRegistry();

self::assertNull($registry->getSchemaById('test'));
}

public function testGetSchemaById()
public function testGetSchemaById(): void
{
$template = $this->getMockForAbstractClass(SchemaTemplateInterface::class);

Expand All @@ -87,7 +89,7 @@ public function testGetSchemaById()
self::assertEquals($template, $registry->getSchemaById('test'));
}

public function testRegisterSchemaFileThatDoesntExist()
public function testRegisterSchemaFileThatDoesntExist(): void
{
$fileInfo = new SplFileInfo('somenonexistingfile');
$registry = new SchemaRegistry();
Expand All @@ -101,7 +103,7 @@ public function testRegisterSchemaFileThatDoesntExist()
$method->invokeArgs($registry, [$fileInfo]);
}

public function testRegisterSchemaFileThatIsNotReadable()
public function testRegisterSchemaFileThatIsNotReadable(): void
{
touch('testfile');
chmod('testfile', 222);
Expand All @@ -125,7 +127,7 @@ public function testRegisterSchemaFileThatIsNotReadable()
}
}

public function testRegisterSchemaWithInvalidContent()
public function testRegisterSchemaWithInvalidContent(): void
{
touch('testfile');

Expand Down
Loading
Loading