ClassConstFetch   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A compile() 0 29 5
1
<?php
2
3
namespace PHPSA\Compiler\Expression;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Context;
7
use PHPSA\Compiler\Expression;
8
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
9
use PHPSA\Definition\ClassDefinition;
10
11
class ClassConstFetch extends AbstractExpressionCompiler
12
{
13
    protected $name = 'PhpParser\Node\Expr\ClassConstFetch';
14
15
    /**
16
     * classname::class, classname::CONSTANTNAME, ...
17
     *
18
     * @param \PhpParser\Node\Expr\ClassConstFetch $expr
19
     * @param Context $context
20
     * @return CompiledExpression
21
     */
22
    protected function compile($expr, Context $context)
23
    {
24
        $compiler = $context->getExpressionCompiler();
25
26
        if ($expr->name == 'class') {
27
            // @todo return fully qualified classname
28
            return new CompiledExpression();
29
        }
30
31
        $leftCE = $compiler->compile($expr->class);
32
        if ($leftCE->isObject()) {
33
            $leftCEValue = $leftCE->getValue();
34
            if ($leftCEValue instanceof ClassDefinition) {
35
                if (!$leftCEValue->hasConst($expr->name, true)) {
0 ignored issues
show
Documentation introduced by
$expr->name is of type object<PhpParser\Node\Id...Parser\Node\Expr\Error>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
                    $context->notice(
37
                        'language_error',
38
                        sprintf('Constant %s does not exist in %s scope', $expr->name, $expr->class),
39
                        $expr
40
                    );
41
                    return new CompiledExpression(CompiledExpression::UNKNOWN);
42
                }
43
44
                return new CompiledExpression();
45
            }
46
        }
47
48
        $context->debug('Unknown const fetch', $expr);
49
        return new CompiledExpression();
50
    }
51
}
52