Failed Conditions
Push — master ( a26426...65249a )
by Arnold
14:53 queued 04:44
created

Header::convertNameToHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jasny\Controller\Parameter;
4
5
use Jasny\Controller\ParameterException;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
#[\Attribute]
9
class Header extends SingleParameter
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Header
Loading history...
10
{
11
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $type should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $required should have a doc-comment as per coding-style.
Loading history...
12
     * Get request header.
13
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
14
    public function getValue(ServerRequestInterface $request, string $name, ?string $type, bool $required = false): mixed
15
    {
16
        $key = $this->key ?? $this->convertNameToHeader($name);
17
        $value = $request->getHeaderLine($key);
18
19
        if ($required && $value === '') {
20
            throw new ParameterException("Missing required header '$key'");
21
        }
22
23
        return $this->filter($value, $type);
24
    }
25
26
    protected function convertNameToHeader(string $name): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function convertNameToHeader()
Loading history...
27
    {
28
        $sentence = preg_replace('/([a-z0-9])([A-Z])|(\w)_(\w)/', '$1$3-$2$4', $name);
29
        return str_replace(' ', '', ucwords($sentence, '-'));
30
    }
31
}
32