HttpPublisher::publish()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 21
rs 9.5222
1
<?php
2
3
namespace MiladRahimi\PhpRouter\Publisher;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * It publishes responses provided by controllers and middleware as HTTP responses
9
 */
10
class HttpPublisher implements Publisher
11
{
12
    /**
13
     * @inheritdoc
14
     */
15
    public function publish($response): void
16
    {
17
        $output = fopen('php://output', 'a');
18
19
        if ($response instanceof ResponseInterface) {
20
            http_response_code($response->getStatusCode());
21
22
            foreach ($response->getHeaders() as $name => $values) {
23
                @header($name . ': ' . $response->getHeaderLine($name));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for header(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

23
                /** @scrutinizer ignore-unhandled */ @header($name . ': ' . $response->getHeaderLine($name));

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
Are you sure the usage of header($name . ': ' . $r...->getHeaderLine($name)) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
24
            }
25
26
            fwrite($output, $response->getBody());
27
        } elseif (is_scalar($response)) {
28
            fwrite($output, $response);
29
        } elseif ($response === null) {
30
            fwrite($output, '');
31
        } else {
32
            fwrite($output, json_encode($response));
33
        }
34
35
        fclose($output);
36
    }
37
}
38