Skip to content
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 @@ -757,6 +757,10 @@ private function isMethodAccessible(\ReflectionClass $class, string $methodName,
*/
private function camelize(string $string): string
{
if ('' === ltrim($string, '_')) {
return $string;
}

return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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\Tests\Fixtures\UnderscoreDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\VirtualProperties;
use Symfony\Component\PropertyInfo\Type;

Expand Down Expand Up @@ -361,31 +362,31 @@ public static function defaultValueProvider()
/**
* @dataProvider getReadableProperties
*/
public function testIsReadable($property, $expected)
public function testIsReadable(string $class, string $property, bool $expected)
{
$this->assertSame(
$expected,
$this->extractor->isReadable('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', $property, [])
);
$this->assertSame($expected, $this->extractor->isReadable($class, $property, []));
}

public static function getReadableProperties()
{
return [
['bar', false],
['baz', false],
['parent', true],
['a', true],
['b', false],
['c', true],
['d', true],
['e', false],
['f', false],
['Id', true],
['id', true],
['Guid', true],
['guid', false],
['element', false],
[Dummy::class, 'bar', false],
[Dummy::class, 'baz', false],
[Dummy::class, 'parent', true],
[Dummy::class, 'a', true],
[Dummy::class, 'b', false],
[Dummy::class, 'c', true],
[Dummy::class, 'd', true],
[Dummy::class, 'e', false],
[Dummy::class, 'f', false],
[Dummy::class, 'Id', true],
[Dummy::class, 'id', true],
[Dummy::class, 'Guid', true],
[Dummy::class, 'guid', false],
[Dummy::class, 'element', false],
[UnderscoreDummy::class, '_', true],
[UnderscoreDummy::class, '__', true],
[UnderscoreDummy::class, '___', false],
];
}

Expand Down Expand Up @@ -560,6 +561,10 @@ public static function readAccessorProvider(): array
[Dummy::class, 'foo', true, PropertyReadInfo::TYPE_PROPERTY, 'foo', PropertyReadInfo::VISIBILITY_PUBLIC, false],
[Php71Dummy::class, 'foo', true, PropertyReadInfo::TYPE_METHOD, 'getFoo', PropertyReadInfo::VISIBILITY_PUBLIC, false],
[Php71Dummy::class, 'buz', true, PropertyReadInfo::TYPE_METHOD, 'getBuz', PropertyReadInfo::VISIBILITY_PUBLIC, false],
[UnderscoreDummy::class, '_', true, PropertyReadInfo::TYPE_METHOD, 'get_', PropertyReadInfo::VISIBILITY_PUBLIC, false],
[UnderscoreDummy::class, '__', true, PropertyReadInfo::TYPE_METHOD, 'get__', PropertyReadInfo::VISIBILITY_PUBLIC, false],
[UnderscoreDummy::class, 'foo_bar', false, null, null, null, null],
[UnderscoreDummy::class, '_foo_', false, null, null, null, null],
];
}

Expand Down Expand Up @@ -792,4 +797,35 @@ public static function provideVirtualPropertiesMutator(): iterable
yield ['virtualSetHookOnly', PropertyReadInfo::VISIBILITY_PUBLIC, PropertyWriteInfo::VISIBILITY_PUBLIC];
yield ['virtualHook', PropertyReadInfo::VISIBILITY_PUBLIC, PropertyWriteInfo::VISIBILITY_PUBLIC];
}

/**
* @dataProvider camelizeProvider
*/
public function testCamelize(string $input, string $expected)
{
$reflection = new \ReflectionClass($this->extractor);
$method = $reflection->getMethod('camelize');

if (\PHP_VERSION_ID < 80500) {
$method->setAccessible(true);
}

$this->assertSame($expected, $method->invoke($this->extractor, $input));
}

public static function camelizeProvider(): iterable
{
yield 'single underscore' => ['_', '_'];
yield 'double underscore' => ['__', '__'];
yield 'triple underscore' => ['___', '___'];

yield 'snake case' => ['foo_bar', 'FooBar'];
yield 'double snake case' => ['foo__bar', 'FooBar'];
yield 'leading underscore' => ['_foo', 'Foo'];
yield 'trailing underscore' => ['foo_', 'Foo'];
yield 'leading and trailing' => ['_foo_bar_', 'FooBar'];
yield 'empty string' => ['', ''];
yield 'no underscore' => ['fooBar', 'FooBar'];
yield 'pascal case' => ['FooBar', 'FooBar'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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;

/**
* Fixture class for testing underscore-only properties.
*/
class UnderscoreDummy
{
private float $_;
private float $__;

public function get_(): float
{
return $this->_;
}

public function get__(): float
{
return $this->__;
}
}