Completed
Pull Request — master (#196)
by Enrico
05:42
created

ClassConstFetch::compile()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 2
b 0
f 0
nc 5
nop 2
dl 0
loc 29
ccs 0
cts 18
cp 0
crap 30
rs 8.439
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
10
class ClassConstFetch extends AbstractExpressionCompiler
11
{
12
    protected $name = 'PhpParser\Node\Expr\ClassConstFetch';
13
14
    /**
15
     * classname::class, classname::CONSTANTNAME, ...
16
     *
17
     * @param \PhpParser\Node\Expr\ClassConstFetch $expr
18
     * @param Context $context
19
     * @return CompiledExpression
20
     */
21
    protected function compile($expr, Context $context)
22
    {
23
        $compiler = $context->getExpressionCompiler();
24
25
        if ($expr->name == "class") {
26
            // @todo return fully qualified classname
27
            return new CompiledExpression();
28
        }
29
30
        $leftCE = $compiler->compile($expr->class);
31
        if ($leftCE->isObject()) {
32
            $leftCEValue = $leftCE->getValue();
33
            if ($leftCEValue instanceof ClassDefinition) {
0 ignored issues
show
Bug introduced by
The class PHPSA\Compiler\Expression\ClassDefinition does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
34
                if (!$leftCEValue->hasConst($expr->name, true)) {
35
                    $context->notice(
36
                        'undefined-const',
37
                        sprintf('Constant %s does not exist in %s scope', $expr->name, $expr->class),
38
                        $expr
39
                    );
40
                    return new CompiledExpression(CompiledExpression::UNKNOWN);
41
                }
42
43
                return new CompiledExpression();
44
            }
45
        }
46
47
        $context->debug('Unknown const fetch', $expr);
48
        return new CompiledExpression();
49
    }
50
}
51