Skip to content

[Serializer] avoid calling undefined built-in is_*() functions #57320

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

Merged
merged 1 commit into from
Jun 6, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -773,11 +773,24 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
return (float) $data;
}

if ((TypeIdentifier::FALSE === $typeIdentifier && false === $data) || (TypeIdentifier::TRUE === $typeIdentifier && true === $data)) {
return $data;
}

if (('is_'.$typeIdentifier->value)($data)) {
$dataMatchesExpectedType = match ($typeIdentifier) {
TypeIdentifier::ARRAY => \is_array($data),
TypeIdentifier::BOOL => \is_bool($data),
TypeIdentifier::CALLABLE => \is_callable($data),
TypeIdentifier::FALSE => false === $data,
TypeIdentifier::FLOAT => \is_float($data),
TypeIdentifier::INT => \is_int($data),
TypeIdentifier::ITERABLE => is_iterable($data),
TypeIdentifier::MIXED => true,
TypeIdentifier::NULL => null === $data,
TypeIdentifier::OBJECT => \is_object($data),
TypeIdentifier::RESOURCE => \is_resource($data),
TypeIdentifier::STRING => \is_string($data),
TypeIdentifier::TRUE => true === $data,
default => false,
};

if ($dataMatchesExpectedType) {
return $data;
}
} catch (NotNormalizableValueException|InvalidArgumentException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ public function testNormalizationWithMaxDepthOnStdclassObjectDoesNotThrowWarning

public function testDenormalizeCollectionOfScalarTypesPropertyWithPhpDocExtractor()
{
$normalizer = new AbstractObjectNormalizerWithMetadataAndPhpDocExtractor();
$normalizer = new AbstractObjectNormalizerWithMetadataAndPropertyTypeExtractors();
$data = [
'type' => 'foo',
'values' => [
Expand All @@ -1150,7 +1150,7 @@ public function testDenormalizeCollectionOfScalarTypesPropertyWithPhpDocExtracto

public function testDenormalizeCollectionOfUnionTypesPropertyWithPhpDocExtractor()
{
$normalizer = new AbstractObjectNormalizerWithMetadataAndPhpDocExtractor();
$normalizer = new AbstractObjectNormalizerWithMetadataAndPropertyTypeExtractors();
$data = [
'values1' => [
'foo' => 'foo',
Expand All @@ -1166,6 +1166,15 @@ public function testDenormalizeCollectionOfUnionTypesPropertyWithPhpDocExtractor

$this->assertEquals($expected, $normalizer->denormalize($data, UnionCollectionDocBlockDummy::class));
}

public function testDenormalizeMixedProperty()
{
$normalizer = new AbstractObjectNormalizerWithMetadataAndPropertyTypeExtractors();
$expected = new MixedPropertyDummy();
$expected->foo = 'bar';

$this->assertEquals($expected, $normalizer->denormalize(['foo' => 'bar'], MixedPropertyDummy::class));
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down Expand Up @@ -1268,6 +1277,11 @@ class SnakeCaseNestedDummy
public $fooBar;
}

class MixedPropertyDummy
{
public mixed $foo;
}

#[DiscriminatorMap(typeProperty: 'type', mapping: [
'first' => FirstNestedDummyWithConstructorAndDiscriminator::class,
'second' => SecondNestedDummyWithConstructorAndDiscriminator::class,
Expand Down Expand Up @@ -1612,11 +1626,11 @@ public function __construct(
public array $values2;
}

class AbstractObjectNormalizerWithMetadataAndPhpDocExtractor extends AbstractObjectNormalizer
class AbstractObjectNormalizerWithMetadataAndPropertyTypeExtractors extends AbstractObjectNormalizer
{
public function __construct()
{
parent::__construct(new ClassMetadataFactory(new AttributeLoader()), null, new PropertyInfoExtractor([], [new PhpDocExtractor()]));
parent::__construct(new ClassMetadataFactory(new AttributeLoader()), null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]));
}

protected function extractAttributes(object $object, ?string $format = null, array $context = []): array
Expand Down
Loading