8000 [PropertyInfo] Make isWriteable() more consistent with isReadable() when checking snake_case properties by jbtronics · Pull Request #51697 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyInfo] Make isWriteable() more consistent with isReadable() when checking snake_case properties #51697

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
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/PropertyInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Make properties writable when a setter in camelCase exists, similar to the camelCase getter

6.1
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ public function isWritable(string $class, string $property, array $context = [])
return true;
}

// First test with the camelized property name
[$reflectionMethod] = $this->getMutatorMethod($class, $this->camelize($property));
if (null !== $reflectionMethod) {
return true;
}

// Otherwise check for the old way
[$reflectionMethod] = $this->getMutatorMethod($class, $property);

return null !== $reflectionMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php7Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php7ParentDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php81Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\SnakeCaseDummy;
use Symfony\Component\PropertyInfo\Type;

/**
Expand Down Expand Up @@ -409,6 +410,20 @@ public static function getWritableProperties()
];
}

public function testIsReadableSnakeCase()
{
$this->assertTrue($this->extractor->isReadable(SnakeCaseDummy::class, 'snake_property'));
$this->assertTrue($this->extractor->isReadable(SnakeCaseDummy::class, 'snake_readonly'));
}

public function testIsWriteableSnakeCase()
{
$this->assertTrue($this->extractor->isWritable(SnakeCaseDummy::class, 'snake_property'));
$this->assertFalse($this->extractor->isWritable(SnakeCaseDummy::class, 'snake_readonly'));
// Ensure that it's still possible to write to the property using the (old) snake name
$this->assertTrue($this->extractor->isWritable(SnakeCaseDummy::class, 'snake_method'));
}

public function testSingularize()
{
$this->assertTrue($this->extractor->isWritable(AdderRemoverDummy::class, 'analyses'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyInfo\Tests\Fixtures;

class SnakeCaseDummy
{
private string $snake_property;
private string $snake_readOnly;
private string $snake_method;

public function getSnakeProperty()
{
return $this->snake_property;
}

public function getSnakeReadOnly()
{
return $this->snake_readOnly;
}

public function setSnakeProperty($snake_property)
{
$this->snake_property = $snake_property;
}

public function setSnake_method($snake_method)
{
$this->snake_method = $snake_method;
}
}
0