Psr7Response::fileDownload()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 9
cts 10
cp 0.9
rs 9.4285
cc 3
eloc 11
nc 3
nop 2
crap 3.009
1
<?php
2
namespace Madewithlove\Export\Http;
3
4
use Psr\Http\Message\StreamInterface;
5
use Zend\Diactoros\Response;
6
7
trait Psr7Response
8
{
9
    /**
10
     * @param string $content
11
     * @param string $filename
12
     *
13
     * @return \Psr\Http\Message\ResponseInterface
14
     */
15 4
    private function fileDownload($content, $filename)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
16
    {
17 4
        $contentDisposition = sprintf("attachment; filename*=utf-8''%s", rawurlencode($filename));
18
19 4
        $response = new Response();
20
21 4
        if (is_string($content)) {
22 3
            $body = $response->getBody();
23 3
            $body->write($content);
24
        } elseif ($content instanceof StreamInterface) {
25 1
            $body = $content;
26
        }
27
28
        return $response
29 4
            ->withBody($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...
30 4
            ->withHeader('Content-Disposition', $contentDisposition);
31
    }
32
}