Skip to content

Commit

Permalink
Merge pull request #1286 from kukulich/reflection-compatibility
Browse files Browse the repository at this point in the history
Fixed reflection compatibility when method/property/constant/case name is empty
  • Loading branch information
Ocramius authored Oct 14, 2022
2 parents adcb77c + 66c2712 commit 54bd4ad
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 52 deletions.
37 changes: 19 additions & 18 deletions src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use function array_combine;
use function array_map;
use function array_values;
use function assert;
use function constant;
use function func_num_args;
use function sprintf;
Expand Down Expand Up @@ -117,16 +116,16 @@ public function getConstructor(): CoreReflectionMethod|null

public function hasMethod(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

return $this->betterReflectionClass->hasMethod($name);
}

public function getMethod(string $name): ReflectionMethod
{
assert($name !== '');

$method = $this->betterReflectionClass->getMethod($name);
$method = $name !== '' ? $this->betterReflectionClass->getMethod($name) : null;

if ($method === null) {
throw new CoreReflectionException(sprintf('Method %s::%s() does not exist', $this->betterReflectionClass->getName(), $name));
Expand All @@ -152,16 +151,16 @@ public function getMethods(int|null $filter = null): array

public function hasProperty(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

return $this->betterReflectionClass->hasProperty($name);
}

public function getProperty(string $name): ReflectionProperty
{
assert($name !== '');

$betterReflectionProperty = $this->betterReflectionClass->getProperty($name);
$betterReflectionProperty = $name !== '' ? $this->betterReflectionClass->getProperty($name) : null;

if ($betterReflectionProperty === null) {
throw new CoreReflectionException(sprintf('Property %s::$%s does not exist', $this->betterReflectionClass->getName(), $name));
Expand All @@ -187,7 +186,9 @@ public function getProperties(int|null $filter = null): array

public function hasConstant(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

if ($this->betterReflectionClass instanceof BetterReflectionEnum && $this->betterReflectionClass->hasCase($name)) {
return true;
Expand All @@ -211,7 +212,9 @@ public function getConstants(int|null $filter = null): array

public function getConstant(string $name): mixed
{
assert($name !== '');
if ($name === '') {
return false;
}

if ($this->betterReflectionClass instanceof BetterReflectionEnum) {
$enumCase = $this->betterReflectionClass->getCase($name);
Expand Down Expand Up @@ -239,7 +242,9 @@ private function getConstantValue(BetterReflectionClassConstant|BetterReflection

public function getReflectionConstant(string $name): ReflectionClassConstant|false
{
assert($name !== '');
if ($name === '') {
return false;
}

if ($this->betterReflectionClass instanceof BetterReflectionEnum) {
$enumCase = $this->betterReflectionClass->getCase($name);
Expand Down Expand Up @@ -420,9 +425,7 @@ public function getStaticProperties(): array

public function getStaticPropertyValue(string $name, mixed $default = null): mixed
{
assert($name !== '');

$betterReflectionProperty = $this->betterReflectionClass->getProperty($name);
$betterReflectionProperty = $name !== '' ? $this->betterReflectionClass->getProperty($name) : null;

if ($betterReflectionProperty === null) {
if (func_num_args() === 2) {
Expand All @@ -443,9 +446,7 @@ public function getStaticPropertyValue(string $name, mixed $default = null): mix

public function setStaticPropertyValue(string $name, mixed $value): void
{
assert($name !== '');

$betterReflectionProperty = $this->betterReflectionClass->getProperty($name);
$betterReflectionProperty = $name !== '' ? $this->betterReflectionClass->getProperty($name) : null;

if ($betterReflectionProperty === null) {
throw new CoreReflectionException(sprintf('Class %s does not have a property named %s', $this->betterReflectionClass->getName(), $name));
Expand Down
37 changes: 21 additions & 16 deletions src/Reflection/Adapter/ReflectionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use function array_combine;
use function array_map;
use function array_values;
use function assert;
use function constant;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -115,16 +114,16 @@ public function getConstructor(): CoreReflectionMethod|null

public function hasMethod(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

return $this->betterReflectionEnum->hasMethod($name);
}

public function getMethod(string $name): ReflectionMethod
{
assert($name !== '');

$method = $this->betterReflectionEnum->getMethod($name);
$method = $name !== '' ? $this->betterReflectionEnum->getMethod($name) : null;

if ($method === null) {
throw new CoreReflectionException(sprintf('Method %s::%s() does not exist', $this->betterReflectionEnum->getName(), $name));
Expand All @@ -150,16 +149,16 @@ public function getMethods(int|null $filter = null): array

public function hasProperty(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

return $this->betterReflectionEnum->hasProperty($name);
}

public function getProperty(string $name): ReflectionProperty
{
assert($name !== '');

$betterReflectionProperty = $this->betterReflectionEnum->getProperty($name);
$betterReflectionProperty = $name !== '' ? $this->betterReflectionEnum->getProperty($name) : null;

if ($betterReflectionProperty === null) {
throw new CoreReflectionException(sprintf('Property %s::$%s does not exist', $this->betterReflectionEnum->getName(), $name));
Expand All @@ -185,7 +184,9 @@ public function getProperties(int|null $filter = null): array

public function hasConstant(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

return $this->betterReflectionEnum->hasCase($name) || $this->betterReflectionEnum->hasConstant($name);
}
Expand All @@ -205,7 +206,9 @@ public function getConstants(int|null $filter = null): array

public function getConstant(string $name): mixed
{
assert($name !== '');
if ($name === '') {
return false;
}

$enumCase = $this->betterReflectionEnum->getCase($name);
if ($enumCase !== null) {
Expand All @@ -231,7 +234,9 @@ private function getConstantValue(BetterReflectionClassConstant|BetterReflection

public function getReflectionConstant(string $name): ReflectionClassConstant|false
{
assert($name !== '');
if ($name === '') {
return false;
}

// @infection-ignore-all Coalesce: There's no difference
$betterReflectionConstantOrEnumCase = $this->betterReflectionEnum->getCase($name) ?? $this->betterReflectionEnum->getConstant($name);
Expand Down Expand Up @@ -493,16 +498,16 @@ public function isEnum(): bool

public function hasCase(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

return $this->betterReflectionEnum->hasCase($name);
}

public function getCase(string $name): ReflectionEnumUnitCase|ReflectionEnumBackedCase
{
assert($name !== '');

$case = $this->betterReflectionEnum->getCase($name);
$case = $name !== '' ? $this->betterReflectionEnum->getCase($name) : null;

if ($case === null) {
throw new CoreReflectionException(sprintf('Case %s::%s does not exist', $this->betterReflectionEnum->getName(), $name));
Expand Down
37 changes: 19 additions & 18 deletions src/Reflection/Adapter/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use function array_combine;
use function array_map;
use function array_values;
use function assert;
use function func_num_args;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -99,16 +98,16 @@ public function getConstructor(): ReflectionMethod|null

public function hasMethod(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

return $this->betterReflectionObject->hasMethod($this->getMethodRealName($name));
}

public function getMethod(string $name): ReflectionMethod
{
assert($name !== '');

$method = $this->betterReflectionObject->getMethod($this->getMethodRealName($name));
$method = $name !== '' ? $this->betterReflectionObject->getMethod($this->getMethodRealName($name)) : null;

if ($method === null) {
throw new CoreReflectionException(sprintf('Method %s::%s() does not exist', $this->betterReflectionObject->getName(), $name));
Expand Down Expand Up @@ -150,16 +149,16 @@ public function getMethods(int|null $filter = null): array

public function hasProperty(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

return $this->betterReflectionObject->hasProperty($name);
}

public function getProperty(string $name): ReflectionProperty
{
assert($name !== '');

$property = $this->betterReflectionObject->getProperty($name);
$property = $name !== '' ? $this->betterReflectionObject->getProperty($name) : null;

if ($property === null) {
throw new CoreReflectionException(sprintf('Property %s::$%s does not exist', $this->betterReflectionObject->getName(), $name));
Expand All @@ -185,7 +184,9 @@ public function getProperties(int|null $filter = null): array

public function hasConstant(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

return $this->betterReflectionObject->hasConstant($name);
}
Expand All @@ -205,7 +206,9 @@ public function getConstants(int|null $filter = null): array

public function getConstant(string $name): mixed
{
assert($name !== '');
if ($name === '') {
return false;
}

$betterReflectionConstant = $this->betterReflectionObject->getConstant($name);
if ($betterReflectionConstant === null) {
Expand All @@ -217,7 +220,9 @@ public function getConstant(string $name): mixed

public function getReflectionConstant(string $name): ReflectionClassConstant|false
{
assert($name !== '');
if ($name === '') {
return false;
}

$betterReflectionConstant = $this->betterReflectionObject->getConstant($name);

Expand Down Expand Up @@ -370,9 +375,7 @@ public function getStaticProperties(): array

public function getStaticPropertyValue(string $name, mixed $default = null): mixed
{
assert($name !== '');

$betterReflectionProperty = $this->betterReflectionObject->getProperty($name);
$betterReflectionProperty = $name !== '' ? $this->betterReflectionObject->getProperty($name) : null;

if ($betterReflectionProperty === null) {
if (func_num_args() === 2) {
Expand All @@ -393,9 +396,7 @@ public function getStaticPropertyValue(string $name, mixed $default = null): mix

public function setStaticPropertyValue(string $name, mixed $value): void
{
assert($name !== '');

$betterReflectionProperty = $this->betterReflectionObject->getProperty($name);
$betterReflectionProperty = $name !== '' ? $this->betterReflectionObject->getProperty($name) : null;

if ($betterReflectionProperty === null) {
throw new CoreReflectionException(sprintf('Class %s does not have a property named %s', $this->betterReflectionObject->getName(), $name));
Expand Down
Loading

0 comments on commit 54bd4ad

Please sign in to comment.