Skip to content

Commit 50f2452

Browse files
committed
GetSetMethodNormalizer + added case with is*
1 parent ebfbf16 commit 50f2452

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ private function isGetMethod(\ReflectionMethod $method): bool
105105
return !$method->isStatic()
106106
&& !($method->getAttributes(Ignore::class) || $method->getAttributes(LegacyIgnore::class))
107107
&& !$method->getNumberOfRequiredParameters()
108-
&& ((2 < ($methodLength = \strlen($method->name)) && str_starts_with($method->name, 'is'))
109-
|| (3 < $methodLength && (str_starts_with($method->name, 'has') || str_starts_with($method->name, 'get')))
108+
&& ((2 < ($methodLength = \strlen($method->name)) && str_starts_with($method->name, 'is') && !ctype_lower($method->name[2]))
109+
|| (3 < $methodLength && (str_starts_with($method->name, 'has') || str_starts_with($method->name, 'get')) && !ctype_lower($method->name[3]))
110110
);
111111
}
112112

@@ -118,7 +118,9 @@ private function isSetMethod(\ReflectionMethod $method): bool
118118
return !$method->isStatic()
119119
&& !$method->getAttributes(Ignore::class)
120120
&& 0 < $method->getNumberOfParameters()
121-
&& str_starts_with($method->name, 'set');
121+
&& str_starts_with($method->name, 'set')
122+
&& !ctype_lower($method->name[3])
123+
;
122124
}
123125

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

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,14 @@ public function testNormalizeWithDiscriminator()
515515
$this->assertSame(['type' => 'one', 'url' => 'URL_ONE'], $normalizer->normalize(new GetSetMethodDiscriminatedDummyOne()));
516516
}
517517

518+
public function testNormalizeWithMethodNamesSimilarToAccessors()
519+
{
520+
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
521+
$normalizer = new GetSetMethodNormalizer($classMetadataFactory);
522+
523+
$this->assertSame(['class' => 'class', 123 => 123], $normalizer->normalize(new GetSetWithAccessorishMethod()));
524+
}
525+
518526
public function testDenormalizeWithDiscriminator()
519527
{
520528
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
@@ -902,3 +910,49 @@ public function setBar($bar = null, $other = true)
902910
$this->bar = $bar;
903911
}
904912
}
913+
914+
class GetSetWithAccessorishMethod
915+
{
916+
public function cancel()
917+
{
918+
return 'cancel';
919+
}
920+
921+
public function hash()
922+
{
923+
return 'hash';
924+
}
925+
926+
public function getClass()
927+
{
928+
return 'class';
929+
}
930+
931+
public function setClass()
932+
{
933+
}
934+
935+
public function get123()
936+
{
937+
return 123;
938+
}
939+
940+
public function set123()
941+
{
942+
}
943+
944+
public function gettings()
945+
{
946+
947+
}
948+
949+
public function settings()
950+
{
951+
952+
}
953+
954+
public function isolate()
955+
{
956+
957+
}
958+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,4 +1271,19 @@ public function get123()
12711271
{
12721272
return 321;
12731273
}
1274+
1275+
public function gettings()
1276+
{
1277+
$this->accessorishCalled = true;
1278+
}
1279+
1280+
public function settings()
1281+
{
1282+
$this->accessorishCalled = true;
1283+
}
1284+
1285+
public function isolate()
1286+
{
1287+
$this->accessorishCalled = true;
1288+
}
12741289
}

0 commit comments

Comments
 (0)