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
6 changes: 3 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
strategy:
matrix:
include:
- php-version: 8.2
docker-image: 'anzusystems/php:3.0.0-php82-cli'
- php-version: 8.3
docker-image: 'anzusystems/php:3.0.0-php83-cli'
docker-image: 'anzusystems/php:4.0.0-php83-cli'
- php-version: 8.4
docker-image: 'anzusystems/php:4.0.0-php84-cli'

services:
mysql:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM anzusystems/php:3.0.0-php83-cli
FROM anzusystems/php:4.0.0-php84-cli
#
### Basic arguments and variables
ARG DOCKER_USER_ID
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": ">=8.2",
"php": ">=8.3",
"ext-json": "*",
"doctrine/common": "^3.3",
"symfony/property-info": "^6.3|^7.0"
Expand All @@ -25,7 +25,7 @@
"symfony/test-pack": "^1.1",
"symfony/uid": "^6.3|^7.0",
"symplify/easy-coding-standard": "^12.0",
"vimeo/psalm": "^5.16"
"vimeo/psalm": "^6.8"
},
"suggest": {
"doctrine/orm": "Enable EntityIdHandler."
Expand Down
16 changes: 2 additions & 14 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
usePhpDocMethodsWithoutMagicCall="true"
allowStringToStandInForClass="false"
memoizeMethodCallResults="true"
phpVersion="8.4"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand All @@ -24,22 +25,9 @@
<issueHandlers>
<MoreSpecificImplementedParamType errorLevel="suppress"/>
<RiskyTruthyFalsyComparison errorLevel="suppress"/>
<MissingOverrideAttribute errorLevel="suppress"/>
<UnnecessaryVarAnnotation errorLevel="suppress"/> <!-- PHPStorm doesn't understand to generics annotations yet -->

<UnusedFunctionCall>
<errorLevel type="suppress">
<referencedFunction name="array_map"/>
</errorLevel>
</UnusedFunctionCall>

<PossiblyUndefinedMethod>
<errorLevel type="suppress">
<referencedMethod name="Symfony\Component\Config\Definition\Builder\NodeParentInterface::end"/>
<referencedMethod name="Symfony\Component\Config\Definition\Builder\NodeDefinition::children"/>
<referencedMethod name="Symfony\Component\Config\Definition\Builder\NodeDefinition::canbeenabled"/>
</errorLevel>
</PossiblyUndefinedMethod>

<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->

<DeprecatedMethod errorLevel="info"/>
Expand Down
10 changes: 10 additions & 0 deletions src/Handler/Handlers/ObjectHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ public function deserialize(mixed $value, Metadata $metadata): object|iterable
return $collection;
}
if (Type::BUILTIN_TYPE_ARRAY === $metadata->type) {
if ($metadata->customType || $metadata->discriminatorMap) {
$array = [];
foreach ($value as $key => $item) {
/** @psalm-suppress ArgumentTypeCoercion */
$array[$key] = $this->jsonDeserializer->fromArray($item, $this->getDeserializeCustomType($item, $metadata) ?? $metadata->type);
}

return $array;
}

return $value;
}

Expand Down
1 change: 1 addition & 0 deletions src/Metadata/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(
public ?string $persistedName = null,
public ?array $discriminatorMap = null,
public ?array $orderBy = null,
public bool $getterSetterStrategy = true,
) {
}
}
51 changes: 34 additions & 17 deletions src/Metadata/MetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ReflectionUnionType;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\PropertyInfo\Type;
use const PHP_VERSION;

final class MetadataFactory
{
Expand Down Expand Up @@ -130,7 +131,7 @@ private function buildMethodMetadata(ReflectionClass $reflection): array
/** @var Serialize $attribute */
$attribute = $attributes[0]->newInstance();
$dataName = $attribute->serializedName
?? lcfirst(preg_replace('~^[get|is]*(.+)~', '$1', $method->getName()))
?? lcfirst((string) preg_replace('~^[get|is]*(.+)~', '$1', $method->getName()))
;
$metadata[$dataName] = $this->getMethodMetadata($method, $attribute);
}
Expand Down Expand Up @@ -171,7 +172,7 @@ private function getMethodMetadata(ReflectionMethod $method, Serialize $attribut
$attribute->handler,
$this->resolveCustomType($attribute),
$attribute->strategy,
orderBy: $attribute->orderBy
orderBy: $attribute->orderBy,
);
}

