Skip to content

Commit

Permalink
Merge pull request #570 from localheinz/feature/constant
Browse files Browse the repository at this point in the history
Enhancement: Allow fetching constant name from exception
  • Loading branch information
Ocramius authored May 6, 2020
2 parents 0095a8d + 8e58d76 commit cf9ff24
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/NodeCompiler/Exception/UnableToCompileNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

class UnableToCompileNode extends LogicException
{
/** @var string|null */
private $constantName;

public function constantName() : ?string
{
return $this->constantName;
}

public static function forUnRecognizedExpressionInContext(Node\Expr $expression, CompilerContext $context) : self
{
return new self(sprintf(
Expand Down Expand Up @@ -45,12 +53,18 @@ public static function becauseOfNotFoundConstantReference(
CompilerContext $fetchContext,
Node\Expr\ConstFetch $constantFetch
) : self {
return new self(sprintf(
$constantName = reset($constantFetch->name->parts);

$exception = new self(sprintf(
'Could not locate constant "%s" while evaluating expression in %s at line %s',
reset($constantFetch->name->parts),
$constantName,
self::compilerContextToContextDescription($fetchContext),
$constantFetch->getLine()
));

$exception->constantName = $constantName;

return $exception;
}

private static function compilerContextToContextDescription(CompilerContext $fetchContext) : string
Expand Down
28 changes: 23 additions & 5 deletions test/unit/NodeCompiler/Exception/UnableToCompileNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,42 @@
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
use Roave\BetterReflectionTest\BetterReflectionSingleton;
use function sprintf;

/**
* @covers \Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode
*/
final class UnableToCompileNodeTest extends TestCase
{
public function testDefaults() : void
{
$exception = new UnableToCompileNode();

self::assertNull($exception->constantName());
}

/** @dataProvider supportedContextTypes */
public function testBecauseOfNotFoundConstantReference(CompilerContext $context) : void
{
$constantName = 'FOO';

$exception = UnableToCompileNode::becauseOfNotFoundConstantReference(
$context,
new ConstFetch(new Name($constantName))
);

$contextName = $context->hasSelf() ? 'EmptyClass' : 'unknown context (probably a function)';

self::assertSame(
'Could not locate constant "FOO" while evaluating expression in ' . $contextName . ' at line -1',
UnableToCompileNode::becauseOfNotFoundConstantReference(
$context,
new ConstFetch(new Name('FOO'))
)->getMessage()
sprintf(
'Could not locate constant "%s" while evaluating expression in %s at line -1',
$constantName,
$contextName
),
$exception->getMessage()
);

self::assertSame($constantName, $exception->constantName());
}

/** @dataProvider supportedContextTypes */
Expand Down

0 comments on commit cf9ff24

Please sign in to comment.