Completed
Pull Request — master (#25)
by Alexander
01:37
created

Response::fileEndingFromType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Response::code() 0 3 1
1
<?php
2
3
namespace alkemann\h2l;
4
5
use alkemann\h2l\util\Http;
6
7
/**
8
 * Abstract class Response
9
 *
10
 * @package alkemann\h2l
11
 */
12
abstract class Response
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $config = [];
18
    /**
19
     * @var Message
20
     */
21
    protected $message;
22
23
    public function code(): int
24
    {
25
        return $this->message->code();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->message->code() could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
26
    }
27
28
    public function contentType(): string
29
    {
30
        return $this->message->contentType();
31
    }
32
33
    public function message(): ?Message
34
    {
35
        return $this->message;
36
    }
37
38
    /**
39
     * @throws \Error
40
     */
41
    protected function setHeaders(): void
42
    {
43
        $h = $this->config['header_func'] ?? 'header';
44
        if (is_callable($h) === false) {
45
            throw new \Error("header_func is not callable");
0 ignored issues
show
Unused Code introduced by
The call to Error::__construct() has too many arguments starting with 'header_func is not callable'. ( Ignorable by Annotation )

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

45
            throw /** @scrutinizer ignore-call */ new \Error("header_func is not callable");

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
46
        }
47
        $code = $this->message->code();
48
        if ($code != Http::CODE_OK) {
49
            $msg = Http::httpCodeToMessage($code);
50
            $h("HTTP/1.1 {$code} {$msg}");
51
        }
52
53
        foreach ($this->message->headers() as $name => $value) {
54
            $h("{$name}: $value");
55
        }
56
    }
57
58
    abstract public function render(): string;
59
}
60