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 support for php 8 attributes #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jobs:
-
version: 8.0
xdebug: 3.1.1

-
version: 8.1.0RC3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version: 8.1.0RC3
version: 8.1.0

Now that 8.1 is out

xdebug: 3.1.1
runs-on: ubuntu-20.04
steps:
-
Expand Down Expand Up @@ -81,12 +85,18 @@ jobs:
-
version: 8.0
composer: --prefer-lowest
-
version: 8.1.0RC3
composer: --prefer-lowest
-
version: 7.4
composer: --prefer-stable
-
version: 8.0
composer: --prefer-stable
-
version: 8.1.0RC3
composer: --prefer-stable
steps:
-
name: Download sources
Expand Down Expand Up @@ -190,6 +200,7 @@ jobs:
run: |
chmod a+x bin/wait-for-all.sh bin/wait-for-it.sh
make platform
make platform-logs
docker run -i --rm --net=host --sig-proxy=true --pid=host \
-v "${GITHUB_WORKSPACE}":"${GITHUB_WORKSPACE}" -w "${GITHUB_WORKSPACE}" php-avro-serde:7.4 \
vendor/bin/phpunit --group integration
7 changes: 7 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
->in(['src', 'test'])
;

if (version_compare(PHP_VERSION, '8.1') < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we make it work on 8.0 if we don't use nested attributes?

$finder = $finder
->notPath('Objects/Schema/Generation/Attributes')
->notPath('Objects/Schema/Generation/AttributeReader.php')
->notPath('Objects/Schema/Generation/Fixture/Attributes');
}

return (new PhpCsFixer\Config())
->setRules([
'@Symfony' => true,
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ARG PHP_VERSION=7.4

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

ARG XDEBUG_VERSION=3.1.1

Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ZOOKEEPER_IPV4 ?= 192.168.104.101
COMPOSER ?= bin/composer.phar
COMPOSER_VERSION ?= 2.1.9
PHP_STAN ?= bin/phpstan.phar
PHP_STAN_VERSION ?= 0.12.99
PHP_STAN_VERSION ?= 1.0.1
PHP_CS_FIXER ?= bin/php-cs-fixer.phar
PHP_CS_FIXER_VERSION ?= 3.2.1
PHPUNIT ?= vendor/bin/phpunit
Expand Down Expand Up @@ -75,6 +75,10 @@ platform:
docker-compose up -d
bin/wait-for-all.sh

platform-logs:
docker-compose ps
docker-compose logs schema_registry

clean:
rm -rf build
docker-compose down
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"widmogrod/php-functional": "^6.0"
},
"require-dev": {
"phpunit/phpunit": "^8.2.3|^9.4.2",
"phpunit/phpunit": "^9.5.10",
"phpbench/phpbench": "1.0.0-alpha2",
"vlucas/phpdotenv": "~2.4",
"symfony/serializer": "^3.4|^4.3",
Expand Down
15 changes: 15 additions & 0 deletions phpstan-conditional.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types = 1);

$config = [];

if (version_compare(PHP_VERSION, '8.1') < 0) {
$config['parameters']['excludePaths'] = [
'analyseAndScan' => [
'src/Objects/Schema/Generation/Attributes/',
'src/Objects/Schema/Generation/AttributeReader.php'
],
];
}

return $config;
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
includes:
- phpstan-conditional.config.php
parameters:
level: 8
paths: [ src ]
16 changes: 16 additions & 0 deletions src/Objects/Exceptions/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ final class Exceptions
{
public const ERROR_ENCODING = 501;
public const ERROR_DECODING = 502;
public const ERROR_PHP_VERSION = 503;

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

return new AvroDecodingException($message, self::ERROR_DECODING, $previous);
}

public static function forPhpVersion(string $currentVersion, string $minimumVersion): UnsupportedPhpVersionException
{
$message = sprintf(
'The current php version \'%s\' is not supported for this feature. ' .
'Minimum supported version is \'%s\'',
$currentVersion,
$minimumVersion,
);

return new UnsupportedPhpVersionException(
$message,
self::ERROR_PHP_VERSION
);
}
}
11 changes: 11 additions & 0 deletions src/Objects/Exceptions/UnsupportedPhpVersionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Exceptions;

