Skip to content

Commit 90553e1

Browse files
dradisicnick-zh
andauthored
update composer dependencies for PHP 8.2+ compatibility (#66)
* chore: update composer dependencies for PHP 8.2+ compatibility * fix: set PHP parser to specific version 8.2 * chore: remove php version from require * fix cs --------- Co-authored-by: Nick <[email protected]>
1 parent cc2b415 commit 90553e1

File tree

12 files changed

+93
-55
lines changed

12 files changed

+93
-55
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Actions Status](https://github.com/php-kafka/php-avro-schema-generator/workflows/CI/badge.svg)](https://github.com/php-kafka/php-avro-schema-generator/workflows/CI/badge.svg)
33
[![Maintainability](https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69/maintainability)](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/maintainability)
44
[![Test Coverage](https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69/test_coverage)](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/test_coverage)
5-
![Supported PHP versions: 7.4 .. 8.x](https://img.shields.io/badge/php-7.4%20..%208.x-blue.svg)
5+
![Supported PHP versions: ^8.2](https://img.shields.io/badge/php-%5E8.2-blue.svg)
66
[![Latest Stable Version](https://poser.pugx.org/php-kafka/php-avro-schema-generator/v/stable)](https://packagist.org/packages/php-kafka/php-avro-schema-generator)
77

88
## Installation
@@ -53,7 +53,7 @@ How to enable optimizer:
5353

5454
**Console example**
5555
```bash
56-
./vendor/bin/avro-cli --optimizeFullNames --optimizeFieldOrder --optimizePrimitiveSchemas avro:subschema:merge ./example/schemaTemplates ./example/schema
56+
./bin/avro-cli --optimizeFullNames --optimizeFieldOrder --optimizePrimitiveSchemas avro:subschema:merge ./example/schemaTemplates ./example/schema
5757
```
5858
**PHP Example**
5959
```php
@@ -87,7 +87,7 @@ After you have reviewed and adjusted your templates you will need to merge them
8787

8888
**Console example**
8989
```bash
90-
./vendor/bin/avro-cli avro:schema:generate ./example/classes ./example/schemaTemplates
90+
./bin/avro-cli avro:schema:generate ./example/classes ./example/schemaTemplates
9191
```
9292

9393
**PHP Example**

composer.json

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

docker-compose.yml

+4
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ services:
99
HOST_USER_ID: ${USER_ID}
1010
volumes:
1111
- ./:/var/www/html
12+
- type: tmpfs
13+
target: /tmp/test
14+
tmpfs:
15+
size: 256M

docker/dev/php/Dockerfile

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
FROM php:7.4-cli-alpine3.15
1+
FROM php:8.2-cli-alpine3.19
22

33
ARG HOST_USER_ID
44
ARG HOST_USER
55

6+
# Create sanitized username as an ENV variable so it persists throughout the build
7+
ENV CLEAN_USER=default
8+
RUN if [ ! -z "$HOST_USER" ]; then \
9+
export CLEAN_USER=$(echo "$HOST_USER" | sed 's/[^a-zA-Z0-9._-]/_/g') && \
10+
echo "export CLEAN_USER=$CLEAN_USER" >> /etc/profile; \
11+
fi
12+
613
COPY php/files/bin/ /usr/local/bin/
714

815
# SYS: Install required packages
916
RUN apk --no-cache upgrade && \
1017
apk --no-cache add bash git sudo make autoconf gcc g++
1118

1219
RUN if [ ! -z "$HOST_USER_ID" ]; then \
13-
adduser -u $HOST_USER_ID -D -H $HOST_USER && \
14-
echo "ALL ALL=NOPASSWD: ALL" >> /etc/sudoers; \
15-
fi
20+
adduser -u $HOST_USER_ID -D -H $CLEAN_USER && \
21+
echo "ALL ALL=NOPASSWD: ALL" >> /etc/sudoers; \
22+
fi
23+
24+
# Create a directory with proper permissions for test files
25+
RUN mkdir -p /tmp/test && \
26+
chmod 777 /tmp/test && \
27+
chown $CLEAN_USER:$CLEAN_USER /tmp/test
1628

1729
# PHP: Install php extensions
1830
RUN mkdir /phpIni && \
@@ -23,9 +35,8 @@ RUN mkdir /phpIni && \
2335
# COMPOSER: install binary
2436
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
2537

26-
USER $HOST_USER
38+
USER $CLEAN_USER
2739

2840
WORKDIR /var/www/html
2941

3042
CMD tail -f /dev/null
31-

example/generate.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
1212
use PhpKafka\PhpAvroSchemaGenerator\Generator\SchemaGenerator;
1313
use PhpParser\ParserFactory;
14+
use PhpParser\PhpVersion;
1415

15-
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
16+
$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2));
1617
$classPropertyParser = new ClassPropertyParser(new DocCommentParser());
1718
$classParser = new ClassParser($parser, $classPropertyParser);
1819

src/Parser/ClassParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function getNamespace(): ?string
142142
foreach ($this->statements as $statement) {
143143
if ($statement instanceof Namespace_) {
144144
if ($statement->name instanceof Name) {
145-
return implode('\\', $statement->name->parts);
145+
return $statement->name->toString();
146146
}
147147
}
148148
}

src/ServiceProvider/ParserServiceProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParserInterface;
1313
use PhpParser\ParserFactory;
1414
use PhpParser\Parser;
15+
use PhpParser\PhpVersion;
1516
use Pimple\Container;
1617
use Pimple\ServiceProviderInterface;
1718

@@ -24,7 +25,7 @@ public function register(Container $container): void
2425
};
2526

2627
$container[Parser::class] = static function (Container $container): Parser {
27-
return $container[ParserFactory::class]->create(ParserFactory::PREFER_PHP7);
28+
return $container[ParserFactory::class]->createForVersion(PhpVersion::fromComponents(8, 2));
2829
};
2930

3031
$container[DocCommentParserInterface::class] = static function (): DocCommentParserInterface {

tests/Integration/Parser/ClassParserTest.php

+12-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser;
1010
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
1111
use PhpParser\ParserFactory;
12+
use PhpParser\PhpVersion;
1213
use PHPUnit\Framework\TestCase;
1314

1415
/**
@@ -20,7 +21,7 @@ public function testGetClassName(): void
2021
{
2122
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
2223
$propertyParser = new ClassPropertyParser(new DocCommentParser());
23-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
24+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
2425
$parser->setCode((string) file_get_contents($filePath));
2526
self::assertEquals('SomeTestClass', $parser->getClassName());
2627
self::assertEquals('SomeTestClass', $parser->getClassName());
@@ -30,7 +31,7 @@ public function testGetClassNameForInterface(): void
3031
{
3132
$filePath = __DIR__ . '/../../../example/classes/SomeTestInterface.php';
3233
$propertyParser = new ClassPropertyParser(new DocCommentParser());
33-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
34+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
3435
$parser->setCode((string) file_get_contents($filePath));
3536
self::assertNull($parser->getClassName());
3637
}
@@ -39,7 +40,7 @@ public function testGetNamespace(): void
3940
{
4041
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
4142
$propertyParser = new ClassPropertyParser(new DocCommentParser());
42-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
43+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
4344
$parser->setCode((string) file_get_contents($filePath));
4445
self::assertEquals('PhpKafka\\PhpAvroSchemaGenerator\\Example', $parser->getNamespace());
4546
self::assertEquals('PhpKafka\\PhpAvroSchemaGenerator\\Example', $parser->getNamespace());
@@ -49,7 +50,7 @@ public function testGetProperties(): void
4950
{
5051
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
5152
$propertyParser = new ClassPropertyParser(new DocCommentParser());
52-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
53+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
5354
$parser->setCode((string) file_get_contents($filePath));
5455
$properties = $parser->getProperties();
5556
self::assertCount(16, $properties);
@@ -62,7 +63,7 @@ public function testGetProperties(): void
6263
public function testClassAndNamespaceAreNullWithNoCode(): void
6364
{
6465
$propertyParser = new ClassPropertyParser(new DocCommentParser());
65-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
66+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
6667
$refObject = new \ReflectionObject($parser);
6768
$refProperty = $refObject->getProperty('statements');
6869
$refProperty->setAccessible(true);
@@ -76,7 +77,7 @@ public function testClassAndNamespaceAreNullWithNoCode(): void
7677
public function testClassWithNoParent(): void
7778
{
7879
$propertyParser = new ClassPropertyParser(new DocCommentParser());
79-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
80+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
8081
$parser->setCode('<?php class foo {}');
8182
self::assertNull($parser->getNamespace());
8283
self::assertNull($parser->getParentClassName());
@@ -87,7 +88,7 @@ public function testClassWithNoParent(): void
8788
public function testClassWithNullableType(): void
8889
{
8990
$propertyParser = new ClassPropertyParser(new DocCommentParser());
90-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
91+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
9192
$parser->setCode('
9293
<?php
9394
class foo {
@@ -102,7 +103,7 @@ class foo {
102103
public function testClassWithUnionType(): void
103104
{
104105
$propertyParser = new ClassPropertyParser(new DocCommentParser());
105-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
106+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
106107
$parser->setCode('
107108
<?php
108109
class foo {
@@ -117,7 +118,7 @@ class foo {
117118
public function testClassWithDocUnionType(): void
118119
{
119120
$propertyParser = new ClassPropertyParser(new DocCommentParser());
120-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
121+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
121122
$parser->setCode('
122123
<?php
123124
class foo {
@@ -135,7 +136,7 @@ class foo {
135136
public function testClassWithAnnotations(): void
136137
{
137138
$propertyParser = new ClassPropertyParser(new DocCommentParser());
138-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
139+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
139140
$parser->setCode('
140141
<?php
141142
class foo {
@@ -159,7 +160,7 @@ class foo {
159160
public function testClassWithNoParentFile(): void
160161
{
161162
$propertyParser = new ClassPropertyParser(new DocCommentParser());
162-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
163+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
163164
$parser->setCode('<?php class foo extends \RuntimeException {private $x;}');
164165
$properties = $parser->getProperties();
165166
self::assertEquals(1, count($properties));

tests/Integration/Parser/ClassPropertyParserTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser;
88
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
99
use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser;
10-
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
1110
use PhpParser\ParserFactory;
11+
use PhpParser\PhpVersion;
1212
use PHPUnit\Framework\TestCase;
1313

1414
/**
@@ -19,7 +19,7 @@ class ClassPropertyParserTest extends TestCase
1919
public function testNullDefaultProperty(): void
2020
{
2121
$propertyParser = new ClassPropertyParser(new DocCommentParser());
22-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
22+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
2323
$parser->setCode('
2424
<?php
2525
class foo {
@@ -37,7 +37,7 @@ class foo {
3737
public function testIntDefaultProperty(): void
3838
{
3939
$propertyParser = new ClassPropertyParser(new DocCommentParser());
40-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
40+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
4141
$parser->setCode('
4242
<?php
4343
class foo {
@@ -55,7 +55,7 @@ class foo {
5555
public function testFloatDefaultProperty(): void
5656
{
5757
$propertyParser = new ClassPropertyParser(new DocCommentParser());
58-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
58+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
5959
$parser->setCode('
6060
<?php
6161
class foo {
@@ -73,7 +73,7 @@ class foo {
7373
public function testEmptyStringDefaultProperty(): void
7474
{
7575
$propertyParser = new ClassPropertyParser(new DocCommentParser());
76-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
76+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
7777
$parser->setCode('
7878
<?php
7979
class foo {

tests/Integration/Registry/ClassRegistryTest.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry;
1414
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface;
1515
use PhpParser\ParserFactory;
16+
use PhpParser\PhpVersion;
1617
use PHPUnit\Framework\TestCase;
1718
use ReflectionClass;
1819
use SplFileInfo;
@@ -25,7 +26,7 @@ class ClassRegistryTest extends TestCase
2526
public function testClassDirectory(): void
2627
{
2728
$propertyParser = new ClassPropertyParser(new DocCommentParser());
28-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
29+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
2930
$converter = new PhpClassConverter($parser);
3031
$registry = new ClassRegistry($converter);
3132
$result = $registry->addClassDirectory('/tmp');
@@ -39,7 +40,7 @@ public function testLoad(): void
3940
$classDir = __DIR__ . '/../../../example/classes';
4041

4142
$propertyParser = new ClassPropertyParser(new DocCommentParser());
42-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
43+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
4344
$converter = new PhpClassConverter($parser);
4445
$registry = (new ClassRegistry($converter))->addClassDirectory($classDir)->load();
4546

@@ -58,7 +59,7 @@ public function testRegisterSchemaFileThatDoesntExist(): void
5859
{
5960
$fileInfo = new SplFileInfo('somenonexistingfile');
6061
$propertyParser = new ClassPropertyParser(new DocCommentParser());
61-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
62+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
6263
$converter = new PhpClassConverter($parser);
6364
$registry = new ClassRegistry($converter);
6465

@@ -73,13 +74,14 @@ public function testRegisterSchemaFileThatDoesntExist(): void
7374

7475
public function testRegisterSchemaFileThatIsNotReadable(): void
7576
{
76-
touch('testfile');
77-
chmod('testfile', 222);
77+
$filePath = '/tmp/test/testfile';
78+
touch($filePath);
79+
chmod($filePath, 222);
7880

79-
$fileInfo = new SplFileInfo('testfile');
81+
$fileInfo = new SplFileInfo($filePath);
8082

8183
$propertyParser = new ClassPropertyParser(new DocCommentParser());
82-
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
84+
$parser = new ClassParser((new ParserFactory())->createForVersion(PhpVersion::fromComponents(8,2)), $propertyParser);
8385
$converter = new PhpClassConverter($parser);
8486
$registry = new ClassRegistry($converter);
8587

@@ -94,7 +96,7 @@ public function testRegisterSchemaFileThatIsNotReadable(): void
9496
try {
9597
$method->invokeArgs($registry, [$fileInfo]);
9698
} finally {
97-
unlink('testfile');
99+
@unlink($filePath);
98100
}
99101
}
100102
}

0 commit comments

Comments
 (0)