Passed
Push — develop ( 9ae090...ce9409 )
by Schlaefer
42s
created

PluginTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 6
dl 0
loc 44
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B testWhoops() 0 35 2
1
<?php
2
/**
3
 * @link http://philecms.github.io/
4
 * @license http://opensource.org/licenses/MIT
5
 * @package Phile\Plugin\Phile\ErrorHandler\Test
6
 */
7
8
namespace Phile\Plugin\Phile\ErrorHandler\Tests;
9
10
use Phile\Core\Bootstrap;
11
use Phile\Core\Config;
12
use Phile\Test\TestCase;
13
14
class PluginTest extends TestCase
15
{
16
    /**
17
     * Basic test that whoops plugin is running.
18
     *
19
     * The exception is not thrown but caught by Whoops and rendered plaintext
20
     * CLI response.
21
     */
22 1
    public function testWhoops()
23
    {
24 1
        $config = new Config([
25 1
            'plugins' => [
26
                'phile\\errorHandler' => ['active' => true, 'handler' => 'development']
27
            ]
28
        ]);
29 1
        $eventBus = new \Phile\Core\Event;
30 1
        $eventBus->register('after_init_core', function () {
31 1
            throw new \Exception('1845F098-9035-4D8E-9E31');
32 1
        });
33
34 1
        $request = $this->createServerRequestFromArray();
35
36
        try {
37 1
            $core = $this->createPhileCore($eventBus, $config);
38 1
            $core->addBootstrap(function ($eventBus, $config) {
39 1
                $config->set('phile_cli_mode', false);
40 1
                Bootstrap::setupErrorHandler($config);
41 1
            });
42 1
            $response = $this->createPhileResponse($core, $request);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43 1
        } catch (\Exception $e) {
44 1
            ob_start();
45 1
            $errorHandler = \Phile\Core\ServiceLocator::getService(
46 1
                'Phile_ErrorHandler'
47
            );
48 1
            $errorHandler->handleException($e);
49 1
            $body = ob_get_clean();
50
        }
51
52 1
        $this->assertStringStartsWith('Exception: 1845F098-9035-4D8E-9E31 in file', $body);
0 ignored issues
show
Bug introduced by
The variable $body 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...
53
54 1
        $handlers = set_exception_handler(null);
55 1
        $this->assertInstanceOf(\Phile\Plugin\Phile\ErrorHandler\Development::class, $handlers[0]);
56 1
    }
57
}
58