use RuntimeException;

class UnsupportedPhpVersionException extends RuntimeException
{
}
24 changes: 23 additions & 1 deletion src/Objects/Schema/Generation/Annotations/AvroItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,36 @@
namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Annotations;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;
use FlixTech\AvroSerializer\Objects\Schema\Generation\TypeOnlyAttribute;

/**
* @Annotation
*/
final class AvroItems implements TypeOnlyAttribute
{
use ContainsOnlyTypes;
/**
* @var mixed
*/
public $value;

public function value(): array
{
$value = \is_array($this->value) ? $this->value : [$this->value];

return array_map(function ($value) {
if ($value instanceof AvroType) {
return $value;
}

return AvroType::create($value);
}, $value);
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes(...$this->value());
}

/**
* {@inheritdoc}
Expand Down
24 changes: 23 additions & 1 deletion src/Objects/Schema/Generation/Annotations/AvroValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,36 @@
namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Annotations;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;
use FlixTech\AvroSerializer\Objects\Schema\Generation\TypeOnlyAttribute;

/**
* @Annotation
*/
final class AvroValues implements TypeOnlyAttribute
{
use ContainsOnlyTypes;
/**
* @var mixed
*/
public $value;

public function value(): array
{
$value = \is_array($this->value) ? $this->value : [$this->value];

return array_map(function ($value) {
if ($value instanceof AvroType) {
return $value;
}

return AvroType::create($value);
}, $value);
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes(...$this->value());
}

/**
* {@inheritdoc}
Expand Down
50 changes: 0 additions & 50 deletions src/Objects/Schema/Generation/Annotations/ContainsOnlyTypes.php

This file was deleted.

44 changes: 44 additions & 0 deletions src/Objects/Schema/Generation/AttributeReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation;

use FlixTech\AvroSerializer\Objects\Exceptions\Exceptions;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionProperty;

class AttributeReader implements SchemaAttributeReader
{
private const MINIMUM_REQUIRED_VERSION = '8.1';

public function __construct()
{
if (version_compare(PHP_VERSION, self::MINIMUM_REQUIRED_VERSION) < 0) {
throw Exceptions::forPhpVersion(
PHP_VERSION,
self::MINIMUM_REQUIRED_VERSION,
);
}
}

public function readClassAttributes(ReflectionClass $class): SchemaAttributes
{
$attributes = $class->getAttributes();
return $this->getSchemaAttributes(...$attributes);
}

public function readPropertyAttributes(ReflectionProperty $property): SchemaAttributes
{
$attributes = $property->getAttributes();
return $this->getSchemaAttributes(...$attributes);
}

private function getSchemaAttributes(ReflectionAttribute ...$attributes): SchemaAttributes
{
$attributes = array_map(fn($attr) => $attr->newInstance(), $attributes);
$attributes = array_filter($attributes, fn($attr) => $attr instanceof SchemaAttribute);
return new SchemaAttributes(...$attributes);
}
}
50 changes: 50 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;
use FlixTech\AvroSerializer\Objects\Schema\Generation\VariadicAttribute;

#[\Attribute]
final class AvroAliases implements VariadicAttribute
{
/**
* @var array<string>
*/
public array $aliases;

public function __construct(
string ...$aliases,
) {
$this->aliases = $aliases;
}

/**
* {@inheritdoc}
*/
public function name(): string
{
return AttributeName::ALIASES;
}

/**
* {@inheritdoc}
*
* @return array<string>
*/
public function value(): array
{
return $this->aliases;
}

/**
* {@inheritdoc}
*/
public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
Loading