8000 avoid calling undefined built-in is_*() functions · symfony/symfony@aa272c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa272c6

Browse files
committed
avoid calling undefined built-in is_*() functions
1 parent fb64f33 commit aa272c6

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,24 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
773773
return (float) $data;
774774
}
775775

776-
if ((TypeIdentifier::FALSE === $typeIdentifier && false === $data) || (TypeIdentifier::TRUE === $typeIdentifier && true === $data)) {
777-
return $data;
778-
}
779-
780-
if (('is_'.$typeIdentifier->value)($data)) {
776+
$dataMatchesExpectedType = match ($typeIdentifier) {
777+
TypeIdentifier::ARRAY => \is_array($data),
778+
TypeIdentifier::BOOL => \is_bool($data),
779+
TypeIdentifier::CALLABLE => \is_callable($data),
780+
TypeIdentifier::FALSE => false === $data,
781+
TypeIdentifier::FLOAT => \is_float($data),
782+
TypeIdentifier::INT => \is_int($data),
783+
TypeIdentifier::ITERABLE => is_iterable($data),
784+
TypeIdentifier::MIXED => true,
785+
TypeIdentifier::NULL => null === $data,
786+
TypeIdentifier::OBJECT => \is_object($data),
787+
TypeIdentifier::RESOURCE => $data,
788+
TypeIdentifier::STRING => \is_string($data),
789+
TypeIdentifier::TRUE => true === $data,
790+
default => false,
791+
};
792+
793+
if ($dataMatchesExpectedType) {
781794
return $data;
782795
}
783796
} catch (NotNormalizableValueException|InvalidArgumentException $e) {

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,15 @@ public function testDenormalizeCollectionOfUnionTypesPropertyWithPhpDocExtractor
11661166

11671167
$this->assertEquals($expected, $normalizer->denormalize($data, UnionCollectionDocBlockDummy::class));
11681168
}
1169+
1170+
public function testDenormalizeMixedProperty()
1171+
{
1172+
$normalizer = new AbstractObjectNormalizerWithMetadataAndPhpDocExtractor();
1173+
$expected = new MixedPropertyDummy();
1174+
$expected->foo = 'bar';
1175+
1176+
$this->assertEquals($expected, $normalizer->denormalize(['foo' => 'bar'], MixedPropertyDummy::class));
1177+
}
11691178
}
11701179

11711180
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -1268,6 +1277,12 @@ class SnakeCaseNestedDummy
12681277
public $fooBar;
12691278
}
12701279

1280+
class MixedPropertyDummy
1281+
{
1282+
/** @var mixed */
1283+
public $foo;
1284+
}
1285+
12711286
#[DiscriminatorMap(typeProperty: 'type', mapping: [
12721287
'first' => FirstNestedDummyWithConstructorAndDiscriminator::class,
12731288
'second' => SecondNestedDummyWithConstructorAndDiscriminator::class,

0 commit comments

Comments
 (0)
0