CompilingFixturesTest::testParseAndDump()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
namespace Tests\PHPSA;
4
5
use PhpParser\ParserFactory;
6
use RecursiveDirectoryIterator;
7
use RecursiveIteratorIterator;
8
9
class CompilingFixturesTest extends TestCase
10
{
11
    public function provideTestParseAndDump()
12
    {
13
        $iter = new RecursiveIteratorIterator(
14
            new RecursiveDirectoryIterator(
15
                __DIR__ . '/../compiling-fixtures'
16
            ),
17
            RecursiveIteratorIterator::LEAVES_ONLY
18
        );
19
20
        foreach ($iter as $file) {
21
            if (!$file->isFile()) {
22
                continue;
23
            }
24
25
            $contents = file_get_contents($file);
26
            yield $file->getBasename() => explode('----------------------------', $contents);
27
        }
28
    }
29
30
    /** @dataProvider provideTestParseAndDump */
31
    public function testParseAndDump($code, $expectedDump)
32
    {
33
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative(
34
            array(
35
                'usedAttributes' => array(
36
                    'comments',
37
                    'startLine',
38
                    'endLine',
39
                    'startTokenPos',
40
                    'endTokenPos'
41
                )
42
            )
43
        ));
44
        $ast = $parser->parse($code);
45
46
        $expressionCompiler = $this->getContext()->getExpressionCompiler();
47
48
        foreach ($ast as $node) {
0 ignored issues
show
Bug introduced by
The expression $ast of type array<integer,object<PhpParser\Node\Stmt>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
49
            $compiledExpression = $expressionCompiler ->compile($node);
50
            self::assertInstanceOfCompiledExpression($compiledExpression);
51
        }
52
53
        self::assertSame(
54
            json_encode($compiledExpression->__debugInfo()),
0 ignored issues
show
Bug introduced by
The variable $compiledExpression does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
55
            trim($expectedDump)
56
        );
57
    }
58
}
59