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

PropertyFetch::compile()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 29
nc 8
nop 2
dl 0
loc 43
ccs 0
cts 31
cp 0
crap 56
rs 6.7272
c 1
b 0
f 0
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 PropertyFetch extends AbstractExpressionCompiler
11
{
12
    protected $name = 'PhpParser\Node\Expr\PropertyFetch';
13
14
    /**
15
     * classname->property
16
     *
17
     * @param \PhpParser\Node\Expr\PropertyFetch $expr
18
     * @param Context $context
19
     * @return CompiledExpression
20
     */
21
    protected function compile($expr, Context $context)
22
    {
23
        $compiler = $context->getExpressionCompiler();
24
25
        $propertNameCE = $compiler->compile($expr->name);
26
27
        $scopeExpression = $compiler->compile($expr->var);
28
        if ($scopeExpression->isObject()) {
29
            $scopeExpressionValue = $scopeExpression->getValue();
30
            if ($scopeExpressionValue 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...
31
                $propertyName = $propertNameCE->isString() ? $propertNameCE->getValue() : false;
32
                if ($propertyName) {
33
                    if ($scopeExpressionValue->hasProperty($propertyName, true)) {
34
                        $property = $scopeExpressionValue->getProperty($propertyName, true);
35
                        return $compiler->compile($property);
36
                    } else {
37
                        $context->notice(
38
                            'undefined-property',
39
                            sprintf(
40
                                'Property %s does not exist in %s scope',
41
                                $propertyName,
42
                                $scopeExpressionValue->getName()
43
                            ),
44
                            $expr
45
                        );
46
                    }
47
                }
48
            }
49
50
            return new CompiledExpression();
51
        } elseif ($scopeExpression->canBeObject()) {
52
            return new CompiledExpression();
53
        }
54
55
        $context->notice(
56
            'property-fetch-on-non-object',
57
            "It's not possible to fetch a property on a non-object",
58
            $expr,
59
            Check::CHECK_BETA
60
        );
61
62
        return new CompiledExpression();
63
    }
64
}
65