Skip to content

Commit

Permalink
Merge pull request #439 from lookyman/get-constructor-adapter-incompa…
Browse files Browse the repository at this point in the history
…tibility

Adapter's ReflectionClass::getConstructor() returns null when no constructor exists
  • Loading branch information
Ocramius authored Jul 31, 2018
2 parents ca0298b + 69c9124 commit c022c6c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Roave\BetterReflection\Reflection\Adapter;

use OutOfBoundsException;
use ReflectionClass as CoreReflectionClass;
use ReflectionException as CoreReflectionException;
use Roave\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass;
Expand Down Expand Up @@ -127,7 +128,11 @@ public function getDocComment()
*/
public function getConstructor()
{
return new ReflectionMethod($this->betterReflectionClass->getConstructor());
try {
return new ReflectionMethod($this->betterReflectionClass->getConstructor());
} catch (OutOfBoundsException $e) {
return null;
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions test/unit/Reflection/Adapter/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Roave\BetterReflectionTest\Reflection\Adapter;

use OutOfBoundsException;
use PHPUnit\Framework\TestCase;
use ReflectionClass as CoreReflectionClass;
use ReflectionException as CoreReflectionException;
Expand Down Expand Up @@ -425,4 +426,16 @@ public function testGetExtensionNameReturnsFalseWhenNoExtensionName() : void

self::assertFalse($reflectionClassAdapter->getExtensionName());
}

public function testGetConstructorReturnsNullWhenNoConstructorExists() : void
{
$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
$betterReflectionClass
->method('getConstructor')
->willThrowException(new OutOfBoundsException());

$reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);

self::assertNull($reflectionClassAdapter->getConstructor());
}
}

0 comments on commit c022c6c

Please sign in to comment.