Skip to content

Commit 64776fd

Browse files
misterxanFeliks Ignatyev
andauthored
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66

77
class SomeOtherTestClass
88
{
9-
109
}

example/classes/SomeTestClass.php

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44

55
interface SomeTestInterface
66
{
7-
87
}

example/classes/Wonderland/Wonderland.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66

77
class Wonderland
88
{
9-
109
}

phpstan.neon

Lines changed: 8 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 22 additions & 8 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 10 additions & 10 deletions
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());

0 commit comments

Comments
 (0)