StaticPropertyFetch::compile()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 35
ccs 0
cts 21
cp 0
crap 20
rs 9.36
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Smart Analysis project 2015-2016
4
 *
5
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
6
 */
7
8
namespace PHPSA\Compiler\Expression;
9
10
use PHPSA\CompiledExpression;
11
use PHPSA\Context;
12
use PHPSA\Definition\ClassDefinition;
13
14
class StaticPropertyFetch extends AbstractExpressionCompiler
15
{
16
    protected $name = 'PhpParser\Node\Expr\StaticPropertyFetch';
17
18
    /**
19
     * {expr}::${expr};
20
     *
21
     * @param \PhpParser\Node\Expr\StaticPropertyFetch $expr
22
     * @param Context $context
23
     * @return CompiledExpression
24
     */
25
    protected function compile($expr, Context $context)
26
    {
27
        $expressionCompiler = $context->getExpressionCompiler();
28
        $leftCE = $expressionCompiler->compile($expr->class);
29
30
        if ($leftCE->isObject()) {
31
            $name = $expr->name;
32
33
            /** @var ClassDefinition $classDefinition */
34
            $classDefinition = $context->scope;
35
            if (!$classDefinition->hasProperty($name, true)) {
36
                $context->notice(
37
                    'language_error',
38
                    sprintf('Static property $%s does not exist in %s scope', $name, $expr->class),
39
                    $expr
40
                );
41
42
                return new CompiledExpression();
43
            }
44
45
            $property = $classDefinition->getPropertyStatement($name, true);
0 ignored issues
show
Documentation introduced by
$name is of type object<PhpParser\Node\Va...ct<PhpParser\Node\Expr>, 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...
46
            if (!$property->isStatic()) {
47
                $context->notice(
48
                    'language_error',
49
                    sprintf('Property $%s is not static but was called in a static way', $name),
50
                    $expr
51
                );
52
            }
53
54
            return $expressionCompiler->compile($property);
0 ignored issues
show
Bug introduced by
It seems like $property defined by $classDefinition->getPro...yStatement($name, true) on line 45 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...
55
        }
56
57
        $context->debug('Unknown static property fetch', $expr);
58
        return new CompiledExpression();
59
    }
60
}
61