Completed
Push — master ( eb52d9...f4226f )
by recca
02:51
created

FileAPI::doReceive()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 18
cts 18
cp 1
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Recca0120\Upload\Apis;
4
5
class FileAPI extends Base
6
{
7
    /**
8
     * getOriginalName.
9
     *
10
     * @return string
11
     */
12 3
    protected function getOriginalName($contentDisposition)
13
    {
14 3
        $originalName = $this->request->get('name');
15 3
        if (empty($originalName) === true) {
16 3
            list($originalName) = sscanf(
17 3
                $contentDisposition,
18
                'attachment; filename=%s'
19 3
            );
20 3
        }
21
22 3
        return preg_replace('/[\'"]/', '', $originalName);
23
    }
24
25
    /**
26
     * getMimeType.
27
     *
28
     * @param string $originalName
29
     * @return string
30
     */
31 3
    protected function getMimeType($originalName)
32
    {
33 3
        $mimeType = $this->request->header('content-type');
34 3
        if (empty($mimeType) === true) {
35
            $mimeType = $this->filesystem->mimeType($originalName);
36
        }
37
38 3
        return $mimeType;
39
    }
40
41
    /**
42
     * receive.
43
     *
44
     * @param string $inputName
45
     * @return \Symfony\Component\HttpFoundation\File\UploadedFile
46
     *
47
     * @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
48
     */
49 4
    protected function doReceive($inputName)
50
    {
51 4
        $contentDisposition = $this->request->header('content-disposition');
52 4
        if (empty($contentDisposition) === true) {
53 1
            return $this->request->file($inputName);
54
        }
55
56 3
        $contentRange = $this->request->header('content-range');
57 3
        if (empty($contentRange) === false) {
58 2
            list($start, $end, $total) = sscanf($contentRange, 'bytes %d-%d/%d');
59 2
        } else {
60 1
            $start = 0;
61 1
            $end = $this->request->header('content-length');
62 1
            $total = $end;
63
        }
64
65 3
        return $this->receiveChunkedFile(
66 3
            $originalName = $this->getOriginalName($contentDisposition),
0 ignored issues
show
Bug introduced by
It seems like $originalName = $this->g...me($contentDisposition) can also be of type array<integer,string>; however, Recca0120\Upload\Apis\Base::receiveChunkedFile() does only seem to accept string, 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...
67 3
            'php://input',
68 3
            $start,
69 3
            $end >= $total - 1,
70 3
            ['mimeType' => $this->getMimeType($originalName), 'headers' => ['X-Last-Known-Byte' => $end]]
0 ignored issues
show
Bug introduced by
It seems like $originalName defined by $this->getOriginalName($contentDisposition) on line 66 can also be of type array<integer,string>; however, Recca0120\Upload\Apis\FileAPI::getMimeType() does only seem to accept string, 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...
71 3
        );
72
    }
73
}
74