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

PHP8 - Adapter Signatures Compatibility #712

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
30 changes: 26 additions & 4 deletions src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Roave\BetterReflection\Util\FileHelper;

use function array_combine;
use function array_filter;
use function array_map;
use function array_values;
use function assert;
Expand Down Expand Up @@ -250,9 +251,11 @@ public function hasConstant($name)
/**
* {@inheritDoc}
*/
public function getConstants()
public function getConstants(?int $filter = null)
{
return $this->betterReflectionClass->getConstants();
return array_map(static function (BetterReflectionClassConstant $betterConstant) {
return $betterConstant->getValue();
}, $this->filterBetterReflectionClassConstants($filter));
}

/**
Expand All @@ -279,11 +282,30 @@ public function getReflectionConstant($name)
/**
* {@inheritdoc}
*/
public function getReflectionConstants()
public function getReflectionConstants(?int $filter = null)
{
return array_values(array_map(static function (BetterReflectionClassConstant $betterConstant): ReflectionClassConstant {
return new ReflectionClassConstant($betterConstant);
}, $this->betterReflectionClass->getReflectionConstants()));
}, $this->filterBetterReflectionClassConstants($filter)));
}

/**
* @return array<string, BetterReflectionClassConstant>
*/
private function filterBetterReflectionClassConstants(?int $filter): array
{
$reflectionConstants = $this->betterReflectionClass->getReflectionConstants();

if ($filter !== null) {
$reflectionConstants = array_filter(
$this->betterReflectionClass->getReflectionConstants(),
static function (BetterReflectionClassConstant $betterConstant) use ($filter): bool {
return (bool) ($betterConstant->getModifiers() & $filter);
},
);
}

return $reflectionConstants;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function isDisabled()
/**
* {@inheritDoc}
*/
public function invoke($args = null)
public function invoke($arg = null, ...$args)
{
try {
return $this->betterReflectionFunction->invoke(...func_get_args());
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public function getModifiers()
/**
* {@inheritDoc}
*/
public function invoke($object = null, $args = null)
public function invoke($object = null, $arg = null, ...$args)
{
if (! $this->isAccessible()) {
throw new CoreReflectionException('Method not accessible');
Expand Down
22 changes: 20 additions & 2 deletions src/Reflection/Adapter/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use ReflectionException as CoreReflectionException;
use ReflectionObject as CoreReflectionObject;
use Roave\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionClassConstant as BetterReflectionClassConstant;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionObject as BetterReflectionObject;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use Roave\BetterReflection\Util\FileHelper;

use function array_combine;
use function array_filter;
use function array_map;
use function array_values;
use function assert;
Expand Down Expand Up @@ -226,9 +228,25 @@ public function hasConstant($name)
/**
* {@inheritDoc}
*/
public function getConstants()
public function getConstants(?int $filter = null)
{
return $this->betterReflectionObject->getConstants();
$reflectionConstants = $this->betterReflectionObject->getReflectionConstants();

if ($filter !== null) {
$reflectionConstants = array_filter(
$reflectionConstants,
static function (BetterReflectionClassConstant $betterConstant) use ($filter): bool {
return (bool) ($betterConstant->getModifiers() & $filter);
},
);
}

return array_map(
static function (BetterReflectionClassConstant $betterConstant) {
return $betterConstant->getValue();
},
$reflectionConstants,
);
}

/**
Expand Down
143 changes: 142 additions & 1 deletion test/unit/Reflection/Adapter/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ReflectionProperty as CoreReflectionProperty;
use Roave\BetterReflection\Reflection\Adapter\Exception\NotImplemented;
use Roave\BetterReflection\Reflection\Adapter\ReflectionClass as ReflectionClassAdapter;
use Roave\BetterReflection\Reflection\Adapter\ReflectionClassConstant as ReflectionClassConstantAdapter;
use Roave\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionClassConstant as BetterReflectionClassConstant;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
Expand Down Expand Up @@ -78,7 +79,6 @@ public function methodExpectationProvider(): array
['getProperties', null, [$mockProperty], []],
['hasConstant', null, true, ['foo']],
['getConstant', null, 'a', ['foo']],
['getConstants', null, ['a', 'b'], []],
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
['getReflectionConstant', null, $mockConstant, ['foo']],
['getReflectionConstants', null, [$mockConstant], []],
['getInterfaces', null, [$mockClassLike], []],
Expand Down Expand Up @@ -228,6 +228,147 @@ public function testGetMethodsFilter(): void
self::assertSame($privateBetterReflectionMethod->getName(), $privateMethods[0]->getName());
}

public function testGetReflectionConstantsWithFilter(): void
{
$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
$publicBetterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class);
$privateBetterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class);
$protectedBetterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class);

$publicBetterReflectionClassConstant
->method('getModifiers')
->willReturn(CoreReflectionProperty::IS_PUBLIC);

$publicBetterReflectionClassConstant
->method('getName')
->willReturn('PUBLIC_CONSTANT');

$publicBetterReflectionClassConstant
->method('getValue')
->willReturn('public constant');

$privateBetterReflectionClassConstant
->method('getModifiers')
->willReturn(CoreReflectionProperty::IS_PRIVATE);

$privateBetterReflectionClassConstant
->method('getName')
->willReturn('PRIVATE_CONSTANT');

$privateBetterReflectionClassConstant
->method('getValue')
->willReturn('private constant');

$protectedBetterReflectionClassConstant
->method('getModifiers')
->willReturn(CoreReflectionProperty::IS_PROTECTED);

$protectedBetterReflectionClassConstant
->method('getName')
->willReturn('PROTECTED_CONSTANT');

$protectedBetterReflectionClassConstant
->method('getValue')
->willReturn('protected constant');

$betterReflectionClass
->method('getReflectionConstants')
->willReturn([
$publicBetterReflectionClassConstant->getName() => $publicBetterReflectionClassConstant,
$privateBetterReflectionClassConstant->getName() => $privateBetterReflectionClassConstant,
$protectedBetterReflectionClassConstant->getName() => $protectedBetterReflectionClassConstant,
]);

$reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);

$allConstants = $reflectionClassAdapter->getReflectionConstants();
$publicConstants = $reflectionClassAdapter->getReflectionConstants(CoreReflectionProperty::IS_PUBLIC);
$privateConstants = $reflectionClassAdapter->getReflectionConstants(CoreReflectionProperty::IS_PRIVATE);
$protectedConstants = $reflectionClassAdapter->getReflectionConstants(CoreReflectionProperty::IS_PROTECTED);

self::assertCount(3, $allConstants);
self::assertContainsOnlyInstancesOf(ReflectionClassConstantAdapter::class, $allConstants);

self::assertCount(1, $publicConstants);
self::assertSame($publicBetterReflectionClassConstant->getName(), $publicConstants[0]->getName());

self::assertCount(1, $privateConstants);
self::assertSame($privateBetterReflectionClassConstant->getName(), $privateConstants[0]->getName());

self::assertCount(1, $protectedConstants);
self::assertSame($protectedBetterReflectionClassConstant->getName(), $protectedConstants[0]->getName());
}

public function testGetConstantsWithFilter(): void
{
$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
$publicBetterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class);
$privateBetterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class);
$protectedBetterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class);

$publicBetterReflectionClassConstant
->method('getModifiers')
->willReturn(CoreReflectionProperty::IS_PUBLIC);

$publicBetterReflectionClassConstant
->method('getName')
->willReturn('PUBLIC_CONSTANT');

$publicBetterReflectionClassConstant
->method('getValue')
->willReturn('public constant');

$privateBetterReflectionClassConstant
->method('getModifiers')
->willReturn(CoreReflectionProperty::IS_PRIVATE);

$privateBetterReflectionClassConstant
->method('getName')
->willReturn('PRIVATE_CONSTANT');

$privateBetterReflectionClassConstant
->method('getValue')
->willReturn('private constant');

$protectedBetterReflectionClassConstant
->method('getModifiers')
->willReturn(CoreReflectionProperty::IS_PROTECTED);

$protectedBetterReflectionClassConstant
->method('getName')
->willReturn('PROTECTED_CONSTANT');

$protectedBetterReflectionClassConstant
->method('getValue')
->willReturn('protected constant');

$betterReflectionClass
->method('getReflectionConstants')
->willReturn([
$publicBetterReflectionClassConstant->getName() => $publicBetterReflectionClassConstant,
$privateBetterReflectionClassConstant->getName() => $privateBetterReflectionClassConstant,
$protectedBetterReflectionClassConstant->getName() => $protectedBetterReflectionClassConstant,
]);

$reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);

$allConstants = $reflectionClassAdapter->getConstants();
$publicConstants = $reflectionClassAdapter->getConstants(CoreReflectionProperty::IS_PUBLIC);
$privateConstants = $reflectionClassAdapter->getConstants(CoreReflectionProperty::IS_PRIVATE);
$protectedConstants = $reflectionClassAdapter->getConstants(CoreReflectionProperty::IS_PROTECTED);

self::assertCount(3, $allConstants);

self::assertCount(1, $publicConstants);
self::assertEquals([$publicBetterReflectionClassConstant->getName() => $publicBetterReflectionClassConstant->getValue()], $publicConstants);

self::assertCount(1, $privateConstants);
self::assertEquals([$privateBetterReflectionClassConstant->getName() => $privateBetterReflectionClassConstant->getValue()], $privateConstants);

self::assertCount(1, $protectedConstants);
self::assertEquals([$protectedBetterReflectionClassConstant->getName() => $protectedBetterReflectionClassConstant->getValue()], $protectedConstants);
}

public function testIsSubclassOfIsCaseInsensitive(): void
{
$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
Expand Down
73 changes: 72 additions & 1 deletion test/unit/Reflection/Adapter/ReflectionObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
use ReflectionClass as CoreReflectionClass;
use ReflectionException as CoreReflectionException;
use ReflectionObject as CoreReflectionObject;
use ReflectionProperty as CoreReflectionProperty;
use Roave\BetterReflection\Reflection\Adapter\Exception\NotImplemented;
use Roave\BetterReflection\Reflection\Adapter\ReflectionObject as ReflectionObjectAdapter;
use Roave\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionClassConstant as BetterReflectionClassConstant;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionObject as BetterReflectionObject;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
Expand Down Expand Up @@ -73,7 +75,6 @@ public function methodExpectationProvider(): array
['getProperties', null, [$mockProperty], []],
['hasConstant', null, true, ['foo']],
['getConstant', null, 'a', ['foo']],
['getConstants', null, ['a', 'b'], []],
['getInterfaces', null, [$mockClassLike], []],
['getInterfaceNames', null, ['a', 'b'], []],
['isInterface', null, true, []],
Expand Down Expand Up @@ -426,4 +427,74 @@ public function testIsInstanceReturnsNullWithNonObjectParameter(): void

self::assertNull($betterReflectionObject->isInstance('string'));
}

public function testGetConstantsWithFilter(): void
{
$betterReflectionObject = $this->createMock(BetterReflectionObject::class);
$publicBetterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class);
$privateBetterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class);
$protectedBetterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class);

$publicBetterReflectionClassConstant
->method('getModifiers')
->willReturn(CoreReflectionProperty::IS_PUBLIC);

$publicBetterReflectionClassConstant
->method('getName')
->willReturn('PUBLIC_CONSTANT');

$publicBetterReflectionClassConstant
->method('getValue')
->willReturn('public constant');

$privateBetterReflectionClassConstant
->method('getModifiers')
->willReturn(CoreReflectionProperty::IS_PRIVATE);

$privateBetterReflectionClassConstant
->method('getName')
->willReturn('PRIVATE_CONSTANT');

$privateBetterReflectionClassConstant
->method('getValue')
->willReturn('private constant');

$protectedBetterReflectionClassConstant
->method('getModifiers')
->willReturn(CoreReflectionProperty::IS_PROTECTED);

$protectedBetterReflectionClassConstant
->method('getName')
->willReturn('PROTECTED_CONSTANT');

$protectedBetterReflectionClassConstant
->method('getValue')
->willReturn('protected constant');

$betterReflectionObject
->method('getReflectionConstants')
->willReturn([
$publicBetterReflectionClassConstant->getName() => $publicBetterReflectionClassConstant,
$privateBetterReflectionClassConstant->getName() => $privateBetterReflectionClassConstant,
$protectedBetterReflectionClassConstant->getName() => $protectedBetterReflectionClassConstant,
]);

$reflectionObject = new ReflectionObjectAdapter($betterReflectionObject);

$allConstants = $reflectionObject->getConstants();
$publicConstants = $reflectionObject->getConstants(CoreReflectionProperty::IS_PUBLIC);
$privateConstants = $reflectionObject->getConstants(CoreReflectionProperty::IS_PRIVATE);
$protectedConstants = $reflectionObject->getConstants(CoreReflectionProperty::IS_PROTECTED);

self::assertCount(3, $allConstants);

self::assertCount(1, $publicConstants);
self::assertEquals([$publicBetterReflectionClassConstant->getName() => $publicBetterReflectionClassConstant->getValue()], $publicConstants);

self::assertCount(1, $privateConstants);
self::assertEquals([$privateBetterReflectionClassConstant->getName() => $privateBetterReflectionClassConstant->getValue()], $privateConstants);

self::assertCount(1, $protectedConstants);
self::assertEquals([$protectedBetterReflectionClassConstant->getName() => $protectedBetterReflectionClassConstant->getValue()], $protectedConstants);
}
}