PropertyFetch::compile()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 8
nop 2
dl 0
loc 43
ccs 0
cts 28
cp 0
crap 56
rs 8.2986
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Compiler\Expression;
4
5
use PHPSA\Check;
6
use PHPSA\CompiledExpression;
7
use PHPSA\Context;
8
use PHPSA\Definition\ClassDefinition;
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) {
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);
0 ignored issues
show
Bug introduced by
It seems like $property defined by $scopeExpressionValue->g...ty($propertyName, true) on line 34 can be null; however, PHPSA\Compiler\Expression::compile() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
36
                    } else {
37
                        $context->notice(
38
                            'language_error',
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
            'language_error',
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