Skip to content
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

[Serializer] fixed object normalizer for a class with cancel method #56868

Open
wants to merge 5 commits into
base: 6.4
Choose a base branch
from
Open
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 @@ -98,7 +98,10 @@ protected function extractAttributes(object $object, ?string $format = null, arr
$name = $reflMethod->name;
$attributeName = null;

if (str_starts_with($name, 'get') || str_starts_with($name, 'has') || str_starts_with($name, 'can')) {
if (
(str_starts_with($name, 'get') || str_starts_with($name, 'has') || str_starts_with($name, 'can'))
&& ctype_upper($name[3] ?? '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you said in the description methods are case insensitive in PHP, so this looks too brittle to me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I barely see any other option that won't imply having a list of exclusions. If you have any better idea, share please.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would work for me. It'd just also allow _ next to the prefix so that we don't break snake cased methods.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas isn't this a responsibility of NameConverter here?

Copy link
Member

@nicolas-grekas nicolas-grekas Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it might be more robust to check with !ctype_lower

) {
fabpot marked this conversation as resolved.
Show resolved Hide resolved
// getters, hassers and canners
$attributeName = substr($name, 3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,23 @@ public function testDenormalizeWithIgnoreAttributeAndPrivateProperties()

$this->assertEquals($expected, $obj);
}

public function testNormalizeWithMethodNamesSimilarToAccessors()
{
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$normalizer = new ObjectNormalizer($classMetadataFactory);

$object = new ObjectWithAccessorishMethods();
$normalized = $normalizer->normalize($object);

$this->assertFalse($object->isAccessorishCalled());
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
$this->assertSame([
'accessorishCalled' => false,
'tell' => true,
'class' => true,
'responsibility' => true,
], $normalized);
}
}

class ProxyObjectDummy extends ObjectDummy
Expand Down Expand Up @@ -1174,3 +1191,38 @@ class ObjectDummyWithIgnoreAttributeAndPrivateProperty

private $private = 'private';
}

class ObjectWithAccessorishMethods
{
private $accessorishCalled = false;

public function isAccessorishCalled()
{
return $this->accessorishCalled;
}

public function cancel()
{
$this->accessorishCalled = true;
}

public function hash()
{
$this->accessorishCalled = true;
}

public function canTell()
{
return true;
}

public function getClass()
{
return true;
}

public function hasResponsibility()
{
return true;
}
}
Loading