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 4 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
8 changes: 4 additions & 4 deletions src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ public function hasConstant($name)
/**
* {@inheritDoc}
*/
public function getConstants()
public function getConstants(?int $filter = null)
{
return $this->betterReflectionClass->getConstants();
return $this->betterReflectionClass->getConstants($filter);
}

/**
Expand All @@ -279,11 +279,11 @@ 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->betterReflectionClass->getReflectionConstants($filter)));
}

/**
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
4 changes: 2 additions & 2 deletions src/Reflection/Adapter/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ public function hasConstant($name)
/**
* {@inheritDoc}
*/
public function getConstants()
public function getConstants(?int $filter = null)
{
return $this->betterReflectionObject->getConstants();
return $this->betterReflectionObject->getConstants($filter);
}

/**
Expand Down
21 changes: 17 additions & 4 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,16 @@ public function getImmediateConstants(): array
/**
* Get an associative array of the defined constants in this class,
* with keys as constant names and values as constant values.

* @see ReflectionClass::getConstants() for the usage of filter
*
* @return array<string, scalar|array<scalar>|null>
*/
public function getConstants(): array
public function getConstants(?int $filter = null): array
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
{
return array_map(static function (ReflectionClassConstant $classConstant) {
return $classConstant->getValue();
}, $this->getReflectionConstants());
}, $this->getReflectionConstants($filter));
}

/**
Expand Down Expand Up @@ -584,9 +586,11 @@ static function (ReflectionClassConstant $constant): string {
* Get an associative array of the defined constants in this class,
* with keys as constant names and values as {@see ReflectionClassConstant} objects.
*
* @see ReflectionClass::getConstants() for the usage of filter
*
* @return array<string, ReflectionClassConstant> indexed by name
*/
public function getReflectionConstants(): array
public function getReflectionConstants(?int $filter = null): array
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
{
// Note: constants are not merged via their name as array index, since internal PHP constant
// sorting does not follow `\array_merge()` semantics
Expand Down Expand Up @@ -624,7 +628,16 @@ static function (ReflectionClass $interface): array {
$reflectionConstants[$constantName] = $constant;
}

return $reflectionConstants;
if ($filter === null) {
return $reflectionConstants;
}

return array_filter(
$reflectionConstants,
static function (ReflectionClassConstant $constant) use ($filter): bool {
return (bool) ($filter & $constant->getModifiers());
},
);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Reflection/ReflectionClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

class ReflectionClassConstant
{
public const IS_PUBLIC = 1;
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
public const IS_PROTECTED = 2;
public const IS_PRIVATE = 4;

private bool $valueWasCached = false;

/** @var scalar|array<scalar>|null const value */
Expand Down
8 changes: 4 additions & 4 deletions src/Reflection/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ public function getImmediateConstants(): array
/**
* {@inheritdoc}
*/
public function getConstants(): array
public function getConstants(?int $filter = null): array
{
return $this->reflectionClass->getConstants();
return $this->reflectionClass->getConstants($filter);
}

/**
Expand Down Expand Up @@ -224,9 +224,9 @@ public function getImmediateReflectionConstants(): array
/**
* {@inheritdoc}
*/
public function getReflectionConstants(): array
public function getReflectionConstants(?int $filter = null): array
{
return $this->reflectionClass->getReflectionConstants();
return $this->reflectionClass->getReflectionConstants($filter);
}

public function getConstructor(): ReflectionMethod
Expand Down
27 changes: 27 additions & 0 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,33 @@ public function testGetConstantsReturnsInheritedConstants(): void
self::assertSame($expectedConstants, $classInfo->getConstants());
}

public function getConstantsWithFilterDataProvider(): array
{
return [
[ReflectionClassConstant::IS_PUBLIC, 3],
[ReflectionClassConstant::IS_PROTECTED, 1],
[ReflectionClassConstant::IS_PRIVATE, 1],
[
ReflectionClassConstant::IS_PUBLIC |
ReflectionClassConstant::IS_PROTECTED |
ReflectionClassConstant::IS_PRIVATE,
5,
],
];
}

/**
* @dataProvider getConstantsWithFilterDataProvider
*/
public function testGetConstantsWithFilter(int $filter, int $count): void
{
$reflector = new ClassReflector($this->getComposerLocator());
$classInfo = $reflector->reflect(ExampleClass::class);

self::assertCount($count, $classInfo->getConstants($filter));
self::assertCount($count, $classInfo->getReflectionConstants($filter));
}

public function testGetImmediateConstants(): void
{
$classInfo = (new ClassReflector(new SingleFileSourceLocator(
Expand Down