Completed
Branch master (fea354)
by
unknown
03:14
created

HeaderParametersParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
wmc 6
lcom 1
cbo 6
ccs 25
cts 25
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B parse() 0 35 5
1
<?php namespace Neomerx\JsonApi\Http\Headers;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use \InvalidArgumentException;
20
use \Psr\Log\LoggerAwareTrait;
21
use \Psr\Log\LoggerAwareInterface;
22
use \Psr\Http\Message\ServerRequestInterface;
23
use \Neomerx\JsonApi\Exceptions\JsonApiException as E;
24
use \Neomerx\JsonApi\Contracts\Http\HttpFactoryInterface;
25
use \Neomerx\JsonApi\Contracts\Http\Headers\HeaderInterface;
26
use \Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
27
use \Neomerx\JsonApi\Contracts\Http\Headers\HeaderParametersParserInterface;
28
29
/**
30
 * @package Neomerx\JsonApi
31
 */
32
class HeaderParametersParser implements HeaderParametersParserInterface, LoggerAwareInterface
33
{
34
    use LoggerAwareTrait;
35
36
    /**
37
     * @var HttpFactoryInterface
38
     */
39
    private $factory;
40
41
    /**
42
     * @param HttpFactoryInterface $factory
43
     */
44 12
    public function __construct(HttpFactoryInterface $factory)
45
    {
46 12
        $this->factory = $factory;
47 12
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 12
    public function parse(ServerRequestInterface $request)
53
    {
54 12
        $acceptHeader      = null;
55 12
        $contentTypeHeader = null;
56
57 12
        $method = $request->getMethod();
58
59
        try {
60 12
            $contentType = $request->getHeader(HeaderInterface::HEADER_CONTENT_TYPE);
61 12
            $contentTypeHeader = Header::parse(
62 12
                empty($contentType) === true ? MediaTypeInterface::JSON_API_MEDIA_TYPE : $contentType[0],
63
                HeaderInterface::HEADER_CONTENT_TYPE
64 12
            );
65 12
        } catch (InvalidArgumentException $exception) {
66 1
            E::throwException(new E([], E::HTTP_CODE_BAD_REQUEST, $exception));
67
        }
68
69
        try {
70 11
            $headerString = $request->getHeader(HeaderInterface::HEADER_ACCEPT);
71 11
            if (empty($headerString) === false) {
72 10
                $acceptHeader = AcceptHeader::parse($headerString[0]);
73 9
            } else {
74 1
                $jsonMediaType = $this->factory->createAcceptMediaType(
75 1
                    0,
76 1
                    MediaTypeInterface::JSON_API_TYPE,
77
                    MediaTypeInterface::JSON_API_SUB_TYPE
78 1
                );
79 1
                $acceptHeader = $this->factory->createAcceptHeader([$jsonMediaType]);
80
            }
81 11
        } catch (InvalidArgumentException $exception) {
82 1
            E::throwException(new E([], E::HTTP_CODE_BAD_REQUEST, $exception));
83
        }
84
85 10
        return $this->factory->createHeaderParameters($method, $acceptHeader, $contentTypeHeader);
0 ignored issues
show
Bug introduced by
It seems like $acceptHeader can also be of type null or object<Neomerx\JsonApi\Http\Headers\Header>; however, Neomerx\JsonApi\Contract...reateHeaderParameters() does only seem to accept object<Neomerx\JsonApi\C...\AcceptHeaderInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $contentTypeHeader defined by null on line 55 can be null; however, Neomerx\JsonApi\Contract...reateHeaderParameters() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
86
    }
87
}
88