Skip to content

Commit f349ff5

Browse files
committed
Add support for php 8.1 features without breaking older php versions
- Refactor unit tests in order to remove php 8.1 specific tests from older php versions - Conditionally remove or add code to static analysis based on the current php version - Update minimum phpunit version
1 parent 969d8f7 commit f349ff5

29 files changed

+451
-174
lines changed

.php-cs-fixer.dist.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
->in(['src', 'test'])
77
;
88

9+
if (version_compare(PHP_VERSION, '8.1') < 0) {
10+
$finder = $finder
11+
->notPath('Objects/Schema/Generation/Attributes')
12+
->notPath('Objects/Schema/Generation/AttributeReader.php')
13+
->notPath('Objects/Schema/Generation/Fixture/Attributes');
14+
}
15+
916
return (new PhpCsFixer\Config())
1017
->setRules([
1118
'@Symfony' => true,

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ARG PHP_VERSION=7.4
22

3-
FROM php:${PHP_VERSION}-cli-alpine
3+
FROM php:${PHP_VERSION}-cli-alpine3.13
44

55
ARG XDEBUG_VERSION=3.1.1
66

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ZOOKEEPER_IPV4 ?= 192.168.104.101
1111
COMPOSER ?= bin/composer.phar
1212
COMPOSER_VERSION ?= 2.1.9
1313
PHP_STAN ?= bin/phpstan.phar
14-
PHP_STAN_VERSION ?= 0.12.99
14+
PHP_STAN_VERSION ?= 1.0.1
1515
PHP_CS_FIXER ?= bin/php-cs-fixer.phar
1616
PHP_CS_FIXER_VERSION ?= 3.2.1
1717
PHPUNIT ?= vendor/bin/phpunit

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"widmogrod/php-functional": "^6.0"
3232
},
3333
"require-dev": {
34-
"phpunit/phpunit": "^8.2.3|^9.4.2",
34+
"phpunit/phpunit": "^9.5.10",
3535
"phpbench/phpbench": "1.0.0-alpha2",
3636
"vlucas/phpdotenv": "~2.4",
3737
"symfony/serializer": "^3.4|^4.3",

phpstan-conditional.config.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
$config = [];
5+
6+
if (version_compare(PHP_VERSION, '8.1') < 0) {
7+
$config['parameters']['excludePaths'] = [
8+
'analyseAndScan' => [
9+
'src/Objects/Schema/Generation/Attributes/',
10+
'src/Objects/Schema/Generation/AttributeReader.php'
11+
],
12+
];
13+
}
14+
15+
return $config;

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
includes:
2+
- phpstan-conditional.config.php
13
parameters:
24
level: 8
35
paths: [ src ]

src/Objects/Exceptions/Exceptions.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ final class Exceptions
88
{
99
public const ERROR_ENCODING = 501;
1010
public const ERROR_DECODING = 502;
11+
public const ERROR_PHP_VERSION = 503;
1112

1213
/**
1314
* @param mixed $record
@@ -41,4 +42,19 @@ public static function forDecode(string $binaryMessage, \Exception $previous = n
4142

4243
return new AvroDecodingException($message, self::ERROR_DECODING, $previous);
4344
}
45+
46+
public static function forPhpVersion(string $currentVersion, string $minimumVersion): UnsupportedPhpVersionException
47+
{
48+
$message = sprintf(
49+
'The current php version \'%s\' is not supported for this feature. ' .
50+
'Minimum supported version is \'%s\'',
51+
$currentVersion,
52+
$minimumVersion,
53+
);
54+
55+
return new UnsupportedPhpVersionException(
56+
$message,
57+
self::ERROR_PHP_VERSION
58+
);
59+
}
4460
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\AvroSerializer\Objects\Exceptions;
6+
7+
use RuntimeException;
8+
9+
class UnsupportedPhpVersionException extends RuntimeException
10+
{
11+
}

src/Objects/Schema/Generation/Annotations/AvroItems.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
*/
1414
final class AvroItems implements TypeOnlyAttribute
1515
{
16+
/**
17+
* @var mixed
18+
*/
1619
public $value;
1720

1821
public function value(): array
1922
{
20-
$value = is_array($this->value) ? $this->value : [$this->value];
23+
$value = \is_array($this->value) ? $this->value : [$this->value];
2124

2225
return array_map(function ($value) {
2326
if ($value instanceof AvroType) {

src/Objects/Schema/Generation/Annotations/AvroValues.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
*/
1414
final class AvroValues implements TypeOnlyAttribute
1515
{
16+
/**
17+
* @var mixed
18+
*/
1619
public $value;
1720

1821
public function value(): array
1922
{
20-
$value = is_array($this->value) ? $this->value : [$this->value];
23+
$value = \is_array($this->value) ? $this->value : [$this->value];
2124

2225
return array_map(function ($value) {
2326
if ($value instanceof AvroType) {

0 commit comments

Comments
 (0)