Completed
Push — master ( 5a5570...b1ed96 )
by Marco
10s
created

becauseOfNotFoundConstantReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BetterReflection\NodeCompiler\Exception;
6
7
use LogicException;
8
use PhpParser\Node;
9
use Roave\BetterReflection\NodeCompiler\CompilerContext;
10
use Roave\BetterReflection\Reflection\ReflectionClass;
11
use function get_class;
12
use function reset;
13
use function sprintf;
14
15
class UnableToCompileNode extends LogicException
16
{
17 2
    public static function forUnRecognizedExpressionInContext(Node\Expr $expression, CompilerContext $context) : self
18
    {
19 2
        return new self(sprintf(
20 2
            'Unable to compile expression in %s: unrecognized node type %s at line %d',
21 2
            self::compilerContextToContextDescription($context),
22 2
            get_class($expression),
23 2
            $expression->getLine()
24
        ));
25
    }
26
27 2
    public static function becauseOfNotFoundClassConstantReference(
28
        CompilerContext $fetchContext,
29
        ReflectionClass $targetClass,
30
        Node\Expr\ClassConstFetch $constantFetch
31
    ) : self {
32
        /** @var Node\Identifier $constantFetch->name */
33 2
        return new self(sprintf(
34 2
            'Could not locate constant %s::%s while trying to evaluate constant expression in %s at line %s',
35 2
            $targetClass->getName(),
36 2
            $constantFetch->name->name,
37 2
            self::compilerContextToContextDescription($fetchContext),
38 2
            $constantFetch->getLine()
39
        ));
40
    }
41
42 2
    public static function becauseOfNotFoundConstantReference(
43
        CompilerContext $fetchContext,
44
        Node\Expr\ConstFetch $constantFetch
45
    ) : self {
46
        /** @var Node\Name $constantFetch->name */
47 2
        return new self(sprintf(
48 2
            'Could not locate constant "%s" while evaluating expression in %s at line %s',
49 2
            reset($constantFetch->name->parts),
50 2
            self::compilerContextToContextDescription($fetchContext),
51 2
            $constantFetch->getLine()
52
        ));
53
    }
54
55 6
    private static function compilerContextToContextDescription(CompilerContext $fetchContext) : string
56
    {
57
        // @todo improve in https://github.com/Roave/BetterReflection/issues/434
58 6
        return $fetchContext->hasSelf()
59 3
            ? $fetchContext->getSelf()->getName()
60 6
            : 'unknown context (probably a function)';
61
    }
62
}
63