Completed
Push — stable ( 86ecc3...3a45ea )
by Nuno
07:34
created

it_confirms_the_running_in_console()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Tests;
4
5
use LaravelZero\Framework\Contracts\Exceptions\ConsoleException;
6
use Symfony\Component\Console\Exception\CommandNotFoundException;
7
8
class ApplicationTest extends TestCase
9
{
10
    /** @test */
11
    public function it_has_environment_getter()
12
    {
13
        $this->assertSame('Test version', $this->app->version());
14
    }
15
16
    /** @test */
17
    public function it_confirms_the_running_in_console()
18
    {
19
        $this->assertTrue(
20
            $this->app->runningInConsole()
21
        );
22
    }
23
24
    /** @test */
25
    public function it_confirms_that_is_not_down_for_maintenance()
26
    {
27
        $this->assertFalse(
28
            $this->app->isDownForMaintenance()
29
        );
30
    }
31
32
    /** @test */
33
    public function it_can_abort(): void
34
    {
35
        try {
36
            $this->app->abort(404, 'Foo');
37
        } catch (CommandNotFoundException $notFoundException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
38
        }
39
40
        $this->assertInstanceOf(CommandNotFoundException::class, $notFoundException);
0 ignored issues
show
Bug introduced by
The variable $notFoundException 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...
41
        $this->assertEquals($notFoundException->getMessage(), 'Foo');
42
43
        try {
44
            abort(200, 'Bar', ['Foo' => 'Bar']);
45
        } catch (ConsoleException $consoleException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
46
        }
47
48
        $this->assertInstanceOf(ConsoleException::class, $consoleException);
0 ignored issues
show
Bug introduced by
The variable $consoleException 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...
49
        $this->assertEquals($consoleException->getExitCode(), 200);
50
        $this->assertEquals($consoleException->getMessage(), 'Bar');
51
        $this->assertEquals($consoleException->getHeaders(), ['Foo' => 'Bar']);
52
    }
53
}
54