From 601f824901b497a9bd4484e6ae24ab3be6293054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Fri, 14 Oct 2022 23:37:58 +0200 Subject: [PATCH 1/4] Fixed Reflection*::getMethod()/hasMethod() adapters compatibility with native reflection --- src/Reflection/Adapter/ReflectionClass.php | 8 +++---- src/Reflection/Adapter/ReflectionEnum.php | 8 +++---- src/Reflection/Adapter/ReflectionObject.php | 8 +++---- .../Adapter/ReflectionClassTest.php | 22 +++++++++++++++++++ .../Reflection/Adapter/ReflectionEnumTest.php | 22 +++++++++++++++++++ .../Adapter/ReflectionObjectTest.php | 22 +++++++++++++++++++ 6 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/Reflection/Adapter/ReflectionClass.php b/src/Reflection/Adapter/ReflectionClass.php index 49a8822b8..a36dbccf3 100644 --- a/src/Reflection/Adapter/ReflectionClass.php +++ b/src/Reflection/Adapter/ReflectionClass.php @@ -117,16 +117,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)); diff --git a/src/Reflection/Adapter/ReflectionEnum.php b/src/Reflection/Adapter/ReflectionEnum.php index eb880581b..7964d9b11 100644 --- a/src/Reflection/Adapter/ReflectionEnum.php +++ b/src/Reflection/Adapter/ReflectionEnum.php @@ -115,16 +115,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)); diff --git a/src/Reflection/Adapter/ReflectionObject.php b/src/Reflection/Adapter/ReflectionObject.php index a3d115d1b..8547db894 100644 --- a/src/Reflection/Adapter/ReflectionObject.php +++ b/src/Reflection/Adapter/ReflectionObject.php @@ -99,16 +99,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)); diff --git a/test/unit/Reflection/Adapter/ReflectionClassTest.php b/test/unit/Reflection/Adapter/ReflectionClassTest.php index 6170cbcb5..d59dc6879 100644 --- a/test/unit/Reflection/Adapter/ReflectionClassTest.php +++ b/test/unit/Reflection/Adapter/ReflectionClassTest.php @@ -212,6 +212,28 @@ public function testGetParentClassReturnsFalseWhenNoParent(): void self::assertFalse($reflectionClassAdapter->getParentClass()); } + public function testHasMethodReturnsFalseWhenMethodNameIsEmpty(): void + { + $betterReflectionClass = $this->createMock(BetterReflectionClass::class); + $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass); + + self::assertFalse($reflectionClassAdapter->hasMethod('')); + } + + public function testGetMethodThrowsExceptionWhenMethodNameIsEmpty(): void + { + $betterReflectionClass = $this->createMock(BetterReflectionClass::class); + $betterReflectionClass + ->method('getName') + ->willReturn('SomeClass'); + + $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass); + + $this->expectException(CoreReflectionException::class); + $this->expectExceptionMessage('Method SomeClass::() does not exist'); + $reflectionClassAdapter->getMethod(''); + } + public function testGetMethodThrowsExceptionWhenMethodDoesNotExist(): void { $betterReflectionClass = $this->createMock(BetterReflectionClass::class); diff --git a/test/unit/Reflection/Adapter/ReflectionEnumTest.php b/test/unit/Reflection/Adapter/ReflectionEnumTest.php index 3946aed21..d7333410c 100644 --- a/test/unit/Reflection/Adapter/ReflectionEnumTest.php +++ b/test/unit/Reflection/Adapter/ReflectionEnumTest.php @@ -859,6 +859,28 @@ public function testGetTraits(): void self::assertArrayHasKey($traitTwoClassName, $traits); } + public function testHasMethodReturnsFalseWhenMethodNameIsEmpty(): void + { + $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); + $reflectionEnumAdapter = new ReflectionEnumAdapter($betterReflectionEnum); + + self::assertFalse($reflectionEnumAdapter->hasMethod('')); + } + + public function testGetMethodThrowsExceptionWhenMethodNameIsEmpty(): void + { + $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); + $betterReflectionEnum + ->method('getName') + ->willReturn('SomeClass'); + + $reflectionEnumAdapter = new ReflectionEnumAdapter($betterReflectionEnum); + + $this->expectException(CoreReflectionException::class); + $this->expectExceptionMessage('Method SomeClass::() does not exist'); + $reflectionEnumAdapter->getMethod(''); + } + public function testGetMethodThrowsExceptionWhenMethodDoesNotExist(): void { $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); diff --git a/test/unit/Reflection/Adapter/ReflectionObjectTest.php b/test/unit/Reflection/Adapter/ReflectionObjectTest.php index 757f9af26..55ef99c25 100644 --- a/test/unit/Reflection/Adapter/ReflectionObjectTest.php +++ b/test/unit/Reflection/Adapter/ReflectionObjectTest.php @@ -975,6 +975,28 @@ public function testUnknownProperty(): void $reflectionObjectAdapter->foo; } + public function testHasMethodReturnsFalseWhenMethodNameIsEmpty(): void + { + $betterReflectionObject = $this->createMock(BetterReflectionObject::class); + $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject); + + self::assertFalse($reflectionObjectAdapter->hasMethod('')); + } + + public function testGetMethodThrowsExceptionWhenMethodNameIsEmpty(): void + { + $betterReflectionObject = $this->createMock(BetterReflectionObject::class); + $betterReflectionObject + ->method('getName') + ->willReturn('SomeClass'); + + $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject); + + $this->expectException(CoreReflectionException::class); + $this->expectExceptionMessage('Method SomeClass::() does not exist'); + $reflectionObjectAdapter->getMethod(''); + } + public function testGetMethodThrowsExceptionWhenMethodDoesNotExist(): void { $betterReflectionObject = $this->createMock(BetterReflectionObject::class); From ce7ffdf4a45de5cb6e0b82e5f377372476a373fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Fri, 14 Oct 2022 23:49:25 +0200 Subject: [PATCH 2/4] Fixed Reflection*::getProperty()/hasProperty()/getStaticPropertyValue()/setStaticPropertyValue() adapters compatibility with native reflection --- src/Reflection/Adapter/ReflectionClass.php | 16 +++--- src/Reflection/Adapter/ReflectionEnum.php | 8 +-- src/Reflection/Adapter/ReflectionObject.php | 16 +++--- .../Adapter/ReflectionClassTest.php | 50 +++++++++++++++++++ .../Reflection/Adapter/ReflectionEnumTest.php | 22 ++++++++ .../Adapter/ReflectionObjectTest.php | 50 +++++++++++++++++++ 6 files changed, 138 insertions(+), 24 deletions(-) diff --git a/src/Reflection/Adapter/ReflectionClass.php b/src/Reflection/Adapter/ReflectionClass.php index a36dbccf3..b5830114a 100644 --- a/src/Reflection/Adapter/ReflectionClass.php +++ b/src/Reflection/Adapter/ReflectionClass.php @@ -152,16 +152,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)); @@ -420,9 +420,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) { @@ -443,9 +441,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)); diff --git a/src/Reflection/Adapter/ReflectionEnum.php b/src/Reflection/Adapter/ReflectionEnum.php index 7964d9b11..25de7db26 100644 --- a/src/Reflection/Adapter/ReflectionEnum.php +++ b/src/Reflection/Adapter/ReflectionEnum.php @@ -150,16 +150,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)); diff --git a/src/Reflection/Adapter/ReflectionObject.php b/src/Reflection/Adapter/ReflectionObject.php index 8547db894..acf8d7ec0 100644 --- a/src/Reflection/Adapter/ReflectionObject.php +++ b/src/Reflection/Adapter/ReflectionObject.php @@ -150,16 +150,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)); @@ -370,9 +370,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) { @@ -393,9 +391,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)); diff --git a/test/unit/Reflection/Adapter/ReflectionClassTest.php b/test/unit/Reflection/Adapter/ReflectionClassTest.php index d59dc6879..76ac42626 100644 --- a/test/unit/Reflection/Adapter/ReflectionClassTest.php +++ b/test/unit/Reflection/Adapter/ReflectionClassTest.php @@ -518,6 +518,28 @@ public function testImplementsInterfaceIsCaseInsensitive(): void self::assertTrue($reflectionClassAdapter->implementsInterface('FoO')); } + public function testHasPropertyReturnFalseWhenPropertyNameIsEmpty(): void + { + $betterReflectionClass = $this->createMock(BetterReflectionClass::class); + $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass); + + self::assertFalse($reflectionClassAdapter->hasProperty('')); + } + + public function testGetPropertyThrowsExceptionWhenPropertyNameIsEmpty(): void + { + $betterReflectionClass = $this->createMock(BetterReflectionClass::class); + $betterReflectionClass + ->method('getName') + ->willReturn('Boo'); + + $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass); + + $this->expectException(CoreReflectionException::class); + $this->expectExceptionMessage('Property Boo::$ does not exist'); + $reflectionClassAdapter->getProperty(''); + } + public function testGetPropertyThrowsExceptionWhenPropertyDoesNotExist(): void { $betterReflectionClass = $this->createMock(BetterReflectionClass::class); @@ -596,6 +618,20 @@ public function testGetStaticPropertyValue(): void self::assertSame(123, $reflectionClassAdapter->getStaticPropertyValue('foo')); } + public function testGetStaticPropertyValueThrowsExceptionWhenPropertyNameIsEmpty(): void + { + $betterReflectionClass = $this->createMock(BetterReflectionClass::class); + $betterReflectionClass + ->method('getName') + ->willReturn('SomeClass'); + + $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass); + + self::expectException(CoreReflectionException::class); + self::expectExceptionMessage('Property SomeClass::$ does not exist'); + $reflectionClassAdapter->getStaticPropertyValue(''); + } + public function testSetStaticPropertyValue(): void { $betterReflectionProperty = $this->createMock(BetterReflectionProperty::class); @@ -621,6 +657,20 @@ public function testSetStaticPropertyValue(): void $reflectionClassAdapter->setStaticPropertyValue('foo', 123); } + public function testSetStaticPropertyValueThrowsExceptionWhenPropertyNameIsEmpty(): void + { + $betterReflectionClass = $this->createMock(BetterReflectionClass::class); + $betterReflectionClass + ->method('getName') + ->willReturn('SomeClass'); + + $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass); + + self::expectException(CoreReflectionException::class); + self::expectExceptionMessage('Class SomeClass does not have a property named '); + $reflectionClassAdapter->setStaticPropertyValue('', ''); + } + public function testGetStaticPropertyValueThrowsExceptionWhenPropertyDoesNotExist(): void { $betterReflectionClass = $this->createMock(BetterReflectionClass::class); diff --git a/test/unit/Reflection/Adapter/ReflectionEnumTest.php b/test/unit/Reflection/Adapter/ReflectionEnumTest.php index d7333410c..d7c0426b2 100644 --- a/test/unit/Reflection/Adapter/ReflectionEnumTest.php +++ b/test/unit/Reflection/Adapter/ReflectionEnumTest.php @@ -246,6 +246,28 @@ public function testGetConstructorReturnsNullWhenNoConstructorExists(): void self::assertNull($reflectionEnumAdapter->getConstructor()); } + public function testHasPropertyReturnFalseWhenPropertyNameIsEmpty(): void + { + $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); + $reflectionEnumAdapter = new ReflectionEnumAdapter($betterReflectionEnum); + + self::assertFalse($reflectionEnumAdapter->hasProperty('')); + } + + public function testGetPropertyThrowsExceptionWhenPropertyNameIsEmpty(): void + { + $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); + $betterReflectionEnum + ->method('getName') + ->willReturn('Boo'); + + $reflectionEnumAdapter = new ReflectionEnumAdapter($betterReflectionEnum); + + $this->expectException(CoreReflectionException::class); + $this->expectExceptionMessage('Property Boo::$ does not exist'); + $reflectionEnumAdapter->getProperty(''); + } + public function testGetPropertyThrowsExceptionWhenPropertyDoesNotExist(): void { $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); diff --git a/test/unit/Reflection/Adapter/ReflectionObjectTest.php b/test/unit/Reflection/Adapter/ReflectionObjectTest.php index 55ef99c25..f4846dedd 100644 --- a/test/unit/Reflection/Adapter/ReflectionObjectTest.php +++ b/test/unit/Reflection/Adapter/ReflectionObjectTest.php @@ -336,6 +336,28 @@ public function testImplementsInterfaceIsCaseInsensitive(): void self::assertTrue($reflectionObjectAdapter->implementsInterface('FoO')); } + public function testHasPropertyReturnFalseWhenPropertyNameIsEmpty(): void + { + $betterReflectionObject = $this->createMock(BetterReflectionObject::class); + $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject); + + self::assertFalse($reflectionObjectAdapter->hasProperty('')); + } + + public function testGetPropertyThrowsExceptionWhenPropertyNameIsEmpty(): void + { + $betterReflectionObject = $this->createMock(BetterReflectionObject::class); + $betterReflectionObject + ->method('getName') + ->willReturn('Boo'); + + $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject); + + $this->expectException(CoreReflectionException::class); + $this->expectExceptionMessage('Property Boo::$ does not exist'); + $reflectionObjectAdapter->getProperty(''); + } + public function testGetPropertyThrowsExceptionWhenPropertyDoesNotExist(): void { $betterReflectionObject = $this->createMock(BetterReflectionObject::class); @@ -378,6 +400,20 @@ public function testGetStaticPropertyValue(): void self::assertSame(123, $reflectionClassAdapter->getStaticPropertyValue('foo')); } + public function testGetStaticPropertyValueThrowsExceptionWhenPropertyNameIsEmpty(): void + { + $betterReflectionObject = $this->createMock(BetterReflectionObject::class); + $betterReflectionObject + ->method('getName') + ->willReturn('SomeClass'); + + $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject); + + self::expectException(CoreReflectionException::class); + self::expectExceptionMessage('Property SomeClass::$ does not exist'); + $reflectionObjectAdapter->getStaticPropertyValue(''); + } + public function testSetStaticPropertyValue(): void { $betterReflectionProperty = $this->createMock(BetterReflectionProperty::class); @@ -403,6 +439,20 @@ public function testSetStaticPropertyValue(): void $reflectionClassAdapter->setStaticPropertyValue('foo', 123); } + public function testSetStaticPropertyValueThrowsExceptionWhenPropertyNameIsEmpty(): void + { + $betterReflectionObject = $this->createMock(BetterReflectionObject::class); + $betterReflectionObject + ->method('getName') + ->willReturn('SomeClass'); + + $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject); + + self::expectException(CoreReflectionException::class); + self::expectExceptionMessage('Class SomeClass does not have a property named '); + $reflectionObjectAdapter->setStaticPropertyValue('', ''); + } + public function testGetStaticPropertyValueThrowsExceptionWhenPropertyDoesNotExist(): void { $betterReflectionObject = $this->createMock(BetterReflectionObject::class); From fdbc0d8d82ba2436e385b50a568f3d8eba2f746e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Fri, 14 Oct 2022 23:57:47 +0200 Subject: [PATCH 3/4] Fixed Reflection*::getConstant()/getReflectionConstant()/hasConstant() adapters compatibility with native reflection --- src/Reflection/Adapter/ReflectionClass.php | 13 ++++++---- src/Reflection/Adapter/ReflectionEnum.php | 12 +++++++--- src/Reflection/Adapter/ReflectionObject.php | 13 ++++++---- .../Adapter/ReflectionClassTest.php | 24 +++++++++++++++++++ .../Reflection/Adapter/ReflectionEnumTest.php | 24 +++++++++++++++++++ .../Adapter/ReflectionObjectTest.php | 24 +++++++++++++++++++ 6 files changed, 99 insertions(+), 11 deletions(-) diff --git a/src/Reflection/Adapter/ReflectionClass.php b/src/Reflection/Adapter/ReflectionClass.php index b5830114a..db668af0f 100644 --- a/src/Reflection/Adapter/ReflectionClass.php +++ b/src/Reflection/Adapter/ReflectionClass.php @@ -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; @@ -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; @@ -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); @@ -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); diff --git a/src/Reflection/Adapter/ReflectionEnum.php b/src/Reflection/Adapter/ReflectionEnum.php index 25de7db26..5c53c1636 100644 --- a/src/Reflection/Adapter/ReflectionEnum.php +++ b/src/Reflection/Adapter/ReflectionEnum.php @@ -185,7 +185,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); } @@ -205,7 +207,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) { @@ -231,7 +235,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); diff --git a/src/Reflection/Adapter/ReflectionObject.php b/src/Reflection/Adapter/ReflectionObject.php index acf8d7ec0..2eb8d74ff 100644 --- a/src/Reflection/Adapter/ReflectionObject.php +++ b/src/Reflection/Adapter/ReflectionObject.php @@ -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; @@ -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); } @@ -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) { @@ -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); diff --git a/test/unit/Reflection/Adapter/ReflectionClassTest.php b/test/unit/Reflection/Adapter/ReflectionClassTest.php index 76ac42626..cc78b6628 100644 --- a/test/unit/Reflection/Adapter/ReflectionClassTest.php +++ b/test/unit/Reflection/Adapter/ReflectionClassTest.php @@ -808,6 +808,14 @@ public function testGetConstructorReturnsNullWhenNoConstructorExists(): void self::assertNull($reflectionClassAdapter->getConstructor()); } + public function testHasConstantReturnsFalseWhenConstantNameIsEmpty(): void + { + $betterReflectionClass = $this->createMock(BetterReflectionClass::class); + $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass); + + self::assertFalse($reflectionClassAdapter->hasConstant('')); + } + public function testGetConstant(): void { $betterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class); @@ -826,6 +834,14 @@ public function testGetConstant(): void self::assertSame(123, $reflectionClassAdapter->getConstant('FOO')); } + public function testGetConstantReturnsFalseWhenConstantNameIsEmpty(): void + { + $betterReflectionClass = $this->createMock(BetterReflectionClass::class); + $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass); + + self::assertFalse($reflectionClassAdapter->getConstant('')); + } + public function testGetConstantReturnsFalseWhenConstantDoesNotExist(): void { $betterReflectionClass = $this->createMock(BetterReflectionClass::class); @@ -865,6 +881,14 @@ public function testGetReflectionConstantReturnsFalseWhenConstantDoesNotExist(): self::assertFalse($reflectionClassAdapter->getReflectionConstant('FOO')); } + public function testGetReflectionConstantReturnsFalseWhenConstantNameIsEmpty(): void + { + $betterReflectionClass = $this->createMock(BetterReflectionClass::class); + $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass); + + self::assertFalse($reflectionClassAdapter->getReflectionConstant('')); + } + public function testPropertyName(): void { $betterReflectionClass = $this->createMock(BetterReflectionClass::class); diff --git a/test/unit/Reflection/Adapter/ReflectionEnumTest.php b/test/unit/Reflection/Adapter/ReflectionEnumTest.php index d7c0426b2..856a8e02c 100644 --- a/test/unit/Reflection/Adapter/ReflectionEnumTest.php +++ b/test/unit/Reflection/Adapter/ReflectionEnumTest.php @@ -719,6 +719,14 @@ public function testHasConstantWithEnumCase(): void self::assertTrue($reflectionClassAdapter->hasConstant('ENUM_CASE')); } + public function testHasConstantReturnsFalseWhenConstantNameIsEmpty(): void + { + $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); + $reflectionEnumAdapter = new ReflectionEnumAdapter($betterReflectionEnum); + + self::assertFalse($reflectionEnumAdapter->hasConstant('')); + } + public function testGetConstant(): void { $betterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class); @@ -737,6 +745,14 @@ public function testGetConstant(): void self::assertSame(123, $reflectionClassAdapter->getConstant('FOO')); } + public function testGetConstantReturnsFalseWhenConstantNameIsEmpty(): void + { + $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); + $reflectionClassAdapter = new ReflectionEnumAdapter($betterReflectionEnum); + + self::assertFalse($reflectionClassAdapter->getConstant('')); + } + public function testGetConstantReturnsFalseWhenConstantDoesNotExist(): void { $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); @@ -783,6 +799,14 @@ public function testGetConstantWithEnumCase(): void self::assertInstanceOf(AutoloadableEnum::class, $reflectionClassAdapter->getConstant('ENUM_CASE')); } + public function testGetReflectionConstantReturnsFalseWhenConstantNameIsEmpty(): void + { + $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); + $reflectionClassAdapter = new ReflectionEnumAdapter($betterReflectionEnum); + + self::assertFalse($reflectionClassAdapter->getReflectionConstant('')); + } + public function testGetReflectionConstantWithEnumCase(): void { $betterReflectionEnumCase = $this->createMock(BetterReflectionEnumCase::class); diff --git a/test/unit/Reflection/Adapter/ReflectionObjectTest.php b/test/unit/Reflection/Adapter/ReflectionObjectTest.php index f4846dedd..fd756328a 100644 --- a/test/unit/Reflection/Adapter/ReflectionObjectTest.php +++ b/test/unit/Reflection/Adapter/ReflectionObjectTest.php @@ -653,6 +653,14 @@ public function testGetConstantsWithFilter(): void self::assertEquals([$protectedBetterReflectionClassConstant->getName() => $protectedBetterReflectionClassConstant->getValue()], $protectedConstants); } + public function testHasConstantReturnsFalseWhenConstantNameIsEmpty(): void + { + $betterReflectionObject = $this->createMock(BetterReflectionObject::class); + $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject); + + self::assertFalse($reflectionObjectAdapter->hasConstant('')); + } + public function testGetConstant(): void { $betterReflectionClassConstant = $this->createMock(BetterReflectionClassConstant::class); @@ -671,6 +679,14 @@ public function testGetConstant(): void self::assertSame(123, $reflectionObjectAdapter->getConstant('FOO')); } + public function testGetConstantReturnsFalseWhenConstantNameIsEmpty(): void + { + $betterReflectionObject = $this->createMock(BetterReflectionObject::class); + $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject); + + self::assertFalse($reflectionObjectAdapter->getConstant('')); + } + public function testGetConstantReturnsFalseWhenConstantDoesNotExist(): void { $betterReflectionObject = $this->createMock(BetterReflectionObject::class); @@ -699,6 +715,14 @@ public function testGetReflectionConstant(): void self::assertInstanceOf(ReflectionClassConstantAdapter::class, $reflectionObjectAdapter->getReflectionConstant('FOO')); } + public function testGetReflectionConstantReturnsFalseWhenConstantNameIsEmpty(): void + { + $betterReflectionObject = $this->createMock(BetterReflectionObject::class); + $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject); + + self::assertFalse($reflectionObjectAdapter->getReflectionConstant('')); + } + public function testGetReflectionConstantReturnsFalseWhenConstantDoesNotExist(): void { $betterReflectionObject = $this->createMock(BetterReflectionObject::class); From 66c271269a3d01a17662ca7e7383adb385a2e222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 15 Oct 2022 00:03:02 +0200 Subject: [PATCH 4/4] Fixed ReflectionEnum::getCase()/hasCase() adapters compatibility with native reflection --- src/Reflection/Adapter/ReflectionEnum.php | 9 ++++---- .../Reflection/Adapter/ReflectionEnumTest.php | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Reflection/Adapter/ReflectionEnum.php b/src/Reflection/Adapter/ReflectionEnum.php index 5c53c1636..07d35d259 100644 --- a/src/Reflection/Adapter/ReflectionEnum.php +++ b/src/Reflection/Adapter/ReflectionEnum.php @@ -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; @@ -499,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)); diff --git a/test/unit/Reflection/Adapter/ReflectionEnumTest.php b/test/unit/Reflection/Adapter/ReflectionEnumTest.php index 856a8e02c..78da3aee2 100644 --- a/test/unit/Reflection/Adapter/ReflectionEnumTest.php +++ b/test/unit/Reflection/Adapter/ReflectionEnumTest.php @@ -594,6 +594,28 @@ public function testGetAttributesThrowsExceptionForInvalidFlags(): void $reflectionEnumAdapter->getAttributes(null, 123); } + public function testHasCaseReturnsFalseWhenCaseNameIsEmpty(): void + { + $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); + $reflectionEnumAdapter = new ReflectionEnumAdapter($betterReflectionEnum); + + self::assertFalse($reflectionEnumAdapter->hasCase('')); + } + + public function testGetCaseThrowsExceptionWhenCaseNameIsEmpty(): void + { + $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class); + $betterReflectionEnum + ->method('getName') + ->willReturn('SomeEnum'); + + $reflectionEnumAdapter = new ReflectionEnumAdapter($betterReflectionEnum); + + self::expectException(CoreReflectionException::class); + self::expectExceptionMessage('Case SomeEnum:: does not exist'); + $reflectionEnumAdapter->getCase(''); + } + public function testGetCaseWhenCaseDoesNotExist(): void { $betterReflectionEnum = $this->createMock(BetterReflectionEnum::class);