Skip to content

Commit

Permalink
added context reflection class and method support
Browse files Browse the repository at this point in the history
  • Loading branch information
robfrawley committed Mar 29, 2018
1 parent 986d65c commit a48b026
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 21 deletions.
22 changes: 20 additions & 2 deletions lib/ExceptionContextInterface.php
Expand Up @@ -22,19 +22,37 @@ interface ExceptionContextInterface extends \Throwable
*/
public function getContext(): FileContextInterface;

/**
* Returns the reflection class of the thrown exception's context.
*
* @return null|\ReflectionClass
*/
public function getContextClass(): ?\ReflectionClass;

/**
* Returns the class name of the thrown exception's context.
*
* @param bool $qualified
*
* @return null|string
*/
public function getContextClass(): ?string;
public function getContextClassName(bool $qualified = true): ?string;

/**
* Returns the reflection method of the thrown exception's context.
*
* @return null|\ReflectionMethod
*/
public function getContextMethod(): ?\ReflectionMethod;

/**
* Returns the method name of the thrown exception's context.
*
* @param bool $qualified
*
* @return null|string
*/
public function getContextMethod(): ?string;
public function getContextMethodName(bool $qualified = false): ?string;

/**
* Returns file lines for the line context.
Expand Down
40 changes: 36 additions & 4 deletions lib/ExceptionContextTrait.php
Expand Up @@ -35,15 +35,45 @@ final public function getContext(): FileContextInterface
return $this->context;
}

/**
* Returns the reflection class of the thrown exception's context.
*
* @return null|\ReflectionClass
*/
final public function getContextClass(): ?\ReflectionClass
{
try {
return $this->getContext()->getClass();
} catch (\RuntimeException $e) {
return null;
}
}

/**
* Returns the class name of the thrown exception's context.
*
* @param bool $qualified
*
* @return null|string
*/
final public function getContextClass(): ?string
final public function getContextClassName(bool $qualified = true): ?string
{
try {
return $this->getContext()->getClassName(true);
return $this->getContext()->getClassName($qualified);
} catch (\RuntimeException $e) {
return null;
}
}

/**
* Returns the reflection method of the thrown exception's context.
*
* @return null|\ReflectionMethod
*/
final public function getContextMethod(): ?\ReflectionMethod
{
try {
return $this->getContext()->getMethod();
} catch (\RuntimeException $e) {
return null;
}
Expand All @@ -52,12 +82,14 @@ final public function getContextClass(): ?string
/**
* Returns the method name of the thrown exception's context.
*
* @param bool $qualified
*
* @return null|string
*/
final public function getContextMethod(): ?string
final public function getContextMethodName(bool $qualified = false): ?string
{
try {
return $this->getContext()->getMethodName(true);
return $this->getContext()->getMethodName($qualified);
} catch (\RuntimeException $e) {
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/ExceptionTrait.php
Expand Up @@ -41,7 +41,7 @@ final public function __toString(): string
$message = vsprintf('%s: %s (in "%s" at "%s:%d").', [
$this->getType(false),
$this->getMessage(),
$this->getContextMethod(),
$this->getContextMethodName(),
$this->getFile(),
$this->getLine(),
]);
Expand All @@ -65,8 +65,8 @@ final public function __toArray(): array
'message' => $this->getMessage(),
'code' => $this->getCode(),
'context' => $this->getContext(),
'class' => $this->getContextClass(),
'method' => $this->getContextMethod(),
'class' => $this->getContextClassName(),
'method' => $this->getContextMethodName(),
'file-name' => $this->getFile(),
'file-line' => $this->getLine(),
'file-diff' => $this->getContextFileSnippet(),
Expand Down
18 changes: 10 additions & 8 deletions tests/ExceptionTest.php
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use SR\Exception\Exception;
use SR\Utilities\ClassQuery;

/**
* @covers \SR\Exception\Exception
Expand All @@ -39,16 +40,15 @@ public function testGetInput()
$this->assertSame($i, $e2->getInputReplacements());
}

public function testGetMethod()
public function testGetMethodAndClassContext()
{
$exception = $this->getException();
$this->assertSame(__CLASS__.'::getException', $exception->getContextMethod());
}

public function testGetClass()
{
$exception = $this->getException();
$this->assertSame(__CLASS__, $exception->getContextClass());
$this->assertSame('getException', $exception->getContextMethod()->getShortName());
$this->assertSame('getException', $exception->getContextMethodName());
$this->assertSame(__CLASS__.'::getException', $exception->getContextMethodName(true));
$this->assertSame(__CLASS__, $exception->getContextClass()->getName());
$this->assertSame(__CLASS__, $exception->getContextClassName());
$this->assertSame(ClassQuery::getNameShort(__CLASS__), $exception->getContextClassName(false));
}

public function testFileDiff()
Expand Down Expand Up @@ -217,7 +217,9 @@ public function testToStringExceptionNotThrownOnBadContext()
$property->setValue($exception, realpath(__DIR__.'/Fixtures/NoClass.php'));

$this->assertNull($exception->getContextClass());
$this->assertNull($exception->getContextClassName());
$this->assertNull($exception->getContextMethod());
$this->assertNull($exception->getContextMethodName());
$this->assertCount(0, $exception->getContextFileSnippet());
}

Expand Down
13 changes: 9 additions & 4 deletions tests/ExceptionTypesTest.php
Expand Up @@ -26,6 +26,7 @@
use SR\Exception\Runtime\RuntimeException;
use SR\Exception\Runtime\UnderflowException;
use SR\Exception\Runtime\UnexpectedValueException;
use SR\Utilities\ClassQuery;
use SR\Utilities\Context\FileContextInterface;

/**
Expand Down Expand Up @@ -85,15 +86,19 @@ public function testBasicFunctionality($type)
$this->assertInstanceOf(FileContextInterface::class, $exception->getContext());
$this->assertSame(__FILE__, $exception->getFile());
$this->assertSame(__FILE__, $exception->getContext()->getFile()->getPathname());
$this->assertSame(__CLASS__, $exception->getContextClass());
$this->assertSame(__CLASS__.'::getException', $exception->getContextMethod());
$this->assertSame(__CLASS__, $exception->getContextClassName());
$this->assertSame(ClassQuery::getNameShort(__CLASS__), $exception->getContextClassName(false));
$this->assertSame('getException', $exception->getContextMethodName());
$this->assertSame(__CLASS__.'::getException', $exception->getContextMethodName(true));
$this->assertSame($previous, $exception->getPrevious());

$exception = $this->getExceptionStatic($type, $previous);
$this->assertSame(__FILE__, $exception->getFile());
$this->assertSame(__FILE__, $exception->getContext()->getFile()->getPathname());
$this->assertSame(__CLASS__, $exception->getContextClass());
$this->assertSame(__CLASS__.'::getExceptionStatic', $exception->getContextMethod());
$this->assertSame(__CLASS__, $exception->getContextClassName());
$this->assertSame(ClassQuery::getNameShort(__CLASS__), $exception->getContextClassName(false));
$this->assertSame('getExceptionStatic', $exception->getContextMethodName());
$this->assertSame(__CLASS__.'::getExceptionStatic', $exception->getContextMethodName(true));
$this->assertSame($previous, $exception->getPrevious());
}

Expand Down

0 comments on commit a48b026

Please sign in to comment.