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

Adapter method ReflectionClass#getProperty() throw an exception when a property doesn't exist #391

Merged
merged 2 commits into from
Feb 3, 2018
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
9 changes: 9 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ This document serves as a reference to upgrade your current
BetterReflection installation if improvements, deprecations
or backwards compatibility (BC) breakages occur.

## 3.0.0

### BC breaks

* Method `Roave\BetterReflection\Reflection\Adapter\ReflectionClass#getProperty()` throws exception
when property does not exist to be compatible with core reflection.
* Method `Roave\BetterReflection\Reflection\Adapter\ReflectionObject#getProperty()` throws exception
when property does not exist to be compatible with core reflection.

## 2.0.0

### Namespace change
Expand Down
14 changes: 9 additions & 5 deletions src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function getProperty($name)
$betterReflectionProperty = $this->betterReflectionClass->getProperty($name);

if ($betterReflectionProperty === null) {
return null;
throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
}

return new ReflectionProperty($betterReflectionProperty);
Expand Down Expand Up @@ -422,16 +422,18 @@ public function getStaticProperties()
*/
public function getStaticPropertyValue($name, $default = null)
{
$property = $this->getProperty($name);
$betterReflectionProperty = $this->betterReflectionClass->getProperty($name);

if ($property === null) {
if ($betterReflectionProperty === null) {
if (func_num_args() === 2) {
return $default;
}

throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
}

$property = new ReflectionProperty($betterReflectionProperty);

if (! $property->isAccessible()) {
throw new CoreReflectionException(sprintf('Property "%s" is not accessible', $name));
}
Expand All @@ -448,12 +450,14 @@ public function getStaticPropertyValue($name, $default = null)
*/
public function setStaticPropertyValue($name, $value)
{
$property = $this->getProperty($name);
$betterReflectionProperty = $this->betterReflectionClass->getProperty($name);

if ($property === null) {
if ($betterReflectionProperty === null) {
throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
}

$property = new ReflectionProperty($betterReflectionProperty);

if (! $property->isAccessible()) {
throw new CoreReflectionException(sprintf('Property "%s" is not accessible', $name));
}
Expand Down
14 changes: 9 additions & 5 deletions src/Reflection/Adapter/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function getProperty($name)
$property = $this->betterReflectionObject->getProperty($name);

if ($property === null) {
return null;
throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
}

return new ReflectionProperty($property);
Expand Down Expand Up @@ -392,16 +392,18 @@ public function getStaticProperties()
*/
public function getStaticPropertyValue($name, $default = null)
{
$property = $this->getProperty($name);
$betterReflectionProperty = $this->betterReflectionObject->getProperty($name);

if ($property === null) {
if ($betterReflectionProperty === null) {
if (func_num_args() === 2) {
return $default;
}

throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
}

$property = new ReflectionProperty($betterReflectionProperty);

if (! $property->isAccessible()) {
throw new CoreReflectionException(sprintf('Property "%s" is not accessible', $name));
}
Expand All @@ -418,12 +420,14 @@ public function getStaticPropertyValue($name, $default = null)
*/
public function setStaticPropertyValue($name, $value)
{
$property = $this->getProperty($name);
$betterReflectionProperty = $this->betterReflectionObject->getProperty($name);

if ($property === null) {
if ($betterReflectionProperty === null) {
throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
}

$property = new ReflectionProperty($betterReflectionProperty);

if (! $property->isAccessible()) {
throw new CoreReflectionException(sprintf('Property "%s" is not accessible', $name));
}
Expand Down
20 changes: 17 additions & 3 deletions test/unit/Reflection/Adapter/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ public function testImplementsInterfaceIsCaseInsensitive() : void
self::assertTrue($reflectionClassAdapter->implementsInterface('foo'));
}

public function testGetPropertyThrowsExceptionWhenPropertyDoesNotExist() : void
{
$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
$betterReflectionClass
->method('getProperty')
->with('foo')
->willReturn(null);

$reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);

$this->expectException(CoreReflectionException::class);
$reflectionClassAdapter->getProperty('foo');
}

public function testGetStaticPropertyValue() : void
{
$betterReflectionProperty = $this->createMock(BetterReflectionProperty::class);
Expand Down Expand Up @@ -359,7 +373,7 @@ public function testSetStaticPropertyValueThrowsExceptionWhenPropertyNotAccessib
$reflectionClassAdapter->setStaticPropertyValue('foo', null);
}

public function testGetStaticPropertyValueThrowsExceptionWhenPropertyPropertyDoesNotExist() : void
public function testGetStaticPropertyValueThrowsExceptionWhenPropertyDoesNotExist() : void
{
$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
$betterReflectionClass
Expand All @@ -373,7 +387,7 @@ public function testGetStaticPropertyValueThrowsExceptionWhenPropertyPropertyDoe
$reflectionClassAdapter->getStaticPropertyValue('foo');
}

public function testGetStaticPropertyValueReturnsDefaultValueWhenPropertyPropertyDoesNotExist() : void
public function testGetStaticPropertyValueReturnsDefaultValueWhenPropertyDoesNotExist() : void
{
$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
$betterReflectionClass
Expand All @@ -386,7 +400,7 @@ public function testGetStaticPropertyValueReturnsDefaultValueWhenPropertyPropert
self::assertSame('default', $reflectionClassAdapter->getStaticPropertyValue('foo', 'default'));
}

public function testSetStaticPropertyValueThrowsExceptionWhenPropertyPropertyDoesNotExist() : void
public function testSetStaticPropertyValueThrowsExceptionWhenPropertyDoesNotExist() : void
{
$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
$betterReflectionClass
Expand Down
20 changes: 17 additions & 3 deletions test/unit/Reflection/Adapter/ReflectionObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ public function testImplementsInterfaceIsCaseInsensitive() : void
self::assertTrue($reflectionObjectAdapter->implementsInterface('foo'));
}

public function testGetPropertyThrowsExceptionWhenPropertyDoesNotExist() : void
{
$betterReflectionObject = $this->createMock(BetterReflectionObject::class);
$betterReflectionObject
->method('getProperty')
->with('foo')
->willReturn(null);

$reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject);

$this->expectException(CoreReflectionException::class);
$reflectionObjectAdapter->getProperty('foo');
}

public function testGetStaticPropertyValue() : void
{
$betterReflectionProperty = $this->createMock(BetterReflectionProperty::class);
Expand Down Expand Up @@ -332,7 +346,7 @@ public function testSetStaticPropertyValueThrowsExceptionWhenPropertyNotAccessib
$reflectionObjectAdapter->setStaticPropertyValue('foo', null);
}

public function testGetStaticPropertyValueThrowsExceptionWhenPropertyPropertyDoesNotExist() : void
public function testGetStaticPropertyValueThrowsExceptionWhenPropertyDoesNotExist() : void
{
$betterReflectionObject = $this->createMock(BetterReflectionObject::class);
$betterReflectionObject
Expand All @@ -345,7 +359,7 @@ public function testGetStaticPropertyValueThrowsExceptionWhenPropertyPropertyDoe
$reflectionObjectAdapter->getStaticPropertyValue('foo');
}

public function testGetStaticPropertyValueReturnsDefaultValueWhenPropertyPropertyDoesNotExist() : void
public function testGetStaticPropertyValueReturnsDefaultValueWhenPropertyDoesNotExist() : void
{
$betterReflectionObject = $this->createMock(BetterReflectionObject::class);
$betterReflectionObject
Expand All @@ -357,7 +371,7 @@ public function testGetStaticPropertyValueReturnsDefaultValueWhenPropertyPropert
self::assertSame('default', $reflectionObjectAdapter->getStaticPropertyValue('foo', 'default'));
}

public function testSetStaticPropertyValueThrowsExceptionWhenPropertyPropertyDoesNotExist() : void
public function testSetStaticPropertyValueThrowsExceptionWhenPropertyDoesNotExist() : void
{
$betterReflectionObject = $this->createMock(BetterReflectionObject::class);
$betterReflectionObject
Expand Down