Skip to content

Commit 64776fd

Browse files
misterxanFeliks Ignatyev
and
Feliks Ignatyev
authored
Add compatibility with php8.2 (#62)
Also fix some warnings after running phpunit/php-cs-fixer/phpstan Co-authored-by: Feliks Ignatyev <[email protected]>
1 parent 77007b6 commit 64776fd

29 files changed

+130
-109
lines changed

composer.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
"license": "MIT",
1010
"require": {
1111
"ext-json": "*",
12-
"flix-tech/avro-php": "^3.0|^4.0",
12+
"flix-tech/avro-php": "^3.0|^4.0|^5.0",
1313
"symfony/console": "^4.3|^5.1|^6.0",
1414
"nikic/php-parser": "^4.13",
1515
"pimple/pimple": "^3.5"
1616
},
1717
"require-dev": {
18-
"friendsofphp/php-cs-fixer": "^2.15",
19-
"infection/infection": "^0.25",
18+
"friendsofphp/php-cs-fixer": "^2.19|^3.15",
19+
"infection/infection": "^0.25|^0.27",
20+
"composer/xdebug-handler": "^2.0|^3.0",
2021
"phpstan/phpstan": "^1.2",
2122
"phpunit/phpunit": "^9.3",
2223
"rregeer/phpunit-coverage-check": "^0.3",

example/classes/SomeOtherTestClass.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66

77
class SomeOtherTestClass
88
{
9-
109
}

example/classes/SomeTestClass.php

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
class SomeTestClass extends SomeBaseClass
1111
{
12-
1312
/**
1413
* @var string
1514
*/

example/classes/SomeTestInterface.php

-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44

55
interface SomeTestInterface
66
{
7-
87
}

example/classes/Wonderland/Wonderland.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66

77
class Wonderland
88
{
9-
109
}

phpstan.neon

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
parameters:
22
level: 8
33
paths: [ src ]
4-
checkGenericClassInNonGenericObjectType: false
4+
checkGenericClassInNonGenericObjectType: false
5+
excludePaths:
6+
- vendor
7+
ignoreErrors:
8+
# Disable error in class for example of generation
9+
-
10+
message: '#Property PhpKafka\\PhpAvroSchemaGenerator\\Example\\SomeTestClass::\$string is unused.#'
11+
path: example/classes/SomeTestClass.php

src/Optimizer/FullNameOptimizer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ private function removeNamespaceFromString(string $currentNamespace, $data)
127127
$currentNameSpacePaths = explode('.', $currentNamespace);
128128
$dataNameSpacePaths = explode('.', $data);
129129

130-
foreach ($dataNameSpacePaths as $idx => $dataNameSpacePath) {
131-
if ($currentNameSpacePaths[$idx] === $dataNameSpacePath) {
130+
foreach ($currentNameSpacePaths as $idx => $currentNameSpacePath) {
131+
if ($currentNameSpacePath === $dataNameSpacePaths[$idx]) {
132132
unset($dataNameSpacePaths[$idx]);
133133
} else {
134134
break;

src/Parser/ClassParser.php

+22-8
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getClassName(): ?string
6161
}
6262

6363
/**
64-
* @return string|null
64+
* @return class-string|null
6565
*/
6666
public function getParentClassName(): ?string
6767
{
@@ -74,14 +74,14 @@ public function getParentClassName(): ?string
7474
foreach ($statement->stmts as $nsStatement) {
7575
if ($nsStatement instanceof Class_) {
7676
if (null !== $nsStatement->extends) {
77-
return implode('\\', $nsStatement->extends->parts);
77+
return $this->buildClassName($nsStatement->extends->getParts());
7878
}
7979
}
8080
}
8181
} else {
8282
if ($statement instanceof Class_) {
8383
if (null !== $statement->extends) {
84-
return implode('\\', $statement->extends->parts);
84+
return $this->buildClassName($statement->extends->getParts());
8585
}
8686
}
8787
}
@@ -90,6 +90,9 @@ public function getParentClassName(): ?string
9090
return null;
9191
}
9292

93+
/**
94+
* @return class-string[]
95+
*/
9396
public function getUsedClasses(): array
9497
{
9598
$usedClasses = [];
@@ -104,8 +107,8 @@ public function getUsedClasses(): array
104107
if ($nStatement instanceof Use_) {
105108
/** @var UseUse $use */
106109
foreach ($nStatement->uses as $use) {
107-
$className = $use->name->parts[array_key_last($use->name->parts)];
108-
$usedClasses[$className] = implode('\\', $use->name->parts);
110+
$className = $use->name->getParts()[array_key_last($use->name->getParts())];
111+
$usedClasses[$className] = $this->buildClassName($use->name->getParts());
109112
}
110113
}
111114
}
@@ -115,6 +118,18 @@ public function getUsedClasses(): array
115118
return $usedClasses;
116119
}
117120

121+
/**
122+
* @param string[] $parts
123+
* @return class-string
124+
*/
125+
public function buildClassName(array $parts): string
126+
{
127+
/** @var class-string $classname */
128+
$classname = implode('\\', $parts);
129+
130+
return $classname;
131+
}
132+
118133
/**
119134
* @return string
120135
*/
@@ -196,16 +211,15 @@ private function getAllClassProperties(Class_ $class, array $properties): array
196211
*/
197212
private function getParentClassStatements(): ?array
198213
{
199-
/** @var class-string[] $usedClasses */
200214
$usedClasses = $this->getUsedClasses();
201215
$parentClass = $this->getParentClassName();
202216

203217
if (null === $parentClass) {
204218
return [];
205219
}
206220

207-
if (null !== $usedClasses[$this->getParentClassName()]) {
208-
$parentClass = $usedClasses[$this->getParentClassName()];
221+
if (array_key_exists($parentClass, $usedClasses) && null !== $usedClasses[$parentClass]) {
222+
$parentClass = $usedClasses[$parentClass];
209223
}
210224

211225
$rc = new ReflectionClass($parentClass);

src/Registry/SchemaRegistry.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,13 @@ private function registerSchemaFile(\SplFileInfo $fileInfo): void
135135
}
136136

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

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

143+
$namespace = array_key_exists('namespace', $schemaData) ? (string) $schemaData['namespace'] : '';
144+
144145
$template = (new SchemaTemplate())
145146
->withFilename($fileInfo->getBasename())
146147
->withSchemaDefinition($fileContent)

tests/Integration/Parser/ClassParserTest.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,45 @@
1212
use PHPUnit\Framework\TestCase;
1313

1414
/**
15-
* @covers PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser
15+
* @covers \PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser
1616
*/
1717
class ClassParserTest extends TestCase
1818
{
19-
public function testGetClassName()
19+
public function testGetClassName(): void
2020
{
2121
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
2222
$propertyParser = new ClassPropertyParser(new DocCommentParser());
2323
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
24-
$parser->setCode(file_get_contents($filePath));
24+
$parser->setCode((string) file_get_contents($filePath));
2525
self::assertEquals('SomeTestClass', $parser->getClassName());
2626
self::assertEquals('SomeTestClass', $parser->getClassName());
2727
}
2828

29-
public function testGetClassNameForInterface()
29+
public function testGetClassNameForInterface(): void
3030
{
3131
$filePath = __DIR__ . '/../../../example/classes/SomeTestInterface.php';
3232
$propertyParser = new ClassPropertyParser(new DocCommentParser());
3333
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
34-
$parser->setCode(file_get_contents($filePath));
34+
$parser->setCode((string) file_get_contents($filePath));
3535
self::assertNull($parser->getClassName());
3636
}
3737

38-
public function testGetNamespace()
38+
public function testGetNamespace(): void
3939
{
4040
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
4141
$propertyParser = new ClassPropertyParser(new DocCommentParser());
4242
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
43-
$parser->setCode(file_get_contents($filePath));
43+
$parser->setCode((string) file_get_contents($filePath));
4444
self::assertEquals('PhpKafka\\PhpAvroSchemaGenerator\\Example', $parser->getNamespace());
4545
self::assertEquals('PhpKafka\\PhpAvroSchemaGenerator\\Example', $parser->getNamespace());
4646
}
4747

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

@@ -65,7 +65,7 @@ public function testClassAndNamespaceAreNullWithNoCode(): void
6565
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
6666
$refObject = new \ReflectionObject($parser);
6767
$refProperty = $refObject->getProperty('statements');
68-
$refProperty->setAccessible( true );
68+
$refProperty->setAccessible(true);
6969
$refProperty->setValue($parser, null);
7070
self::assertNull($parser->getClassName());
7171
self::assertNull($parser->getNamespace());

tests/Integration/Parser/DocCommentParserTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PHPUnit\Framework\TestCase;
99

1010
/**
11-
* @covers PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser
11+
* @covers \PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser
1212
*/
1313
class DocCommentParserTest extends TestCase
1414
{
@@ -23,9 +23,9 @@ public function testParseDoc(): void
2323
self::assertEquals(
2424
[
2525
'var' => 'string',
26-
'function-description' =>''
26+
'function-description' => ''
2727
],
2828
$result
2929
);
3030
}
31-
}
31+
}

tests/Integration/Registry/ClassRegistryTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
use SplFileInfo;
1919

2020
/**
21-
* @covers PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry
21+
* @covers \PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry
2222
*/
2323
class ClassRegistryTest extends TestCase
2424
{
25-
public function testClassDirectory()
25+
public function testClassDirectory(): void
2626
{
2727
$propertyParser = new ClassPropertyParser(new DocCommentParser());
2828
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
@@ -34,7 +34,7 @@ public function testClassDirectory()
3434
self::assertEquals(['/tmp' => 1], $result->getClassDirectories());
3535
}
3636

37-
public function testLoad()
37+
public function testLoad(): void
3838
{
3939
$classDir = __DIR__ . '/../../../example/classes';
4040

@@ -54,7 +54,7 @@ public function testLoad()
5454
}
5555
}
5656

57-
public function testRegisterSchemaFileThatDoesntExist()
57+
public function testRegisterSchemaFileThatDoesntExist(): void
5858
{
5959
$fileInfo = new SplFileInfo('somenonexistingfile');
6060
$propertyParser = new ClassPropertyParser(new DocCommentParser());
@@ -71,7 +71,7 @@ public function testRegisterSchemaFileThatDoesntExist()
7171
$method->invokeArgs($registry, [$fileInfo]);
7272
}
7373

74-
public function testRegisterSchemaFileThatIsNotReadable()
74+
public function testRegisterSchemaFileThatIsNotReadable(): void
7575
{
7676
touch('testfile');
7777
chmod('testfile', 222);

tests/Integration/Registry/SchemaRegistryTest.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
use SplFileInfo;
1212

1313
/**
14-
* @covers PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistry
14+
* @covers \PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistry
1515
*/
1616
class SchemaRegistryTest extends TestCase
1717
{
18-
public function testSchemaDirectories()
18+
public function testSchemaDirectories(): void
1919
{
2020
$registry = new SchemaRegistry();
2121
$result = $registry->addSchemaTemplateDirectory('/tmp');
@@ -24,7 +24,7 @@ public function testSchemaDirectories()
2424
self::assertEquals(['/tmp' => 1], $result->getSchemaDirectories());
2525
}
2626

27-
public function testLoad()
27+
public function testLoad(): void
2828
{
2929
$schemaIds = [
3030
'Book',
@@ -49,10 +49,12 @@ public function testLoad()
4949

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

52-
self::assertSame(sort($expectedNames), sort($registry->getSchemaNamesPerNamespace('com.example')));
52+
$actualNamespaces = $registry->getSchemaNamesPerNamespace('com.example');
53+
54+
self::assertSame(sort($expectedNames), sort($actualNamespaces));
5355
}
5456

55-
public function testGetRootSchemas()
57+
public function testGetRootSchemas(): void
5658
{
5759
$schemaDir = __DIR__ . '/../../../example/schemaTemplates';
5860
$registry = (new SchemaRegistry())->addSchemaTemplateDirectory($schemaDir)->load();
@@ -66,14 +68,14 @@ public function testGetRootSchemas()
6668
}
6769
}
6870

69-
public function testGetSchemaByIdNotExisting()
71+
public function testGetSchemaByIdNotExisting(): void
7072
{
7173
$registry = new SchemaRegistry();
7274

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

76-
public function testGetSchemaById()
78+
public function testGetSchemaById(): void
7779
{
7880
$template = $this->getMockForAbstractClass(SchemaTemplateInterface::class);
7981

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

90-
public function testRegisterSchemaFileThatDoesntExist()
92+
public function testRegisterSchemaFileThatDoesntExist(): void
9193
{
9294
$fileInfo = new SplFileInfo('somenonexistingfile');
9395
$registry = new SchemaRegistry();
@@ -101,7 +103,7 @@ public function testRegisterSchemaFileThatDoesntExist()
101103
$method->invokeArgs($registry, [$fileInfo]);
102104
}
103105

104-
public function testRegisterSchemaFileThatIsNotReadable()
106+
public function testRegisterSchemaFileThatIsNotReadable(): void
105107
{
106108
touch('testfile');
107109
chmod('testfile', 222);
@@ -125,7 +127,7 @@ public function testRegisterSchemaFileThatIsNotReadable()
125127
}
126128
}
127129

128-
public function testRegisterSchemaWithInvalidContent()
130+
public function testRegisterSchemaWithInvalidContent(): void
129131
{
130132
touch('testfile');
131133

0 commit comments

Comments
 (0)