Passed
Branch master (203e41)
by Nate
02:00
created

AbstractParameterHandler::handlePart()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 5
crap 4
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Retrofit\Internal\ParameterHandler;
10
11
use Generator;
12
use RuntimeException;
13
use Tebru\Retrofit\Http\MultipartBody;
14
use Tebru\Retrofit\ParameterHandler;
15
use Tebru\Retrofit\Internal\RequestBuilder;
16
use Tebru\Retrofit\RequestBodyConverter;
17
18
/**
19
 * Class AbstractFieldHandler
20
 *
21
 * @author Nate Brunette <[email protected]>
22
 */
23
abstract class AbstractParameterHandler implements ParameterHandler
24
{
25
    private const HEADER_CON_TRANS_ENC = 'Content-Transfer-Encoding';
26
27
    /**
28
     * Convert a value to a generator
29
     *
30
     * This method is used when a value can optionally be an array and each element in the
31
     * array should be processed the same way.
32
     *
33
     * @param array|mixed $list
34
     * @return Generator
35
     * @throws \RuntimeException
36
     */
37 22
    protected function getListValues($list): Generator
38
    {
39 22
        foreach ((array)$list as $key => $element) {
40 18
            if (!is_int($key)) {
41 1
                throw new RuntimeException('Retrofit: Array value must use numeric keys');
42
            }
43
44 17
            yield $element;
45
        }
46 21
    }
47
48
    /**
49
     * Handle Part or PartMap annotations
50
     *
51
     * This could use a simple method using name and value, or if a [@see MultipartBody] is passed in as the
52
     * value, then a filename and additional headers could be set as well.
53
     *
54
     * @param RequestBuilder $requestBuilder
55
     * @param RequestBodyConverter $converter
56
     * @param string $name
57
     * @param mixed $value
58
     * @param string $encoding
59
     * @return void
60
     */
61 6
    protected function handlePart(
62
        RequestBuilder $requestBuilder,
63
        RequestBodyConverter $converter,
64
        string $name,
65
        $value,
66
        string $encoding
67
    ): void {
68 6
        if ($value === null) {
69 2
            return;
70
        }
71
72
        // if not a MultipartBody, only set name, contents, and content header
73 6
        if (!$value instanceof MultipartBody) {
74 4
            $requestBuilder->addPart($name, $converter->convert($value), [self::HEADER_CON_TRANS_ENC => $encoding]);
75 4
            return;
76
        }
77
78 5
        $headers = $value->getHeaders();
79 5
        if (!isset($headers[self::HEADER_CON_TRANS_ENC])) {
80 5
            $headers[self::HEADER_CON_TRANS_ENC] = $encoding;
81
        }
82
83 5
        $requestBuilder->addPart($value->getName(), $value->getContents(), $headers, $value->getFilename());
0 ignored issues
show
Bug introduced by
It seems like $value->getContents() can be null; however, addPart() 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...
84 5
    }
85
}
86