Test Failed
Pull Request — master (#5)
by Ayan
03:44
created

Mime::processMime()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.0042

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 23
c 4
b 0
f 0
dl 0
loc 32
ccs 17
cts 18
cp 0.9444
rs 9.2408
cc 5
nc 5
nop 1
crap 5.0042
1
<?php
2
3
namespace Phprest\Util;
4
5
use Phprest\Application;
6
use Phprest\Util\DataStructure\MimeProcessResult;
7
8
trait Mime
9
{
10
11 10
    protected function processMime(string $mime): MimeProcessResult
12
    {
13 10
        $vendor = (string)$this->getContainer()->get(Application::CONTAINER_ID_VENDOR);
14 10
        $apiVersion = (string)$this->getContainer()->get(Application::CONTAINER_ID_API_VERSION);
15 10
        $apiVersionRegExp = Application::API_VERSION_REG_EXP;
16 10
        $format = null;
17
18
        if (
19 10
            preg_match(
20 10
                '#application/vnd\.' . $vendor . '-v' . $apiVersionRegExp . '\+(xml|json)#',
21
                $mime,
22
                $matches
23
            )
24
        ) {
25 1
            [$mime, $apiVersion, $format] = $matches;
26
        } elseif (
27
            preg_match(
28 9
                '#application/vnd\.' . $vendor . '\+(xml|json).*?version=' . $apiVersionRegExp . '#',
29
                $mime,
30
                $matches
31
            )
32
        ) {
33 1
            [$mime, $format, $apiVersion] = $matches;
34 8
        } elseif ('application/json' === $mime) {
35 3
            $format = 'json';
36 3
            $mime = 'application/vnd.' . $vendor . '-v' . $apiVersion . '+json';
37 5
        } elseif ('application/xml' === $mime) {
38 2
            $format = 'xml';
39 2
            $mime = 'application/vnd.' . $vendor . '-v' . $apiVersion . '+xml';
40
        }
41
42 10
        return new MimeProcessResult($mime, $vendor, $apiVersion, $format);
0 ignored issues
show
Bug introduced by
It seems like $format can also be of type null; however, parameter $format of Phprest\Util\DataStructu...ssResult::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

42
        return new MimeProcessResult($mime, $vendor, $apiVersion, /** @scrutinizer ignore-type */ $format);
Loading history...
43
    }
44
45
    abstract protected function getContainer();
46
}
47