StaticSt::compile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Compiler\Statement;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Context;
7
8
class StaticSt extends AbstractCompiler
9
{
10
    protected $name = '\PhpParser\Node\Stmt\Static_';
11
12
    /**
13
     * @param \PhpParser\Node\Stmt\Static_ $stmt
14
     * @param Context $context
15
     * @return CompiledExpression
16
     */
17
    public function compile($stmt, Context $context)
18
    {
19
        $compiler = $context->getExpressionCompiler();
20
21
        foreach ($stmt->vars as $var) {
22
            $compiler->compile($var->default);
0 ignored issues
show
Bug introduced by
It seems like $var->default can be null; however, 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...
23
        }
24
    }
25
}
26