Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['8.0', '8.1', '8.2', '8.3']
php-version: ['8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
include:
- php-version: '8.0'
composer-flags: '--prefer-stable --prefer-lowest'
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# 2.6.2

* Test with PHP 8.4 and 8.5.

# 2.6.1

* Fixed: Dates with a getter/setter where incorrectly handled in the refactoring for 2.6.0.
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class Compiler
public function __construct(
private Builder $metadataBuilder,
private DeserializerGenerator $deserializerGenerator,
private SerializerGenerator $serializerGenerator
private SerializerGenerator $serializerGenerator,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/ClassToGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ClassToGenerate implements \IteratorAggregate
public function __construct(
private GeneratorConfiguration $configuration,
private string $className,
?array $defaultVersions = null
?array $defaultVersions = null,
) {
$this->defaultVersions = null === $defaultVersions ? null : array_map('strval', $defaultVersions);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/GroupCombination.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
* If not specified, this falls back to the class default.
* If the array is not null, it must have a length > 0.
*/
private ?array $versions = null
private ?array $versions = null,
) {
sort($this->groups);

Expand Down
24 changes: 12 additions & 12 deletions src/DeserializerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
private Deserialization $templating,
array $classesToGenerate,
private string $cacheDirectory,
?GeneratorConfiguration $configuration = null
?GeneratorConfiguration $configuration = null,
) {
$this->filesystem = new Filesystem();
$this->configuration = $this->createGeneratorConfiguration($configuration, $classesToGenerate);
Expand Down Expand Up @@ -64,7 +64,7 @@ public function generate(Builder $metadataBuilder): void
private function writeFile(ClassMetadata $classMetadata): void
{
if (\count($classMetadata->getConstructorParameters())) {
throw new \Exception(sprintf('We currently do not support deserializing when the root class has a non-empty constructor. Class %s', $classMetadata->getClassName()));
throw new \Exception(\sprintf('We currently do not support deserializing when the root class has a non-empty constructor. Class %s', $classMetadata->getClassName()));
}

$functionName = self::buildDeserializerFunctionName($classMetadata->getClassName());
Expand All @@ -77,7 +77,7 @@ private function writeFile(ClassMetadata $classMetadata): void
$this->generateCodeForClass($classMetadata, $arrayPath, new ModelPath('model'))
);

$this->filesystem->dumpFile(sprintf('%s/%s.php', $this->cacheDirectory, $functionName), $code);
$this->filesystem->dumpFile(\sprintf('%s/%s.php', $this->cacheDirectory, $functionName), $code);
}

/**
Expand All @@ -87,7 +87,7 @@ private function generateCodeForClass(
ClassMetadata $classMetadata,
ArrayPath $arrayPath,
ModelPath $modelPath,
array $stack = []
array $stack = [],
): string {
$stack[$classMetadata->getClassName()] = ($stack[$classMetadata->getClassName()] ?? 0) + 1;

Expand Down Expand Up @@ -128,9 +128,9 @@ private function generateCodeForClass(
continue;
}
if ($definition->isRequired()) {
$msg = sprintf('Unknown constructor argument "%s". Class %s only has properties that tell how to handle %s.', $definition->getName(), $classMetadata->getClassName(), implode(', ', array_keys($constructorArgumentNames)));
$msg = \sprintf('Unknown constructor argument "%s". Class %s only has properties that tell how to handle %s.', $definition->getName(), $classMetadata->getClassName(), implode(', ', array_keys($constructorArgumentNames)));
if ($overwrittenNames) {
$msg .= sprintf(' Multiple definitions for fields %s seen - the last one overwrites previous ones.', implode(', ', array_keys($overwrittenNames)));
$msg .= \sprintf(' Multiple definitions for fields %s seen - the last one overwrites previous ones.', implode(', ', array_keys($overwrittenNames)));
}
throw new \Exception($msg);
}
Expand All @@ -150,7 +150,7 @@ private function generateCodeForProperty(
PropertyMetadata $propertyMetadata,
ArrayPath $arrayPath,
ModelPath $modelPath,
array $stack
array $stack,
): string {
if ($propertyMetadata->isReadOnly()) {
return '';
Expand Down Expand Up @@ -184,7 +184,7 @@ private function generateCodeForField(
PropertyMetadata $propertyMetadata,
ArrayPath $arrayPath,
ModelPath $modelPath,
array $stack
array $stack,
): string {
return $this->templating->renderConditional(
(string) $arrayPath,
Expand All @@ -199,7 +199,7 @@ private function generateInnerCodeForFieldType(
PropertyMetadata $propertyMetadata,
ArrayPath $arrayPath,
ModelPath $modelPropertyPath,
array $stack
array $stack,
): string {
$type = $propertyMetadata->getType();

Expand Down Expand Up @@ -241,7 +241,7 @@ private function generateCodeForArray(
PropertyTypeArray $type,
ArrayPath $arrayPath,
ModelPath $modelPath,
array $stack
array $stack,
): string {
if ($type->getSubType() instanceof PropertyTypePrimitive) {
// for arrays of scalars, copy the field even when its an empty array
Expand Down Expand Up @@ -287,7 +287,7 @@ private function generateCodeForArrayCollection(
PropertyTypeArray $type,
ArrayPath $arrayPath,
ModelPath $modelPath,
array $stack
array $stack,
): string {
$tmpVariable = ModelPath::tempVariable([(string) $modelPath, $propertyMetadata->getName()]);
$innerCode = $this->generateCodeForArray($type, $arrayPath, $tmpVariable, $stack);
Expand All @@ -304,7 +304,7 @@ private function generateCodeForArrayCollection(
*/
private function createGeneratorConfiguration(
?GeneratorConfiguration $configuration,
array $classesToGenerate
array $classesToGenerate,
): GeneratorConfiguration {
if (null === $configuration) {
$configuration = new GeneratorConfiguration([], []);
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/UnsupportedTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class UnsupportedTypeException extends Exception

public static function typeUnsupportedDeserialization(string $type): self
{
return new self(sprintf(self::UNSUPPORTED_TYPE_DESERIALIZATION, $type));
return new self(\sprintf(self::UNSUPPORTED_TYPE_DESERIALIZATION, $type));
}

/**
Expand All @@ -29,6 +29,6 @@ public static function typeUnsupportedSerialization(string $type, ?string $versi
$versionInfo = $version ?: '[no version]';
$groupInfo = \count($groups) ? implode(', ', $groups) : '[no groups]';

return new self(sprintf(self::UNSUPPORTED_TYPE_SERIALIZATION, $type, $versionInfo, $groupInfo));
return new self(\sprintf(self::UNSUPPORTED_TYPE_SERIALIZATION, $type, $versionInfo, $groupInfo));
}
}
2 changes: 1 addition & 1 deletion src/Path/ArrayEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Liip\Serializer\Path;

final class ArrayEntry extends AbstractEntry
final class ArrayEntry extends AbstractEntry implements \Stringable
{
public function __toString(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Path/ModelEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Liip\Serializer\Path;

final class ModelEntry extends AbstractEntry
final class ModelEntry extends AbstractEntry implements \Stringable
{
public function __toString(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Path/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Liip\Serializer\Path;

final class Root extends AbstractEntry
final class Root extends AbstractEntry implements \Stringable
{
public function __toString(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Recursion.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class Recursion
public static function check(string $className, array $stack, string $modelPath): bool
{
if (\array_key_exists($className, $stack) && $stack[$className] > 1) {
throw new \Exception(sprintf('recursion for %s at %s', key($stack), $modelPath));
throw new \Exception(\sprintf('recursion for %s at %s', key($stack), $modelPath));
}

return false;
Expand Down
10 changes: 5 additions & 5 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function serialize(mixed $data, string $format, ?Context $context = null)
try {
return Json::encode($this->objectToArray($data, true, $context), \JSON_UNESCAPED_SLASHES);
} catch (\JsonException $e) {
throw new Exception(sprintf('Failed to JSON encode data for %s. This is not supposed to happen.', get_debug_type($data)), 0, $e);
throw new Exception(\sprintf('Failed to JSON encode data for %s. This is not supposed to happen.', get_debug_type($data)), 0, $e);
}
}

Expand Down Expand Up @@ -94,14 +94,14 @@ private function arrayToObject(array $data, string $type, ?Context $context): mi
}

$functionName = DeserializerGenerator::buildDeserializerFunctionName($type);
$filename = sprintf('%s/%s.php', $this->cacheDirectory, $functionName);
$filename = \sprintf('%s/%s.php', $this->cacheDirectory, $functionName);
if (!file_exists($filename)) {
throw UnsupportedTypeException::typeUnsupportedDeserialization($type);
}
require_once $filename;

if (!\is_callable($functionName)) {
throw new Exception(sprintf('Internal Error: Deserializer for %s in file %s does not have expected function %s', $type, $filename, $functionName));
throw new Exception(\sprintf('Internal Error: Deserializer for %s in file %s does not have expected function %s', $type, $filename, $functionName));
}

try {
Expand Down Expand Up @@ -129,15 +129,15 @@ private function objectToArray(mixed $data, bool $useStdClass, ?Context $context
}
}
$functionName = SerializerGenerator::buildSerializerFunctionName($type, $version ?: null, $groups);
$filename = sprintf('%s/%s.php', $this->cacheDirectory, $functionName);
$filename = \sprintf('%s/%s.php', $this->cacheDirectory, $functionName);
if (!file_exists($filename)) {
throw UnsupportedTypeException::typeUnsupportedSerialization($type, $version, $groups);
}

require_once $filename;

if (!\is_callable($functionName)) {
throw new Exception(sprintf('Internal Error: Serializer for %s in file %s does not have expected function %s', $type, $filename, $functionName));
throw new Exception(\sprintf('Internal Error: Serializer for %s in file %s does not have expected function %s', $type, $filename, $functionName));
}

try {
Expand Down
16 changes: 8 additions & 8 deletions src/SerializerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class SerializerGenerator
public function __construct(
private Serialization $templating,
private GeneratorConfiguration $configuration,
private string $cacheDirectory
private string $cacheDirectory,
) {
$this->filesystem = new Filesystem();
}
Expand Down Expand Up @@ -95,7 +95,7 @@ private function writeFile(
string $className,
?string $apiVersion,
array $serializerGroups,
ClassMetadata $classMetadata
ClassMetadata $classMetadata,
): void {
$functionName = self::buildSerializerFunctionName($className, $apiVersion, $serializerGroups);

Expand All @@ -105,7 +105,7 @@ private function writeFile(
$this->generateCodeForClass($classMetadata, $apiVersion, $serializerGroups, '', '$model')
);

$this->filesystem->dumpFile(sprintf('%s/%s.php', $this->cacheDirectory, $functionName), $code);
$this->filesystem->dumpFile(\sprintf('%s/%s.php', $this->cacheDirectory, $functionName), $code);
}

/**
Expand All @@ -118,7 +118,7 @@ private function generateCodeForClass(
array $serializerGroups,
string $arrayPath,
string $modelPath,
array $stack = []
array $stack = [],
): string {
$stack[$classMetadata->getClassName()] = ($stack[$classMetadata->getClassName()] ?? 0) + 1;

Expand All @@ -140,7 +140,7 @@ private function generateCodeForField(
array $serializerGroups,
string $arrayPath,
string $modelPath,
array $stack
array $stack,
): string {
if (Recursion::hasMaxDepthReached($propertyMetadata, $stack)) {
return '';
Expand All @@ -158,7 +158,7 @@ private function generateCodeForField(
);
}
if (!$propertyMetadata->isPublic()) {
throw new \Exception(sprintf('Property %s is not public and no getter has been defined. Stack %s', $modelPropertyPath, var_export($stack, true)));
throw new \Exception(\sprintf('Property %s is not public and no getter has been defined. Stack %s', $modelPropertyPath, var_export($stack, true)));
}

return $this->templating->renderConditional(
Expand All @@ -177,7 +177,7 @@ private function generateCodeForFieldType(
array $serializerGroups,
string $fieldPath,
string $modelPropertyPath,
array $stack
array $stack,
): string {
switch ($type) {
case $type instanceof PropertyTypeDateTime:
Expand Down Expand Up @@ -214,7 +214,7 @@ private function generateCodeForArray(
array $serializerGroups,
string $arrayPath,
string $modelPath,
array $stack
array $stack,
): string {
$index = '$index'.mb_strlen($arrayPath);
$subType = $type->getSubType();
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/AccessorOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
*
* @Serializer\Until("1")
*/
public ?string $apiString2
public ?string $apiString2,
) {
$this->apiString1 = $apiString1;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/NonEmptyConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(
/**
* @Serializer\Type("string")
*/
private string $optional = 'optional'
private string $optional = 'optional',
) {
}

Expand Down