Expand Down Expand Up @@ -201,35 +202,51 @@ private function getPropertyMetadata(ReflectionProperty $property, Serialize $at
}
}
}
$getter = $getterPrefix . ucfirst($property->getName());
$declaringClass = $property->getDeclaringClass();
if (false === $declaringClass->hasMethod($getter)) {
// fallback to "get" prefix
$getterFallback = 'get' . ucfirst($property->getName());
if (false === $declaringClass->hasMethod($getterFallback)) {
throw new SerializerException('Getter method ' . $getter . ' or ' . $getterFallback . ' not found in ' . $declaringClass->getName() . '.');
$getter = $setter = null;
$getterSetterStrategy = true;
if (version_compare(PHP_VERSION, '8.4.0', '>=') && $property->hasHooks()) {
$getterSetterStrategy = false;
/** @psalm-suppress UndefinedClass */
if ($property->hasHook(\PropertyHookType::Get)) {
$getter = $property->getName();
}
/** @psalm-suppress UndefinedClass */
if ($property->hasHook(\PropertyHookType::Set)) {
$setter = $property->getName();
}

$getter = $getterFallback;
}
$setter = 'set' . ucfirst($property->getName());
if (false === $declaringClass->hasMethod($setter)) {
// setter is required for deserialization only
$setter = null;
if ($getterSetterStrategy) {
$getter = $getterPrefix . ucfirst($property->getName());
$declaringClass = $property->getDeclaringClass();
if (false === $declaringClass->hasMethod($getter)) {
// fallback to "get" prefix
$getterFallback = 'get' . ucfirst($property->getName());
if (false === $declaringClass->hasMethod($getterFallback)) {
throw new SerializerException('Getter method ' . $getter . ' or ' . $getterFallback . ' not found in ' . $declaringClass->getName() . '.');
}

$getter = $getterFallback;
}
$setter = 'set' . ucfirst($property->getName());
if (false === $declaringClass->hasMethod($setter)) {
// setter is required for deserialization only
$setter = null;
}
}

return new Metadata(
$type,
(bool) $propertyType?->allowsNull(),
$getter,
(string) $getter,
$property->getName(),
$setter,
$attribute->handler,
$this->resolveCustomType($attribute),
$attribute->strategy,
$attribute->persistedName,
$attribute->discriminatorMap,
orderBy: $attribute->orderBy
orderBy: $attribute->orderBy,
getterSetterStrategy: $getterSetterStrategy,
);
}

Expand Down
7 changes: 6 additions & 1 deletion src/Metadata/MetadataRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function get(string $className): ClassMetadata
}
if (false === isset($this->metadata[$className])) {
try {
$cachedItem = $this->appCache->getItem(self::CACHE_PREFIX . $className);
$cachedItem = $this->appCache->getItem($this->getCacheKey($className));
if ($cachedItem->isHit()) {
$this->metadata[$className] = $cachedItem->get();

Expand All @@ -55,4 +55,9 @@ public function get(string $className): ClassMetadata

return $this->metadata[$className];
}

private function getCacheKey(string $className): string
{
return self::CACHE_PREFIX . str_replace('\\', '_', $className);
}
}
2 changes: 1 addition & 1 deletion src/Service/JsonDeserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function arrayToObject(array $data, string $className): object
}

try {
$object->{$metadata->setter}($value);
$metadata->getterSetterStrategy ? $object->{$metadata->setter}($value) : $object->{$metadata->property} = $value;
} catch (Throwable $exception) {
throw new SerializerException('Unable to deserialize "' . $name . '". Check type.', 0, $exception);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Service/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function objectToArray(object $data, SerializationContext $context): arr
{
$output = [];
foreach ($this->metadataRegistry->get($data::class)->getAll() as $name => $metadata) {
$value = $data->{$metadata->getter}();
$value = $metadata->getterSetterStrategy ? $data->{$metadata->getter}() : $data->{$metadata->property};

if (null === $value && !$context->shouldSerializeNull()) {
continue;
Expand Down
Loading