Completed
Push — master ( b7cb0e...ffcfdb )
by Дмитрий
02:53
created

CompilingFixturesTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
dl 0
loc 50
rs 10
wmc 5
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A provideTestParseAndDump() 0 18 3
B testParseAndDump() 0 27 2
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) {
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